diff --git a/CHANGELOG.md b/CHANGELOG.md index 2853af1..9afa33b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Throw `Searchkick::ImportError` for errors when importing records - Errors now inherit from `Searchkick::Error` +- Added `order` option to aggs - Added `mapping` method ## 1.0.1 diff --git a/README.md b/README.md index 9c93cbd..df42d87 100644 --- a/README.md +++ b/README.md @@ -594,6 +594,14 @@ Limit Product.search "apples", aggs: {store_id: {limit: 10}} ``` +Order [master] + +```ruby +Product.search "wingtips", aggs: {color: {order: {"_term" => "asc"}}} # alphabetically +``` + +[All of these options are supported](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order) + Ranges ```ruby diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index 694f46c..891e89e 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -356,21 +356,24 @@ module Searchkick payload[:aggs][field] = { range: { field: agg_options[:field] || field, - ranges: agg_options[:ranges] + ranges: agg_options[:ranges], + order: agg_options[:order] } } elsif agg_options[:date_ranges] payload[:aggs][field] = { date_range: { field: agg_options[:field] || field, - ranges: agg_options[:date_ranges] + ranges: agg_options[:date_ranges], + order: agg_options[:order] } } else payload[:aggs][field] = { terms: { field: agg_options[:field] || field, - size: size + size: size, + order: agg_options[:order] } } end diff --git a/test/aggs_test.rb b/test/aggs_test.rb index 0553169..cf36efc 100644 --- a/test/aggs_test.rb +++ b/test/aggs_test.rb @@ -19,6 +19,11 @@ class AggsTest < Minitest::Test assert_equal ({1 => 1}), store_agg(aggs: {store_id: {where: {in_stock: true}}}) end + def test_order + agg = Product.search("Product", aggs: {color: {order: {"_term" => "desc"}}}).aggs["color"] + assert_equal %w[red green blue], agg["buckets"].map { |b| b["key"] } + end + def test_field assert_equal ({1 => 1, 2 => 2}), store_agg(aggs: {store_id: {}}) assert_equal ({1 => 1, 2 => 2}), store_agg(aggs: {store_id: {field: "store_id"}}) -- libgit2 0.21.0