Commit 89502e6ba5fdea27fc5d0b7597ba150acb65501f
1 parent
5de399d4
Exists in
master
and in
5 other branches
Added missing_ids method to results - #1452
Showing
3 changed files
with
18 additions
and
8 deletions
Show diff stats
CHANGELOG.md
lib/searchkick/results.rb
@@ -22,6 +22,8 @@ module Searchkick | @@ -22,6 +22,8 @@ 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 | + @missing_ids = [] | ||
26 | + | ||
25 | if options[:load] | 27 | if options[:load] |
26 | # results can have different types | 28 | # results can have different types |
27 | results = {} | 29 | results = {} |
@@ -42,8 +44,6 @@ module Searchkick | @@ -42,8 +44,6 @@ module Searchkick | ||
42 | end | 44 | end |
43 | end | 45 | end |
44 | 46 | ||
45 | - missing_ids = [] | ||
46 | - | ||
47 | # sort | 47 | # sort |
48 | results = | 48 | results = |
49 | hits.map do |hit| | 49 | hits.map do |hit| |
@@ -58,12 +58,12 @@ module Searchkick | @@ -58,12 +58,12 @@ module Searchkick | ||
58 | end | 58 | end |
59 | [result, hit] | 59 | [result, hit] |
60 | end.select do |result, hit| | 60 | end.select do |result, hit| |
61 | - missing_ids << hit["_id"] unless result | 61 | + @missing_ids << hit["_id"] unless result |
62 | result | 62 | result |
63 | end | 63 | end |
64 | 64 | ||
65 | - if missing_ids.any? | ||
66 | - Searchkick.warn("Records in search index do not exist in database: #{missing_ids.join(", ")}") | 65 | + if @missing_ids.any? |
66 | + Searchkick.warn("Records in search index do not exist in database: #{@missing_ids.join(", ")}") | ||
67 | end | 67 | end |
68 | 68 | ||
69 | results | 69 | results |
@@ -92,6 +92,11 @@ module Searchkick | @@ -92,6 +92,11 @@ module Searchkick | ||
92 | end | 92 | end |
93 | end | 93 | end |
94 | 94 | ||
95 | + def missing_ids | ||
96 | + with_hit | ||
97 | + @missing_ids | ||
98 | + end | ||
99 | + | ||
95 | def suggestions | 100 | def suggestions |
96 | if response["suggest"] | 101 | if response["suggest"] |
97 | response["suggest"].values.flat_map { |v| v.first["options"] }.sort_by { |o| -o["score"] }.map { |o| o["text"] }.uniq | 102 | response["suggest"].values.flat_map { |v| v.first["options"] }.sort_by { |o| -o["score"] }.map { |o| o["text"] }.uniq |
test/search_test.rb
@@ -37,12 +37,16 @@ class SearchTest < Minitest::Test | @@ -37,12 +37,16 @@ class SearchTest < 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_record_not_found | 40 | + def test_missing_ids |
41 | store_names ["Product A", "Product B"] | 41 | store_names ["Product A", "Product B"] |
42 | - Product.where(name: "Product A").delete_all | 42 | + product = Product.find_by(name: "Product A") |
43 | + product.delete | ||
43 | 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 |
44 | - assert_search "product", ["Product B"] | 45 | + result = Product.search("product") |
46 | + assert_equal ["Product B"], result.map(&:name) | ||
47 | + assert_equal [product.id.to_s], result.missing_ids | ||
45 | end | 48 | end |
49 | + assert_equal [], Product.search("product", load: false).missing_ids | ||
46 | ensure | 50 | ensure |
47 | Product.reindex | 51 | Product.reindex |
48 | end | 52 | end |