From f78cea520fbef1a53e384e47f81cc75e1d1a2bf4 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 23 Apr 2018 18:58:30 -0700 Subject: [PATCH] Warn when records in search index do not exist in database --- CHANGELOG.md | 1 + lib/searchkick/results.rb | 27 +++++++++++++++++---------- test/index_test.rb | 4 +++- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2baedbd..9c9b014 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 3.0.4 [unreleased] - Friendlier error message for bad mapping with partial matches +- Warn when records in search index do not exist in database ## 3.0.3 diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index 9eb668c..8ef1147 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -27,18 +27,25 @@ module Searchkick end # sort - hits.map do |hit| - result = results[hit["_type"]][hit["_id"].to_s] - if result && !(options[:load].is_a?(Hash) && options[:load][:dumpable]) - if hit["highlight"] && !result.respond_to?(:search_highlights) - highlights = Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, v.first] }] - result.define_singleton_method(:search_highlights) do - highlights + results = + hits.map do |hit| + result = results[hit["_type"]][hit["_id"].to_s] + if result && !(options[:load].is_a?(Hash) && options[:load][:dumpable]) + if hit["highlight"] && !result.respond_to?(:search_highlights) + highlights = Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, v.first] }] + result.define_singleton_method(:search_highlights) do + highlights + end end end - end - result - end.compact + result + end.compact + + if results.size != hits.size + warn "[searchkick] WARNING: Records in search index do not exist in database" + end + + results else hits.map do |hit| result = diff --git a/test/index_test.rb b/test/index_test.rb index f6de110..73f61e9 100644 --- a/test/index_test.rb +++ b/test/index_test.rb @@ -85,7 +85,9 @@ class IndexTest < Minitest::Test def test_record_not_found store_names ["Product A", "Product B"] Product.where(name: "Product A").delete_all - assert_search "product", ["Product B"] + assert_output nil, /\[searchkick\] WARNING: Records in search index do not exist in database\n/ do + assert_search "product", ["Product B"] + end ensure Product.reindex end -- libgit2 0.21.0