diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index da44598..56a00ed 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -19,13 +19,11 @@ module Searchkick @results ||= with_hit.map(&:first) end - # TODO return enumerator like with_score def with_hit - @with_hit ||= begin - if missing_records.any? - Searchkick.warn("Records in search index do not exist in database: #{missing_records.map { |v| v[:id] }.join(", ")}") - end - with_hit_and_missing_records[0] + return enum_for(:with_hit) unless block_given? + + build_hits.each do |result| + yield result end end @@ -157,10 +155,11 @@ module Searchkick end end - # TODO return enumerator like with_score def with_highlights(multiple: false) - with_hit.map do |result, hit| - [result, hit_highlights(hit, multiple: multiple)] + return enum_for(:with_highlights, multiple: multiple) unless block_given? + + with_hit.each do |result, hit| + yield result, hit_highlights(hit, multiple: multiple) end end @@ -302,6 +301,15 @@ module Searchkick end end + def build_hits + @build_hits ||= begin + if missing_records.any? + Searchkick.warn("Records in search index do not exist in database: #{missing_records.map { |v| v[:id] }.join(", ")}") + end + with_hit_and_missing_records[0] + end + end + def results_query(records, hits) ids = hits.map { |hit| hit["_id"] } if options[:includes] || options[:model_includes] diff --git a/test/results_test.rb b/test/results_test.rb index c6f3881..7e7044b 100644 --- a/test/results_test.rb +++ b/test/results_test.rb @@ -1,6 +1,20 @@ require_relative "test_helper" class ResultsTest < Minitest::Test + def test_with_hit + store_names ["Product A", "Product B"] + results = Product.search("product") + assert_kind_of Enumerator, results.with_hit + assert_equal 2, results.with_hit.to_a.size + count = 0 + results.with_hit do |product, hit| + assert_kind_of Product, product + assert_kind_of Hash, hit + count += 1 + end + assert_equal 2, count + end + def test_with_score store_names ["Product A", "Product B"] results = Product.search("product") -- libgit2 0.21.0