Commit dca8bd860f6a5aa47f066cd1d4131b5134486296

Authored by Andrew
1 parent 70361fb2

Added note about other aggregation types [skip ci]

Showing 2 changed files with 26 additions and 0 deletions   Show diff stats
README.md
... ... @@ -759,6 +759,12 @@ Date histogram
759 759 Product.search "pear", aggs: {products_per_year: {date_histogram: {field: :created_at, interval: :year}}}
760 760 ```
761 761  
  762 +For other aggregation types, use `body_options`:
  763 +
  764 +```ruby
  765 +Product.search "orange", body_options: {aggs: {price: {histogram: {field: :price, interval: 10}}}
  766 +```
  767 +
762 768 #### Moving From Facets
763 769  
764 770 1. Replace `facets` with `aggs` in searches. **Note:** Stats facets are not supported at this time.
... ...
test/aggs_test.rb
... ... @@ -178,6 +178,26 @@ class AggsTest < Minitest::Test
178 178 assert_equal 66, products.aggs["sum_price"]["value"]
179 179 end
180 180  
  181 + def test_body_options
  182 + products =
  183 + Product.search("*",
  184 + body_options: {
  185 + aggs: {
  186 + price: {
  187 + histogram: {field: :price, interval: 10}
  188 + }
  189 + }
  190 + }
  191 + )
  192 +
  193 + expected = [
  194 + {"key" => 0.0, "doc_count" => 1},
  195 + {"key" => 10.0, "doc_count" => 1},
  196 + {"key" => 20.0, "doc_count" => 2}
  197 + ]
  198 + assert_equal products.aggs["price"]["buckets"], expected
  199 + end
  200 +
181 201 protected
182 202  
183 203 def buckets_as_hash(agg)
... ...