diff --git a/README.md b/README.md index ac929ab..c29da58 100644 --- a/README.md +++ b/README.md @@ -578,9 +578,16 @@ Limit Product.search "apples", aggs: {store_id: {limit: 10}} ``` +Ranges + +```ruby +price_ranges = [{to: 20}, {from: 20, to: 50}, {from: 50}] +Product.search "*", aggs: {price: {ranges: price_ranges}} +``` + #### Moving From Facets -1. Replace `facets` with `aggs` in searches. **Note:** Range and stats facets are not supported at this time. +1. Replace `facets` with `aggs` in searches. **Note:** Stats facets are not supported at this time. ```ruby products = Product.search "chuck taylor", facets: [:brand] @@ -627,6 +634,14 @@ Product.search "apples", aggs: {store_id: {limit: 10}} 3. By default, `where` conditions apply to aggregations. This is equivalent to `smart_facets: true`. If you have `smart_facets: true`, you can remove it. If this is not desired, set `smart_aggs: false`. +4. If you have any range facets with dates, change the key from `ranges` to `date_ranges`. + + ```ruby + facets: {date_field: {ranges: date_ranges}} + # to + aggs: {date_field: {date_ranges: date_ranges}} + ``` + ### Facets [deprecated] Facets have been deprecated in favor of aggregations as of Searchkick 0.9.2. See [how to upgrade](#moving-from-facets). diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index 7d43b82..697cf9e 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -344,12 +344,28 @@ module Searchkick aggs.each do |field, agg_options| size = agg_options[:limit] ? agg_options[:limit] : 100_000 - payload[:aggs][field] = { - terms: { - field: agg_options[:field] || field, - size: size + if agg_options[:ranges] + payload[:aggs][field] = { + range: { + field: agg_options[:field] || field, + ranges: agg_options[:ranges] + } } - } + elsif agg_options[:date_ranges] + payload[:aggs][field] = { + date_range: { + field: agg_options[:field] || field, + ranges: agg_options[:date_ranges] + } + } + else + payload[:aggs][field] = { + terms: { + field: agg_options[:field] || field, + size: size + } + } + end where = {} where = (options[:where] || {}).reject { |k| k == field } unless options[:smart_aggs] == false diff --git a/test/aggs_test.rb b/test/aggs_test.rb index d45f4bc..48ec3e8 100644 --- a/test/aggs_test.rb +++ b/test/aggs_test.rb @@ -32,6 +32,27 @@ class AggsTest < Minitest::Test assert_equal(1, agg["sum_other_doc_count"]) if Gem::Version.new(Searchkick.server_version) >= Gem::Version.new("1.4.0") end + def test_ranges + price_ranges = [{to: 10}, {from: 10, to: 20}, {from: 20}] + agg = Product.search("Product", aggs: {price: {ranges: price_ranges}}).aggs["price"] + + assert_equal 3, agg["buckets"].size + assert_equal 10.0, agg["buckets"][0]["to"] + assert_equal 20.0, agg["buckets"][2]["from"] + assert_equal 1, agg["buckets"][0]["doc_count"] + assert_equal 0, agg["buckets"][1]["doc_count"] + assert_equal 2, agg["buckets"][2]["doc_count"] + end + + def test_date_ranges + ranges = [{to: 1.day.ago}, {from: 1.day.ago, to: 1.day.from_now}, {from: 1.day.from_now}] + agg = Product.search("Product", aggs: {created_at: {date_ranges: ranges}}).aggs["created_at"] + + assert_equal 1, agg["buckets"][0]["doc_count"] + assert_equal 1, agg["buckets"][1]["doc_count"] + assert_equal 1, agg["buckets"][2]["doc_count"] + end + def test_query_where assert_equal ({1 => 1}), store_agg(where: {in_stock: true}, aggs: [:store_id]) end -- libgit2 0.21.0