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/multi_search.rb,
lib/searchkick/index_options.rb,
lib/searchkick/reindex_queue.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: BulkReindexJob, DangerousOperation, Error, ImportError, Index, Indexer, InvalidQueryError, LogSubscriber, Middleware, MissingIndexError, MultiSearch, ProcessBatchJob, ProcessQueueJob, Query, ReindexQueue, ReindexV2Job, Results, UnsupportedVersionError

Constant Summary collapse

VERSION =
"2.4.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.aws_credentialsObject

Returns the value of attribute aws_credentials.



42
43
44
# File 'lib/searchkick.rb', line 42

def aws_credentials
  @aws_credentials
end

.clientObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/searchkick.rb', line 51

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"}}
    }.deep_merge(client_options)) do |f|
      f.use Searchkick::Middleware
      f.request :aws_signers_v4, {
        credentials: Aws::Credentials.new(aws_credentials[:access_key_id], aws_credentials[:secret_access_key]),
        service_name: "es",
        region: aws_credentials[:region] || "us-east-1"
      } if aws_credentials
    end
  end
end

.client_optionsObject

Returns the value of attribute client_options.



40
41
42
# File 'lib/searchkick.rb', line 40

def client_options
  @client_options
end

.envObject



69
70
71
# File 'lib/searchkick.rb', line 69

def self.env
  @env ||= ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
end

.index_prefixObject

Returns the value of attribute index_prefix.



40
41
42
# File 'lib/searchkick.rb', line 40

def index_prefix
  @index_prefix
end

.index_suffixObject

Returns the value of attribute index_suffix.



40
41
42
# File 'lib/searchkick.rb', line 40

def index_suffix
  @index_suffix
end

.modelsObject

Returns the value of attribute models.



40
41
42
# File 'lib/searchkick.rb', line 40

def models
  @models
end

.queue_nameObject

Returns the value of attribute queue_name.



40
41
42
# File 'lib/searchkick.rb', line 40

def queue_name
  @queue_name
end

.redisObject

Returns the value of attribute redis.



40
41
42
# File 'lib/searchkick.rb', line 40

def redis
  @redis
end

.search_method_nameObject

Returns the value of attribute search_method_name.



40
41
42
# File 'lib/searchkick.rb', line 40

def search_method_name
  @search_method_name
end

.search_timeoutObject



73
74
75
# File 'lib/searchkick.rb', line 73

def self.search_timeout
  @search_timeout || timeout
end

.timeoutObject

Returns the value of attribute timeout.



40
41
42
# File 'lib/searchkick.rb', line 40

def timeout
  @timeout
end

.wordnet_pathObject

Returns the value of attribute wordnet_path.



40
41
42
# File 'lib/searchkick.rb', line 40

def wordnet_path
  @wordnet_path
end

Class Method Details

.callbacks(value) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/searchkick.rb', line 123

def self.callbacks(value)
  if block_given?
    previous_value = callbacks_value
    begin
      self.callbacks_value = value
      yield
      indexer.perform if callbacks_value == :bulk
    ensure
      self.callbacks_value = previous_value
    end
  else
    self.callbacks_value = value
  end
end

.callbacks?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/searchkick.rb', line 119

def self.callbacks?
  Thread.current[:searchkick_callbacks_enabled].nil? || Thread.current[:searchkick_callbacks_enabled]
end

.callbacks_valueObject

private



195
196
197
# File 'lib/searchkick.rb', line 195

def self.callbacks_value
  Thread.current[:searchkick_callbacks_enabled]
end

.callbacks_value=(value) ⇒ Object

private



200
201
202
# File 'lib/searchkick.rb', line 200

def self.callbacks_value=(value)
  Thread.current[:searchkick_callbacks_enabled] = value
end

.disable_callbacksObject



115
116
117
# File 'lib/searchkick.rb', line 115

def self.disable_callbacks
  self.callbacks_value = false
end

.enable_callbacksObject

callbacks



111
112
113
# File 'lib/searchkick.rb', line 111

def self.enable_callbacks
  self.callbacks_value = nil
end

.indexerObject

private



190
191
192
# File 'lib/searchkick.rb', line 190

def self.indexer
  Thread.current[:searchkick_indexer] ||= Searchkick::Indexer.new
end

.load_records(records, ids) ⇒ Object

private

Raises:



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/searchkick.rb', line 169

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, retry_misspellings: false) ⇒ Object



105
106
107
# File 'lib/searchkick.rb', line 105

def self.multi_search(queries, retry_misspellings: false)
  Searchkick::MultiSearch.new(queries, retry_misspellings: retry_misspellings).perform
end

.reindex_status(index_name) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/searchkick.rb', line 144

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 = "*", **options, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/searchkick.rb', line 85

def self.search(term = "*", **options, &block)
  klass = options[:model]

  # TODO add in next major version
  # if !klass
  #   index_name = Array(options[:index_name])
  #   if index_name.size == 1 && index_name.first.respond_to?(:searchkick_index)
  #     klass = index_name.first
  #   end
  # end

  query = Searchkick::Query.new(klass, term, options.except(:model))
  block.call(query.body) if block
  if options[:execute] == false
    query
  else
    query.execute
  end
end

.server_below?(version) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/searchkick.rb', line 81

def self.server_below?(version)
  Gem::Version.new(server_version.sub("-", ".")) < Gem::Version.new(version.sub("-", "."))
end

.server_versionObject



77
78
79
# File 'lib/searchkick.rb', line 77

def self.server_version
  @server_version ||= client.info["version"]["number"]
end

.with_redisObject



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/searchkick.rb', line 156

def self.with_redis
  if redis
    if redis.respond_to?(:with)
      redis.with do |r|
        yield r
      end
    else
      yield redis
    end
  end
end