From 1216d68342b4a5e9a268f7d0a68c957f3c12cc15 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Fri, 20 Nov 2020 15:37:24 -0800 Subject: [PATCH] Changed to missing_records --- CHANGELOG.md | 2 +- lib/searchkick/results.rb | 35 +++++++++++++++++++++++------------ test/search_test.rb | 7 ++++--- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9d3bb8..e237e27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## 4.4.2 (unreleased) -- Added `missing_hits` method to results +- Added `missing_records` method to results - Fixed issue with `like` and special characters ## 4.4.1 (2020-06-24) diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index 9cdd5db..ac880a3 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -22,15 +22,15 @@ module Searchkick # TODO return enumerator like with_score def with_hit @with_hit ||= begin - if missing_hits.any? - Searchkick.warn("Records in search index do not exist in database: #{missing_hits.map { |v| v["_id"] }.join(", ")}") + if missing_records.any? + Searchkick.warn("Records in search index do not exist in database: #{missing_records.map { |v| v[:id] }.join(", ")}") end - with_hit_and_missing_hits[0] + with_hit_and_missing_records[0] end end - def missing_hits - @missing_hits ||= with_hit_and_missing_hits[1] + def missing_records + @missing_records ||= with_hit_and_missing_records[1] end def suggestions @@ -219,15 +219,16 @@ module Searchkick private - def with_hit_and_missing_hits - @with_hit_and_missing_hits ||= begin - missing_hits = [] + def with_hit_and_missing_records + @with_hit_and_missing_records ||= begin + missing_records = [] if options[:load] # results can have different types results = {} - hits.group_by { |hit, _| hit["_index"] }.each do |index, grouped_hits| + index_models = {} + hits.group_by { |hit, _| hit["_index"] }.each do |index, index_hits| klasses = if @klass [@klass] @@ -236,10 +237,11 @@ module Searchkick Array((options[:index_mapping] || {})[index_alias]) end raise Searchkick::Error, "Unknown model for index: #{index}" unless klasses.any? + index_models[index] = klasses results[index] = {} klasses.each do |klass| - results[index].merge!(results_query(klass, grouped_hits).to_a.index_by { |r| r.id.to_s }) + results[index].merge!(results_query(klass, index_hits).to_a.index_by { |r| r.id.to_s }) end end @@ -257,7 +259,16 @@ module Searchkick end [result, hit] end.select do |result, hit| - missing_hits << hit unless result + unless result + models = index_models[hit["_index"]] + missing_records << { + id: hit["_id"], + # may be multiple models for inheritance with child models + # not ideal to return different types + # but this situation shouldn't be common + model: models.size == 1 ? models.first : models + } + end result end else @@ -284,7 +295,7 @@ module Searchkick end end - [results, missing_hits] + [results, missing_records] end end diff --git a/test/search_test.rb b/test/search_test.rb index e3019de..a10633e 100644 --- a/test/search_test.rb +++ b/test/search_test.rb @@ -37,16 +37,17 @@ class SearchTest < Minitest::Test assert_equal ["Dollar Tree"], products.map(&:name) end - def test_missing_hits + def test_missing_records store_names ["Product A", "Product B"] product = Product.find_by(name: "Product A") product.delete assert_output nil, /\[searchkick\] WARNING: Records in search index do not exist in database/ do result = Product.search("product") assert_equal ["Product B"], result.map(&:name) - assert_equal [product.id.to_s], result.missing_hits.map { |v| v["_id"] } + assert_equal [product.id.to_s], result.missing_records.map { |v| v[:id] } + assert_equal [Product], result.missing_records.map { |v| v[:model] } end - assert_empty Product.search("product", load: false).missing_hits + assert_empty Product.search("product", load: false).missing_records ensure Product.reindex end -- libgit2 0.21.0