Commit fee4f5319c388bdeefe411515bd8a72761d28b81
1 parent
aaf47c9e
Exists in
master
and in
21 other branches
Added order option to aggs - closes #426
Showing
4 changed files
with
20 additions
and
3 deletions
Show diff stats
CHANGELOG.md
README.md
... | ... | @@ -594,6 +594,14 @@ Limit |
594 | 594 | Product.search "apples", aggs: {store_id: {limit: 10}} |
595 | 595 | ``` |
596 | 596 | |
597 | +Order [master] | |
598 | + | |
599 | +```ruby | |
600 | +Product.search "wingtips", aggs: {color: {order: {"_term" => "asc"}}} # alphabetically | |
601 | +``` | |
602 | + | |
603 | +[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) | |
604 | + | |
597 | 605 | Ranges |
598 | 606 | |
599 | 607 | ```ruby | ... | ... |
lib/searchkick/query.rb
... | ... | @@ -356,21 +356,24 @@ module Searchkick |
356 | 356 | payload[:aggs][field] = { |
357 | 357 | range: { |
358 | 358 | field: agg_options[:field] || field, |
359 | - ranges: agg_options[:ranges] | |
359 | + ranges: agg_options[:ranges], | |
360 | + order: agg_options[:order] | |
360 | 361 | } |
361 | 362 | } |
362 | 363 | elsif agg_options[:date_ranges] |
363 | 364 | payload[:aggs][field] = { |
364 | 365 | date_range: { |
365 | 366 | field: agg_options[:field] || field, |
366 | - ranges: agg_options[:date_ranges] | |
367 | + ranges: agg_options[:date_ranges], | |
368 | + order: agg_options[:order] | |
367 | 369 | } |
368 | 370 | } |
369 | 371 | else |
370 | 372 | payload[:aggs][field] = { |
371 | 373 | terms: { |
372 | 374 | field: agg_options[:field] || field, |
373 | - size: size | |
375 | + size: size, | |
376 | + order: agg_options[:order] | |
374 | 377 | } |
375 | 378 | } |
376 | 379 | end | ... | ... |
test/aggs_test.rb
... | ... | @@ -19,6 +19,11 @@ class AggsTest < Minitest::Test |
19 | 19 | assert_equal ({1 => 1}), store_agg(aggs: {store_id: {where: {in_stock: true}}}) |
20 | 20 | end |
21 | 21 | |
22 | + def test_order | |
23 | + agg = Product.search("Product", aggs: {color: {order: {"_term" => "desc"}}}).aggs["color"] | |
24 | + assert_equal %w[red green blue], agg["buckets"].map { |b| b["key"] } | |
25 | + end | |
26 | + | |
22 | 27 | def test_field |
23 | 28 | assert_equal ({1 => 1, 2 => 2}), store_agg(aggs: {store_id: {}}) |
24 | 29 | assert_equal ({1 => 1, 2 => 2}), store_agg(aggs: {store_id: {field: "store_id"}}) | ... | ... |