Commit a483ae65181df0db8f211fe817901b1f2128f350

Authored by Andrew Kane
1 parent 1f3ab4df

Do not throw errors when index becomes out of sync with database - for #31

CHANGELOG.md
  1 +## 0.7.5 [unreleased]
  2 +
  3 +- Do not throw errors when index becomes out of sync with database
  4 +
1 5 ## 0.7.4
2 6  
3 7 - Fixed reindex with inheritance
... ...
lib/searchkick/results.rb
... ... @@ -24,13 +24,18 @@ module Searchkick
24 24 if options[:includes]
25 25 records = records.includes(options[:includes])
26 26 end
27   - results[type] = records.find(grouped_hits.map{|hit| hit["_id"] })
  27 + results[type] =
  28 + if records.respond_to?(:primary_key)
  29 + records.where(records.primary_key => grouped_hits.map{|hit| hit["_id"] }).to_a
  30 + else
  31 + records.queryable.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a
  32 + end
28 33 end
29 34  
30 35 # sort
31 36 hits.map do |hit|
32 37 results[hit["_type"]].find{|r| r.id.to_s == hit["_id"].to_s }
33   - end
  38 + end.compact
34 39 else
35 40 hits.map do |hit|
36 41 result = hit.except("_source").merge(hit["_source"])
... ...
test/index_test.rb
... ... @@ -35,6 +35,12 @@ class TestIndex < Minitest::Unit::TestCase
35 35 assert_equal ["Dollar Tree"], Store.search(query: {match: {name: "Dollar Tree"}}).map(&:name)
36 36 end
37 37  
  38 + def test_record_not_found
  39 + store_names ["Product A", "Product B"]
  40 + Product.where(name: "Product A").delete_all
  41 + assert_search "product", ["Product B"]
  42 + end
  43 +
38 44 if defined?(ActiveRecord)
39 45  
40 46 def test_transaction
... ...