Commit 1216d68342b4a5e9a268f7d0a68c957f3c12cc15

Authored by Andrew Kane
1 parent fe1cf63b

Changed to missing_records

CHANGELOG.md
1 1 ## 4.4.2 (unreleased)
2 2  
3   -- Added `missing_hits` method to results
  3 +- Added `missing_records` method to results
4 4 - Fixed issue with `like` and special characters
5 5  
6 6 ## 4.4.1 (2020-06-24)
... ...
lib/searchkick/results.rb
... ... @@ -22,15 +22,15 @@ module Searchkick
22 22 # TODO return enumerator like with_score
23 23 def with_hit
24 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 27 end
28   - with_hit_and_missing_hits[0]
  28 + with_hit_and_missing_records[0]
29 29 end
30 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 34 end
35 35  
36 36 def suggestions
... ... @@ -219,15 +219,16 @@ module Searchkick
219 219  
220 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 226 if options[:load]
227 227 # results can have different types
228 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 232 klasses =
232 233 if @klass
233 234 [@klass]
... ... @@ -236,10 +237,11 @@ module Searchkick
236 237 Array((options[:index_mapping] || {})[index_alias])
237 238 end
238 239 raise Searchkick::Error, "Unknown model for index: #{index}" unless klasses.any?
  240 + index_models[index] = klasses
239 241  
240 242 results[index] = {}
241 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 245 end
244 246 end
245 247  
... ... @@ -257,7 +259,16 @@ module Searchkick
257 259 end
258 260 [result, hit]
259 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 272 result
262 273 end
263 274 else
... ... @@ -284,7 +295,7 @@ module Searchkick
284 295 end
285 296 end
286 297  
287   - [results, missing_hits]
  298 + [results, missing_records]
288 299 end
289 300 end
290 301  
... ...
test/search_test.rb
... ... @@ -37,16 +37,17 @@ class SearchTest &lt; Minitest::Test
37 37 assert_equal ["Dollar Tree"], products.map(&:name)
38 38 end
39 39  
40   - def test_missing_hits
  40 + def test_missing_records
41 41 store_names ["Product A", "Product B"]
42 42 product = Product.find_by(name: "Product A")
43 43 product.delete
44 44 assert_output nil, /\[searchkick\] WARNING: Records in search index do not exist in database/ do
45 45 result = Product.search("product")
46 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 49 end
49   - assert_empty Product.search("product", load: false).missing_hits
  50 + assert_empty Product.search("product", load: false).missing_records
50 51 ensure
51 52 Product.reindex
52 53 end
... ...