Commit d2b5ebac477181ee18d42662ebfb89bb022cae9d

Authored by Andrew Kane
1 parent bfe5dd1a

Improved indexer code [skip ci]

Showing 2 changed files with 15 additions and 9 deletions   Show diff stats
lib/searchkick.rb
... ... @@ -285,7 +285,7 @@ module Searchkick
285 285  
286 286 # private
287 287 def self.indexer
288   - Thread.current[:searchkick_indexer] ||= Searchkick::Indexer.new
  288 + Thread.current[:searchkick_indexer] ||= Indexer.new
289 289 end
290 290  
291 291 # private
... ...
lib/searchkick/indexer.rb
  1 +# thread-local (technically fiber-local) indexer
  2 +# used to aggregate bulk callbacks across models
1 3 module Searchkick
2 4 class Indexer
3 5 attr_reader :queued_items
... ... @@ -14,15 +16,19 @@ module Searchkick
14 16 def perform
15 17 items = @queued_items
16 18 @queued_items = []
17   - if items.any?
18   - response = Searchkick.client.bulk(body: items)
19   - if response["errors"]
20   - first_with_error = response["items"].map do |item|
21   - (item["index"] || item["delete"] || item["update"])
22   - end.find { |item| item["error"] }
23   - raise Searchkick::ImportError, "#{first_with_error["error"]} on item with id '#{first_with_error["_id"]}'"
24   - end
  19 +
  20 + return if items.empty?
  21 +
  22 + response = Searchkick.client.bulk(body: items)
  23 + if response["errors"]
  24 + first_with_error = response["items"].map do |item|
  25 + (item["index"] || item["delete"] || item["update"])
  26 + end.find { |item| item["error"] }
  27 + raise ImportError, "#{first_with_error["error"]} on item with id '#{first_with_error["_id"]}'"
25 28 end
  29 +
  30 + # maybe return response in future
  31 + nil
26 32 end
27 33 end
28 34 end
... ...