Commit d2b5ebac477181ee18d42662ebfb89bb022cae9d
1 parent
bfe5dd1a
Exists in
master
and in
2 other branches
Improved indexer code [skip ci]
Showing
2 changed files
with
15 additions
and
9 deletions
Show diff stats
lib/searchkick.rb
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 | ... | ... |