From a483ae65181df0db8f211fe817901b1f2128f350 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sat, 10 May 2014 13:58:03 -0700 Subject: [PATCH] Do not throw errors when index becomes out of sync with database - for #31 --- CHANGELOG.md | 4 ++++ lib/searchkick/results.rb | 9 +++++++-- test/index_test.rb | 6 ++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b741284..4fdfda2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.7.5 [unreleased] + +- Do not throw errors when index becomes out of sync with database + ## 0.7.4 - Fixed reindex with inheritance diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index d577de8..698ee36 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -24,13 +24,18 @@ module Searchkick if options[:includes] records = records.includes(options[:includes]) end - results[type] = records.find(grouped_hits.map{|hit| hit["_id"] }) + results[type] = + if records.respond_to?(:primary_key) + records.where(records.primary_key => grouped_hits.map{|hit| hit["_id"] }).to_a + else + records.queryable.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a + end end # sort hits.map do |hit| results[hit["_type"]].find{|r| r.id.to_s == hit["_id"].to_s } - end + end.compact else hits.map do |hit| result = hit.except("_source").merge(hit["_source"]) diff --git a/test/index_test.rb b/test/index_test.rb index a7c48c5..33b9c08 100644 --- a/test/index_test.rb +++ b/test/index_test.rb @@ -35,6 +35,12 @@ class TestIndex < Minitest::Unit::TestCase assert_equal ["Dollar Tree"], Store.search(query: {match: {name: "Dollar Tree"}}).map(&:name) end + def test_record_not_found + store_names ["Product A", "Product B"] + Product.where(name: "Product A").delete_all + assert_search "product", ["Product B"] + end + if defined?(ActiveRecord) def test_transaction -- libgit2 0.21.0