Commit 98c9b5ab33ed809382d8ad740981abe221fff43e
1 parent
232b6a60
Exists in
master
and in
21 other branches
Added stats option
Showing
3 changed files
with
9 additions
and
10 deletions
Show diff stats
README.md
... | ... | @@ -443,12 +443,10 @@ price_ranges = [{to: 20}, {from: 20, to: 50}, {from: 50}] |
443 | 443 | Product.search "*", facets: {price: {ranges: price_ranges}} |
444 | 444 | ``` |
445 | 445 | |
446 | - | |
447 | -#### Stats | |
448 | -If stats is enabled the facets returned will in addition to the term and a count contain `max`, `min`, `mean` and `total` fields for the score in each facet. This is very useful if you want to be able to sort facets on relevance rather than count. | |
446 | +Use the `stats` option to get to max, min, mean, and total scores for each facet | |
449 | 447 | |
450 | 448 | ```ruby |
451 | -Product.search "*", facets: {store_id: { stats: true }} | |
449 | +Product.search "*", facets: {store_id: {stats: true}} | |
452 | 450 | ``` |
453 | 451 | |
454 | 452 | ### Highlight | ... | ... |
lib/searchkick/query.rb
... | ... | @@ -219,6 +219,7 @@ module Searchkick |
219 | 219 | facets.each do |field, facet_options| |
220 | 220 | # ask for extra facets due to |
221 | 221 | # https://github.com/elasticsearch/elasticsearch/issues/1305 |
222 | + size = facet_options[:limit] ? facet_options[:limit] + 150 : 100000 | |
222 | 223 | |
223 | 224 | if facet_options[:ranges] |
224 | 225 | payload[:facets][field] = { |
... | ... | @@ -230,15 +231,15 @@ module Searchkick |
230 | 231 | payload[:facets][field] = { |
231 | 232 | terms_stats: { |
232 | 233 | key_field: field, |
233 | - value_script: 'doc.score', | |
234 | - size: facet_options[:limit] ? facet_options[:limit] + 150 : 100000 | |
234 | + value_script: "doc.score", | |
235 | + size: size | |
235 | 236 | } |
236 | 237 | } |
237 | 238 | else |
238 | 239 | payload[:facets][field] = { |
239 | 240 | terms: { |
240 | 241 | field: field, |
241 | - size: facet_options[:limit] ? facet_options[:limit] + 150 : 100000 | |
242 | + size: size | |
242 | 243 | } |
243 | 244 | } |
244 | 245 | end | ... | ... |
test/facets_test.rb
... | ... | @@ -60,9 +60,9 @@ class TestFacets < Minitest::Unit::TestCase |
60 | 60 | end |
61 | 61 | |
62 | 62 | def test_stats_facets |
63 | - options = { where: {store_id: 2}, facets: {store_id: { stats: true }} } | |
64 | - facets = Product.search('Product', options).facets['store_id']["terms"] | |
65 | - expected_facets_keys =['term', 'count', 'total_count', 'min', 'max', 'total', 'mean'] | |
63 | + options = {where: {store_id: 2}, facets: {store_id: {stats: true}}} | |
64 | + facets = Product.search("Product", options).facets["store_id"]["terms"] | |
65 | + expected_facets_keys = %w[term count total_count min max total mean] | |
66 | 66 | assert_equal expected_facets_keys, facets.first.keys |
67 | 67 | end |
68 | 68 | ... | ... |