Module: Searchkick
- Defined in:
- lib/searchkick.rb,
lib/searchkick/index.rb,
lib/searchkick/model.rb,
lib/searchkick/query.rb,
lib/searchkick/indexer.rb,
lib/searchkick/logging.rb,
lib/searchkick/results.rb,
lib/searchkick/version.rb,
lib/searchkick/middleware.rb,
lib/searchkick/record_data.rb,
lib/searchkick/bulk_indexer.rb,
lib/searchkick/hash_wrapper.rb,
lib/searchkick/multi_search.rb,
lib/searchkick/index_options.rb,
lib/searchkick/reindex_queue.rb,
lib/searchkick/record_indexer.rb,
lib/searchkick/reindex_v2_job.rb,
lib/searchkick/bulk_reindex_job.rb,
lib/searchkick/process_batch_job.rb,
lib/searchkick/process_queue_job.rb
Defined Under Namespace
Modules: ControllerRuntime, IndexOptions, IndexWithInstrumentation, IndexerWithInstrumentation, Model, QueryWithInstrumentation, SearchkickWithInstrumentation Classes: BulkIndexer, BulkReindexJob, DangerousOperation, Error, HashWrapper, ImportError, Index, Indexer, InvalidQueryError, LogSubscriber, Middleware, MissingIndexError, MultiSearch, ProcessBatchJob, ProcessQueueJob, Query, RecordData, RecordIndexer, ReindexQueue, ReindexV2Job, Results, UnsupportedVersionError
Constant Summary collapse
- VERSION =
"0.0.2"
Class Attribute Summary collapse
-
.aws_credentials ⇒ Object
Returns the value of attribute aws_credentials.
- .client ⇒ Object
-
.client_options ⇒ Object
Returns the value of attribute client_options.
- .env ⇒ Object
-
.index_prefix ⇒ Object
Returns the value of attribute index_prefix.
-
.index_suffix ⇒ Object
Returns the value of attribute index_suffix.
-
.model_options ⇒ Object
Returns the value of attribute model_options.
-
.models ⇒ Object
Returns the value of attribute models.
-
.queue_name ⇒ Object
Returns the value of attribute queue_name.
-
.redis ⇒ Object
Returns the value of attribute redis.
-
.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?(default: true) ⇒ Boolean
-
.callbacks_value ⇒ Object
private.
-
.callbacks_value=(value) ⇒ Object
private.
- .disable_callbacks ⇒ Object
-
.enable_callbacks ⇒ Object
callbacks.
-
.indexer ⇒ Object
private.
-
.load_records(records, ids) ⇒ Object
private.
- .multi_search(queries) ⇒ Object
- .reindex_status(index_name) ⇒ Object
- .search(term = "*", model: nil, **options, &block) ⇒ Object
- .server_below?(version) ⇒ Boolean
- .server_version ⇒ Object
-
.signer_middleware_aws_params ⇒ Object
private.
-
.signer_middleware_key ⇒ Object
private.
- .with_redis ⇒ Object
Instance Method Summary collapse
Class Attribute Details
.aws_credentials ⇒ Object
Returns the value of attribute aws_credentials.
60 61 62 |
# File 'lib/searchkick.rb', line 60 def aws_credentials @aws_credentials end |
.client ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/searchkick.rb', line 70 def self.client @client ||= begin require "typhoeus/adapters/faraday" if defined?(Typhoeus) Elasticsearch::Client.new({ url: ENV["ELASTICSEARCH_URL"], transport_options: {request: {timeout: timeout}, headers: {content_type: "application/json"}}, retry_on_failure: 2 }.deep_merge()) do |f| f.use Searchkick::Middleware f.request signer_middleware_key, signer_middleware_aws_params if aws_credentials end end end |
.client_options ⇒ Object
Returns the value of attribute client_options.
58 59 60 |
# File 'lib/searchkick.rb', line 58 def @client_options end |
.env ⇒ Object
85 86 87 |
# File 'lib/searchkick.rb', line 85 def self.env @env ||= ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development" end |
.index_prefix ⇒ Object
Returns the value of attribute index_prefix.
58 59 60 |
# File 'lib/searchkick.rb', line 58 def index_prefix @index_prefix end |
.index_suffix ⇒ Object
Returns the value of attribute index_suffix.
58 59 60 |
# File 'lib/searchkick.rb', line 58 def index_suffix @index_suffix end |
.model_options ⇒ Object
Returns the value of attribute model_options.
58 59 60 |
# File 'lib/searchkick.rb', line 58 def @model_options end |
.models ⇒ Object
Returns the value of attribute models.
58 59 60 |
# File 'lib/searchkick.rb', line 58 def models @models end |
.queue_name ⇒ Object
Returns the value of attribute queue_name.
58 59 60 |
# File 'lib/searchkick.rb', line 58 def queue_name @queue_name end |
.redis ⇒ Object
Returns the value of attribute redis.
58 59 60 |
# File 'lib/searchkick.rb', line 58 def redis @redis end |
.search_method_name ⇒ Object
Returns the value of attribute search_method_name.
58 59 60 |
# File 'lib/searchkick.rb', line 58 def search_method_name @search_method_name end |
.search_timeout ⇒ Object
89 90 91 |
# File 'lib/searchkick.rb', line 89 def self.search_timeout @search_timeout || timeout end |
.timeout ⇒ Object
Returns the value of attribute timeout.
58 59 60 |
# File 'lib/searchkick.rb', line 58 def timeout @timeout end |
.wordnet_path ⇒ Object
Returns the value of attribute wordnet_path.
58 59 60 |
# File 'lib/searchkick.rb', line 58 def wordnet_path @wordnet_path end |
Class Method Details
.callbacks(value) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/searchkick.rb', line 145 def self.callbacks(value) if block_given? previous_value = callbacks_value begin self.callbacks_value = value result = yield indexer.perform if callbacks_value == :bulk result ensure self.callbacks_value = previous_value end else self.callbacks_value = value end end |
.callbacks?(default: true) ⇒ Boolean
137 138 139 140 141 142 143 |
# File 'lib/searchkick.rb', line 137 def self.callbacks?(default: true) if callbacks_value.nil? default else callbacks_value != false end end |
.callbacks_value ⇒ Object
private
222 223 224 |
# File 'lib/searchkick.rb', line 222 def self.callbacks_value Thread.current[:searchkick_callbacks_enabled] end |
.callbacks_value=(value) ⇒ Object
private
227 228 229 |
# File 'lib/searchkick.rb', line 227 def self.callbacks_value=(value) Thread.current[:searchkick_callbacks_enabled] = value end |
.disable_callbacks ⇒ Object
133 134 135 |
# File 'lib/searchkick.rb', line 133 def self.disable_callbacks self.callbacks_value = false end |
.enable_callbacks ⇒ Object
callbacks
129 130 131 |
# File 'lib/searchkick.rb', line 129 def self.enable_callbacks self.callbacks_value = nil end |
.indexer ⇒ Object
private
217 218 219 |
# File 'lib/searchkick.rb', line 217 def self.indexer Thread.current[:searchkick_indexer] ||= Searchkick::Indexer.new end |
.load_records(records, ids) ⇒ Object
private
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/searchkick.rb', line 196 def self.load_records(records, ids) records = if records.respond_to?(:primary_key) # ActiveRecord records.where(records.primary_key => ids) if records.primary_key elsif records.respond_to?(:queryable) # Mongoid 3+ records.queryable.for_ids(ids) elsif records.respond_to?(:unscoped) && :id.respond_to?(:in) # Nobrainer records.unscoped.where(:id.in => ids) elsif records.respond_to?(:key_column_names) records.where(records.key_column_names.first => ids) end raise Searchkick::Error, "Not sure how to load records" if !records records end |
.multi_search(queries) ⇒ Object
123 124 125 |
# File 'lib/searchkick.rb', line 123 def self.multi_search(queries) Searchkick::MultiSearch.new(queries).perform end |
.reindex_status(index_name) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/searchkick.rb', line 171 def self.reindex_status(index_name) if redis batches_left = Searchkick::Index.new(index_name).batches_left { completed: batches_left == 0, batches_left: batches_left } else raise Searchkick::Error, "Redis not configured" end end |
.search(term = "*", model: nil, **options, &block) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/searchkick.rb', line 101 def self.search(term = "*", model: nil, **, &block) = .dup klass = model # make Searchkick.search(index_name: [Product]) and Product.search equivalent unless klass index_name = Array([:index_name]) if index_name.size == 1 && index_name.first.respond_to?(:searchkick_index) klass = index_name.first .delete(:index_name) end end query = Searchkick::Query.new(klass, term, ) block.call(query.body) if block if [:execute] == false query else query.execute end end |
.server_below?(version) ⇒ Boolean
97 98 99 |
# File 'lib/searchkick.rb', line 97 def self.server_below?(version) Gem::Version.new(server_version.split("-")[0]) < Gem::Version.new(version.split("-")[0]) end |
.server_version ⇒ Object
93 94 95 |
# File 'lib/searchkick.rb', line 93 def self.server_version @server_version ||= client.info["version"]["number"] end |
.signer_middleware_aws_params ⇒ Object
private
237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/searchkick.rb', line 237 def self.signer_middleware_aws_params if signer_middleware_key == :aws_sigv4 {service: "es", region: "us-east-1"}.merge(aws_credentials) else { credentials: aws_credentials[:credentials] || Aws::Credentials.new(aws_credentials[:access_key_id], aws_credentials[:secret_access_key]), service_name: "es", region: aws_credentials[:region] || "us-east-1" } end end |
.signer_middleware_key ⇒ Object
private
232 233 234 |
# File 'lib/searchkick.rb', line 232 def self.signer_middleware_key defined?(FaradayMiddleware::AwsSignersV4) ? :aws_signers_v4 : :aws_sigv4 end |
.with_redis ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/searchkick.rb', line 183 def self.with_redis if redis if redis.respond_to?(:with) redis.with do |r| yield r end else yield redis end end end |
Instance Method Details
#locale ⇒ Object
24 25 26 |
# File 'lib/searchkick/bulk_reindex_job.rb', line 24 def locale return "en" end |