Commit 2d3e33eb4cec226d66b4387dbf916f7d333bd477
1 parent
4f86c0c8
Exists in
master
and in
2 other branches
Return enumerator for with_hit and with_highlights if no block given [skip ci]
Showing
2 changed files
with
31 additions
and
9 deletions
Show diff stats
lib/searchkick/results.rb
... | ... | @@ -19,13 +19,11 @@ module Searchkick |
19 | 19 | @results ||= with_hit.map(&:first) |
20 | 20 | end |
21 | 21 | |
22 | - # TODO return enumerator like with_score | |
23 | 22 | def with_hit |
24 | - @with_hit ||= begin | |
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 | |
28 | - with_hit_and_missing_records[0] | |
23 | + return enum_for(:with_hit) unless block_given? | |
24 | + | |
25 | + build_hits.each do |result| | |
26 | + yield result | |
29 | 27 | end |
30 | 28 | end |
31 | 29 | |
... | ... | @@ -157,10 +155,11 @@ module Searchkick |
157 | 155 | end |
158 | 156 | end |
159 | 157 | |
160 | - # TODO return enumerator like with_score | |
161 | 158 | def with_highlights(multiple: false) |
162 | - with_hit.map do |result, hit| | |
163 | - [result, hit_highlights(hit, multiple: multiple)] | |
159 | + return enum_for(:with_highlights, multiple: multiple) unless block_given? | |
160 | + | |
161 | + with_hit.each do |result, hit| | |
162 | + yield result, hit_highlights(hit, multiple: multiple) | |
164 | 163 | end |
165 | 164 | end |
166 | 165 | |
... | ... | @@ -302,6 +301,15 @@ module Searchkick |
302 | 301 | end |
303 | 302 | end |
304 | 303 | |
304 | + def build_hits | |
305 | + @build_hits ||= begin | |
306 | + if missing_records.any? | |
307 | + Searchkick.warn("Records in search index do not exist in database: #{missing_records.map { |v| v[:id] }.join(", ")}") | |
308 | + end | |
309 | + with_hit_and_missing_records[0] | |
310 | + end | |
311 | + end | |
312 | + | |
305 | 313 | def results_query(records, hits) |
306 | 314 | ids = hits.map { |hit| hit["_id"] } |
307 | 315 | if options[:includes] || options[:model_includes] | ... | ... |
test/results_test.rb
1 | 1 | require_relative "test_helper" |
2 | 2 | |
3 | 3 | class ResultsTest < Minitest::Test |
4 | + def test_with_hit | |
5 | + store_names ["Product A", "Product B"] | |
6 | + results = Product.search("product") | |
7 | + assert_kind_of Enumerator, results.with_hit | |
8 | + assert_equal 2, results.with_hit.to_a.size | |
9 | + count = 0 | |
10 | + results.with_hit do |product, hit| | |
11 | + assert_kind_of Product, product | |
12 | + assert_kind_of Hash, hit | |
13 | + count += 1 | |
14 | + end | |
15 | + assert_equal 2, count | |
16 | + end | |
17 | + | |
4 | 18 | def test_with_score |
5 | 19 | store_names ["Product A", "Product B"] |
6 | 20 | results = Product.search("product") | ... | ... |