diff --git a/README.md b/README.md index 7a05728..075f53d 100644 --- a/README.md +++ b/README.md @@ -759,6 +759,12 @@ Date histogram Product.search "pear", aggs: {products_per_year: {date_histogram: {field: :created_at, interval: :year}}} ``` +For other aggregation types, use `body_options`: + +```ruby +Product.search "orange", body_options: {aggs: {price: {histogram: {field: :price, interval: 10}}} +``` + #### Moving From Facets 1. Replace `facets` with `aggs` in searches. **Note:** Stats facets are not supported at this time. diff --git a/test/aggs_test.rb b/test/aggs_test.rb index 9269dcb..8068ff0 100644 --- a/test/aggs_test.rb +++ b/test/aggs_test.rb @@ -178,6 +178,26 @@ class AggsTest < Minitest::Test assert_equal 66, products.aggs["sum_price"]["value"] end + def test_body_options + products = + Product.search("*", + body_options: { + aggs: { + price: { + histogram: {field: :price, interval: 10} + } + } + } + ) + + expected = [ + {"key" => 0.0, "doc_count" => 1}, + {"key" => 10.0, "doc_count" => 1}, + {"key" => 20.0, "doc_count" => 2} + ] + assert_equal products.aggs["price"]["buckets"], expected + end + protected def buckets_as_hash(agg) -- libgit2 0.21.0