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
1 | ## 4.4.1 (unreleased) | 1 | ## 4.4.1 (unreleased) |
2 | 2 | ||
3 | - Added `stem_exclusion` and `stemmer_override` options | 3 | - Added `stem_exclusion` and `stemmer_override` options |
4 | +- Added `with_score` method to search results | ||
4 | - Improved error message for `reload_synonyms` with non-OSS version of Elasticsearch | 5 | - Improved error message for `reload_synonyms` with non-OSS version of Elasticsearch |
5 | - Improved output for reindex rake task | 6 | - Improved output for reindex rake task |
6 | 7 |
lib/searchkick/results.rb
@@ -211,12 +211,21 @@ module Searchkick | @@ -211,12 +211,21 @@ module Searchkick | ||
211 | end | 211 | end |
212 | end | 212 | end |
213 | 213 | ||
214 | + # TODO return enumerator like with_score | ||
214 | def with_highlights(multiple: false) | 215 | def with_highlights(multiple: false) |
215 | with_hit.map do |result, hit| | 216 | with_hit.map do |result, hit| |
216 | [result, hit_highlights(hit, multiple: multiple)] | 217 | [result, hit_highlights(hit, multiple: multiple)] |
217 | end | 218 | end |
218 | end | 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 | def misspellings? | 229 | def misspellings? |
221 | @options[:misspellings] | 230 | @options[:misspellings] |
222 | end | 231 | end |
@@ -0,0 +1,17 @@ | @@ -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 |