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

Class Method Summary collapse

Class Attribute Details

.clientObject



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

.envObject



49
50
51
# File 'lib/searchkick.rb', line 49

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

.modelsObject

Returns the value of attribute models.



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

def models
  @models
end

.search_method_nameObject

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_timeoutObject



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

def self.search_timeout
  @search_timeout || timeout
end

.timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

.wordnet_pathObject

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

Returns:

  • (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_valueObject

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_itemsObject

private



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

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

.disable_callbacksObject



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

def self.disable_callbacks
  self.callbacks_value = false
end

.enable_callbacksObject



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_bulkObject

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_itemsObject

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, 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)


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_versionObject



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

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