Class: Searchkick::Indexer

Inherits:
Object
  • Object
show all
Includes:
IndexerWithInstrumentation
Defined in:
lib/searchkick/indexer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIndexer

Returns a new instance of Indexer.



5
6
7
# File 'lib/searchkick/indexer.rb', line 5

def initialize
  @queued_items = []
end

Instance Attribute Details

#queued_itemsObject (readonly)

Returns the value of attribute queued_items.



3
4
5
# File 'lib/searchkick/indexer.rb', line 3

def queued_items
  @queued_items
end

Instance Method Details

#performObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/searchkick/indexer.rb', line 14

def perform
  items = @queued_items
  @queued_items = []
  if items.any?
    response = Searchkick.client.bulk(body: items)
    if response["errors"]
      first_with_error = response["items"].map do |item|
        (item["index"] || item["delete"] || item["update"])
      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) ⇒ Object



9
10
11
12
# File 'lib/searchkick/indexer.rb', line 9

def queue(items)
  @queued_items.concat(items)
  perform unless Searchkick.callbacks_value == :bulk
end