diff --git a/README.md b/README.md index 5979885..f26afba 100644 --- a/README.md +++ b/README.md @@ -443,12 +443,10 @@ price_ranges = [{to: 20}, {from: 20, to: 50}, {from: 50}] Product.search "*", facets: {price: {ranges: price_ranges}} ``` - -#### Stats -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. +Use the `stats` option to get to max, min, mean, and total scores for each facet ```ruby -Product.search "*", facets: {store_id: { stats: true }} +Product.search "*", facets: {store_id: {stats: true}} ``` ### Highlight diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index 897d1c5..26926c8 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -219,6 +219,7 @@ module Searchkick facets.each do |field, facet_options| # ask for extra facets due to # https://github.com/elasticsearch/elasticsearch/issues/1305 + size = facet_options[:limit] ? facet_options[:limit] + 150 : 100000 if facet_options[:ranges] payload[:facets][field] = { @@ -230,15 +231,15 @@ module Searchkick payload[:facets][field] = { terms_stats: { key_field: field, - value_script: 'doc.score', - size: facet_options[:limit] ? facet_options[:limit] + 150 : 100000 + value_script: "doc.score", + size: size } } else payload[:facets][field] = { terms: { field: field, - size: facet_options[:limit] ? facet_options[:limit] + 150 : 100000 + size: size } } end diff --git a/test/facets_test.rb b/test/facets_test.rb index 23df522..adf5a78 100644 --- a/test/facets_test.rb +++ b/test/facets_test.rb @@ -60,9 +60,9 @@ class TestFacets < Minitest::Unit::TestCase end def test_stats_facets - options = { where: {store_id: 2}, facets: {store_id: { stats: true }} } - facets = Product.search('Product', options).facets['store_id']["terms"] - expected_facets_keys =['term', 'count', 'total_count', 'min', 'max', 'total', 'mean'] + options = {where: {store_id: 2}, facets: {store_id: {stats: true}}} + facets = Product.search("Product", options).facets["store_id"]["terms"] + expected_facets_keys = %w[term count total_count min max total mean] assert_equal expected_facets_keys, facets.first.keys end -- libgit2 0.21.0