Module: Searchkick
- Defined in:
- lib/searchkick.rb,
lib/searchkick/index.rb,
lib/searchkick/model.rb,
lib/searchkick/query.rb,
lib/searchkick/logging.rb,
lib/searchkick/results.rb,
lib/searchkick/version.rb,
lib/searchkick/middleware.rb,
lib/searchkick/reindex_job.rb,
lib/searchkick/reindex_v2_job.rb
Defined Under Namespace
Modules: ControllerRuntime, IndexWithInstrumentation, Model, QueryWithInstrumentation, Reindex, SearchkickWithInstrumentation Classes: DangerousOperation, Error, ImportError, Index, InvalidQueryError, LogSubscriber, Middleware, MissingIndexError, Query, ReindexJob, ReindexV2Job, Results, UnsupportedVersionError
Constant Summary collapse
- VERSION =
"0.0.2"
Class Attribute Summary collapse
- .client ⇒ Object
- .env ⇒ Object
-
.models ⇒ Object
Returns the value of attribute models.
-
.search_method_name ⇒ Object
Returns the value of attribute search_method_name.
- .search_timeout ⇒ Object
-
.timeout ⇒ Object
Returns the value of attribute timeout.
-
.wordnet_path ⇒ Object
Returns the value of attribute wordnet_path.
Class Method Summary collapse
- .callbacks(value) ⇒ Object
- .callbacks? ⇒ Boolean
-
.callbacks_value ⇒ Object
private.
-
.callbacks_value=(value) ⇒ Object
private.
-
.clear_queued_items ⇒ Object
private.
- .disable_callbacks ⇒ Object
- .enable_callbacks ⇒ Object
- .multi_search(queries) ⇒ Object
-
.perform_bulk ⇒ Object
private.
-
.perform_items(items) ⇒ Object
private.
-
.queue_items(items) ⇒ Object
private.
-
.queued_items ⇒ Object
private.
- .search(term = nil, options = {}, &block) ⇒ Object
- .server_below?(version) ⇒ Boolean
- .server_version ⇒ Object
Class Attribute Details
.client ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/searchkick.rb', line 39 def self.client @client ||= Elasticsearch::Client.new( url: ENV["ELASTICSEARCH_URL"], transport_options: {request: {timeout: timeout}} ) do |f| f.use Searchkick::Middleware end end |
.env ⇒ Object
49 50 51 |
# File 'lib/searchkick.rb', line 49 def self.env @env ||= ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development" end |
.models ⇒ Object
Returns the value of attribute models.
31 32 33 |
# File 'lib/searchkick.rb', line 31 def models @models end |
.search_method_name ⇒ Object
Returns the value of attribute search_method_name.
31 32 33 |
# File 'lib/searchkick.rb', line 31 def search_method_name @search_method_name end |
.search_timeout ⇒ Object
53 54 55 |
# File 'lib/searchkick.rb', line 53 def self.search_timeout @search_timeout || timeout end |
.timeout ⇒ Object
Returns the value of attribute timeout.
31 32 33 |
# File 'lib/searchkick.rb', line 31 def timeout @timeout end |
.wordnet_path ⇒ Object
Returns the value of attribute wordnet_path.
31 32 33 |
# File 'lib/searchkick.rb', line 31 def wordnet_path @wordnet_path end |
Class Method Details
.callbacks(value) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/searchkick.rb', line 77 def self.callbacks(value) if block_given? previous_value = callbacks_value begin self.callbacks_value = value yield perform_bulk if callbacks_value == :bulk ensure self.callbacks_value = previous_value end else self.callbacks_value = value end end |
.callbacks? ⇒ Boolean
73 74 75 |
# File 'lib/searchkick.rb', line 73 def self.callbacks? Thread.current[:searchkick_callbacks_enabled].nil? || Thread.current[:searchkick_callbacks_enabled] end |
.callbacks_value ⇒ Object
private
127 128 129 |
# File 'lib/searchkick.rb', line 127 def self.callbacks_value Thread.current[:searchkick_callbacks_enabled] end |
.callbacks_value=(value) ⇒ Object
private
132 133 134 |
# File 'lib/searchkick.rb', line 132 def self.callbacks_value=(value) Thread.current[:searchkick_callbacks_enabled] = value end |
.clear_queued_items ⇒ Object
private
122 123 124 |
# File 'lib/searchkick.rb', line 122 def self.clear_queued_items Thread.current[:searchkick_queued_items] = [] end |
.disable_callbacks ⇒ Object
69 70 71 |
# File 'lib/searchkick.rb', line 69 def self.disable_callbacks self.callbacks_value = false end |
.enable_callbacks ⇒ Object
65 66 67 |
# File 'lib/searchkick.rb', line 65 def self.enable_callbacks self.callbacks_value = nil end |
.multi_search(queries) ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/searchkick.rb', line 146 def self.multi_search(queries) if queries.any? responses = client.msearch(body: queries.flat_map { |q| [q.params.except(:body), q.body] })["responses"] queries.each_with_index do |query, i| query.handle_response(responses[i]) end end nil end |
.perform_bulk ⇒ Object
private
99 100 101 102 103 |
# File 'lib/searchkick.rb', line 99 def self.perform_bulk items = queued_items clear_queued_items perform_items(items) end |
.perform_items(items) ⇒ Object
private
106 107 108 109 110 111 112 113 114 |
# File 'lib/searchkick.rb', line 106 def self.perform_items(items) if items.any? response = client.bulk(body: items) if response["errors"] first_item = response["items"].first raise Searchkick::ImportError, (first_item["index"] || first_item["delete"])["error"] end end end |
.queue_items(items) ⇒ Object
private
93 94 95 96 |
# File 'lib/searchkick.rb', line 93 def self.queue_items(items) queued_items.concat(items) perform_bulk unless callbacks_value == :bulk end |
.queued_items ⇒ Object
private
117 118 119 |
# File 'lib/searchkick.rb', line 117 def self.queued_items Thread.current[:searchkick_queued_items] ||= [] end |
.search(term = nil, options = {}, &block) ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/searchkick.rb', line 136 def self.search(term = nil, = {}, &block) query = Searchkick::Query.new(nil, term, ) block.call(query.body) if block if [:execute] == false query else query.execute end end |
.server_below?(version) ⇒ Boolean
61 62 63 |
# File 'lib/searchkick.rb', line 61 def self.server_below?(version) Gem::Version.new(server_version.sub("-", ".")) < Gem::Version.new(version.sub("-", ".")) end |
.server_version ⇒ Object
57 58 59 |
# File 'lib/searchkick.rb', line 57 def self.server_version @server_version ||= client.info["version"]["number"] end |