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/index_options.rb,
lib/searchkick/reindex_v2_job.rb

Defined Under Namespace

Modules: ControllerRuntime, IndexOptions, IndexWithInstrumentation, Model, QueryWithInstrumentation, Reindex, SearchkickWithInstrumentation Classes: DangerousOperation, Error, ImportError, Index, InvalidQueryError, LogSubscriber, Middleware, MissingIndexError, Query, ReindexJob, ReindexV2Job, Results, UnsupportedVersionError

Constant Summary collapse

VERSION =
"1.3.6"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clientObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/searchkick.rb', line 40

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"}}
    ) do |f|
      f.use Searchkick::Middleware
    end
  end
end

.envObject



53
54
55
# File 'lib/searchkick.rb', line 53

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

.modelsObject

Returns the value of attribute models.



32
33
34
# File 'lib/searchkick.rb', line 32

def models
  @models
end

.search_method_nameObject

Returns the value of attribute search_method_name.



32
33
34
# File 'lib/searchkick.rb', line 32

def search_method_name
  @search_method_name
end

.search_timeoutObject



57
58
59
# File 'lib/searchkick.rb', line 57

def self.search_timeout
  @search_timeout || timeout
end

.timeoutObject

Returns the value of attribute timeout.



32
33
34
# File 'lib/searchkick.rb', line 32

def timeout
  @timeout
end

.wordnet_pathObject

Returns the value of attribute wordnet_path.



32
33
34
# File 'lib/searchkick.rb', line 32

def wordnet_path
  @wordnet_path
end

Class Method Details

.callbacks(value) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/searchkick.rb', line 81

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

Returns:

  • (Boolean)


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

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

.callbacks_valueObject

private



133
134
135
# File 'lib/searchkick.rb', line 133

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

.callbacks_value=(value) ⇒ Object

private



138
139
140
# File 'lib/searchkick.rb', line 138

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

.clear_queued_itemsObject

private



128
129
130
# File 'lib/searchkick.rb', line 128

def self.clear_queued_items
  Thread.current[:searchkick_queued_items] = []
end

.disable_callbacksObject



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

def self.disable_callbacks
  self.callbacks_value = false
end

.enable_callbacksObject



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

def self.enable_callbacks
  self.callbacks_value = nil
end

.multi_search(queries) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/searchkick.rb', line 152

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_bulkObject

private



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

def self.perform_bulk
  items = queued_items
  clear_queued_items
  perform_items(items)
end

.perform_items(items) ⇒ Object

private



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/searchkick.rb', line 110

def self.perform_items(items)
  if items.any?
    response = client.bulk(body: items)
    if response["errors"]
      first_with_error = response["items"].map do |item|
        (item["index"] || item["delete"])
      end.find { |item| item["error"] }
      raise Searchkick::ImportError, "#{first_with_error["error"]} on item with id '#{first_with_error["_id"]}'"
    end
  end
end

.queue_items(items) ⇒ Object

private



97
98
99
100
# File 'lib/searchkick.rb', line 97

def self.queue_items(items)
  queued_items.concat(items)
  perform_bulk unless callbacks_value == :bulk
end

.queued_itemsObject

private



123
124
125
# File 'lib/searchkick.rb', line 123

def self.queued_items
  Thread.current[:searchkick_queued_items] ||= []
end

.search(term = nil, options = {}, &block) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/searchkick.rb', line 142

def self.search(term = nil, options = {}, &block)
  query = Searchkick::Query.new(nil, term, options)
  block.call(query.body) if block
  if options[:execute] == false
    query
  else
    query.execute
  end
end

.server_below?(version) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/searchkick.rb', line 65

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

.server_versionObject



61
62
63
# File 'lib/searchkick.rb', line 61

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