Commit 1216d68342b4a5e9a268f7d0a68c957f3c12cc15

Authored by Andrew Kane
1 parent fe1cf63b

Changed to missing_records

1 ## 4.4.2 (unreleased) 1 ## 4.4.2 (unreleased)
2 2
3 -- Added `missing_hits` method to results 3 +- Added `missing_records` method to results
4 - Fixed issue with `like` and special characters 4 - Fixed issue with `like` and special characters
5 5
6 ## 4.4.1 (2020-06-24) 6 ## 4.4.1 (2020-06-24)
lib/searchkick/results.rb
@@ -22,15 +22,15 @@ module Searchkick @@ -22,15 +22,15 @@ module Searchkick
22 # TODO return enumerator like with_score 22 # TODO return enumerator like with_score
23 def with_hit 23 def with_hit
24 @with_hit ||= begin 24 @with_hit ||= begin
25 - if missing_hits.any?  
26 - Searchkick.warn("Records in search index do not exist in database: #{missing_hits.map { |v| v["_id"] }.join(", ")}") 25 + if missing_records.any?
  26 + Searchkick.warn("Records in search index do not exist in database: #{missing_records.map { |v| v[:id] }.join(", ")}")
27 end 27 end
28 - with_hit_and_missing_hits[0] 28 + with_hit_and_missing_records[0]
29 end 29 end
30 end 30 end
31 31
32 - def missing_hits  
33 - @missing_hits ||= with_hit_and_missing_hits[1] 32 + def missing_records
  33 + @missing_records ||= with_hit_and_missing_records[1]
34 end 34 end
35 35
36 def suggestions 36 def suggestions
@@ -219,15 +219,16 @@ module Searchkick @@ -219,15 +219,16 @@ module Searchkick
219 219
220 private 220 private
221 221
222 - def with_hit_and_missing_hits  
223 - @with_hit_and_missing_hits ||= begin  
224 - missing_hits = [] 222 + def with_hit_and_missing_records
  223 + @with_hit_and_missing_records ||= begin
  224 + missing_records = []
225 225
226 if options[:load] 226 if options[:load]
227 # results can have different types 227 # results can have different types
228 results = {} 228 results = {}
229 229
230 - hits.group_by { |hit, _| hit["_index"] }.each do |index, grouped_hits| 230 + index_models = {}
  231 + hits.group_by { |hit, _| hit["_index"] }.each do |index, index_hits|
231 klasses = 232 klasses =
232 if @klass 233 if @klass
233 [@klass] 234 [@klass]
@@ -236,10 +237,11 @@ module Searchkick @@ -236,10 +237,11 @@ module Searchkick
236 Array((options[:index_mapping] || {})[index_alias]) 237 Array((options[:index_mapping] || {})[index_alias])
237 end 238 end
238 raise Searchkick::Error, "Unknown model for index: #{index}" unless klasses.any? 239 raise Searchkick::Error, "Unknown model for index: #{index}" unless klasses.any?
  240 + index_models[index] = klasses
239 241
240 results[index] = {} 242 results[index] = {}
241 klasses.each do |klass| 243 klasses.each do |klass|
242 - results[index].merge!(results_query(klass, grouped_hits).to_a.index_by { |r| r.id.to_s }) 244 + results[index].merge!(results_query(klass, index_hits).to_a.index_by { |r| r.id.to_s })
243 end 245 end
244 end 246 end
245 247
@@ -257,7 +259,16 @@ module Searchkick @@ -257,7 +259,16 @@ module Searchkick
257 end 259 end
258 [result, hit] 260 [result, hit]
259 end.select do |result, hit| 261 end.select do |result, hit|
260 - missing_hits << hit unless result 262 + unless result
  263 + models = index_models[hit["_index"]]
  264 + missing_records << {
  265 + id: hit["_id"],
  266 + # may be multiple models for inheritance with child models
  267 + # not ideal to return different types
  268 + # but this situation shouldn't be common
  269 + model: models.size == 1 ? models.first : models
  270 + }
  271 + end
261 result 272 result
262 end 273 end
263 else 274 else
@@ -284,7 +295,7 @@ module Searchkick @@ -284,7 +295,7 @@ module Searchkick
284 end 295 end
285 end 296 end
286 297
287 - [results, missing_hits] 298 + [results, missing_records]
288 end 299 end
289 end 300 end
290 301
test/search_test.rb
@@ -37,16 +37,17 @@ class SearchTest &lt; Minitest::Test @@ -37,16 +37,17 @@ class SearchTest &lt; Minitest::Test
37 assert_equal ["Dollar Tree"], products.map(&:name) 37 assert_equal ["Dollar Tree"], products.map(&:name)
38 end 38 end
39 39
40 - def test_missing_hits 40 + def test_missing_records
41 store_names ["Product A", "Product B"] 41 store_names ["Product A", "Product B"]
42 product = Product.find_by(name: "Product A") 42 product = Product.find_by(name: "Product A")
43 product.delete 43 product.delete
44 assert_output nil, /\[searchkick\] WARNING: Records in search index do not exist in database/ do 44 assert_output nil, /\[searchkick\] WARNING: Records in search index do not exist in database/ do
45 result = Product.search("product") 45 result = Product.search("product")
46 assert_equal ["Product B"], result.map(&:name) 46 assert_equal ["Product B"], result.map(&:name)
47 - assert_equal [product.id.to_s], result.missing_hits.map { |v| v["_id"] } 47 + assert_equal [product.id.to_s], result.missing_records.map { |v| v[:id] }
  48 + assert_equal [Product], result.missing_records.map { |v| v[:model] }
48 end 49 end
49 - assert_empty Product.search("product", load: false).missing_hits 50 + assert_empty Product.search("product", load: false).missing_records
50 ensure 51 ensure
51 Product.reindex 52 Product.reindex
52 end 53 end