Commit 3aa30aa53813315e63fd2bd0d3f7856dc2f945e0

Authored by Andrew Kane
1 parent 36cd3d52

Added with_score method to search results

CHANGELOG.md
1 1 ## 4.4.1 (unreleased)
2 2  
3 3 - Added `stem_exclusion` and `stemmer_override` options
  4 +- Added `with_score` method to search results
4 5 - Improved error message for `reload_synonyms` with non-OSS version of Elasticsearch
5 6 - Improved output for reindex rake task
6 7  
... ...
lib/searchkick/results.rb
... ... @@ -211,12 +211,21 @@ module Searchkick
211 211 end
212 212 end
213 213  
  214 + # TODO return enumerator like with_score
214 215 def with_highlights(multiple: false)
215 216 with_hit.map do |result, hit|
216 217 [result, hit_highlights(hit, multiple: multiple)]
217 218 end
218 219 end
219 220  
  221 + def with_score
  222 + return enum_for(:with_score) unless block_given?
  223 +
  224 + with_hit.each do |result, hit|
  225 + yield result, hit["_score"]
  226 + end
  227 + end
  228 +
220 229 def misspellings?
221 230 @options[:misspellings]
222 231 end
... ...
test/results_test.rb 0 → 100644
... ... @@ -0,0 +1,17 @@
  1 +require_relative "test_helper"
  2 +
  3 +class ResultsTest < Minitest::Test
  4 + def test_with_score
  5 + store_names ["Product A", "Product B"]
  6 + results = Product.search("product")
  7 + assert_kind_of Enumerator, results.with_score
  8 + assert_equal 2, results.with_score.to_a.size
  9 + count = 0
  10 + results.with_score do |product, score|
  11 + assert_kind_of Product, product
  12 + assert_kind_of Numeric, score
  13 + count += 1
  14 + end
  15 + assert_equal 2, count
  16 + end
  17 +end
... ...