Commit 3aa30aa53813315e63fd2bd0d3f7856dc2f945e0
1 parent
36cd3d52
Exists in
master
and in
5 other branches
Added with_score method to search results
Showing
3 changed files
with
27 additions
and
0 deletions
Show diff stats
CHANGELOG.md
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 | ... | ... |
... | ... | @@ -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 | ... | ... |