Commit f78cea520fbef1a53e384e47f81cc75e1d1a2bf4

Authored by Andrew
1 parent b3bb13f2

Warn when records in search index do not exist in database

1 ## 3.0.4 [unreleased] 1 ## 3.0.4 [unreleased]
2 2
3 - Friendlier error message for bad mapping with partial matches 3 - Friendlier error message for bad mapping with partial matches
  4 +- Warn when records in search index do not exist in database
4 5
5 ## 3.0.3 6 ## 3.0.3
6 7
lib/searchkick/results.rb
@@ -27,18 +27,25 @@ module Searchkick @@ -27,18 +27,25 @@ module Searchkick
27 end 27 end
28 28
29 # sort 29 # sort
30 - hits.map do |hit|  
31 - result = results[hit["_type"]][hit["_id"].to_s]  
32 - if result && !(options[:load].is_a?(Hash) && options[:load][:dumpable])  
33 - if hit["highlight"] && !result.respond_to?(:search_highlights)  
34 - highlights = Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, v.first] }]  
35 - result.define_singleton_method(:search_highlights) do  
36 - highlights 30 + results =
  31 + hits.map do |hit|
  32 + result = results[hit["_type"]][hit["_id"].to_s]
  33 + if result && !(options[:load].is_a?(Hash) && options[:load][:dumpable])
  34 + if hit["highlight"] && !result.respond_to?(:search_highlights)
  35 + highlights = Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, v.first] }]
  36 + result.define_singleton_method(:search_highlights) do
  37 + highlights
  38 + end
37 end 39 end
38 end 40 end
39 - end  
40 - result  
41 - end.compact 41 + result
  42 + end.compact
  43 +
  44 + if results.size != hits.size
  45 + warn "[searchkick] WARNING: Records in search index do not exist in database"
  46 + end
  47 +
  48 + results
42 else 49 else
43 hits.map do |hit| 50 hits.map do |hit|
44 result = 51 result =
test/index_test.rb
@@ -85,7 +85,9 @@ class IndexTest < Minitest::Test @@ -85,7 +85,9 @@ class IndexTest < Minitest::Test
85 def test_record_not_found 85 def test_record_not_found
86 store_names ["Product A", "Product B"] 86 store_names ["Product A", "Product B"]
87 Product.where(name: "Product A").delete_all 87 Product.where(name: "Product A").delete_all
88 - assert_search "product", ["Product B"] 88 + assert_output nil, /\[searchkick\] WARNING: Records in search index do not exist in database\n/ do
  89 + assert_search "product", ["Product B"]
  90 + end
89 ensure 91 ensure
90 Product.reindex 92 Product.reindex
91 end 93 end