Commit 3b17515c0273e2056a87c6048a1aabe24217f245

Authored by Ricardo David Rodríguez
Committed by Andrew Kane
1 parent 250baf40

Add support for script option in aggregations. (#1177)

@@ -792,6 +792,12 @@ Minimum document count @@ -792,6 +792,12 @@ Minimum document count
792 Product.search "apples", aggs: {store_id: {min_doc_count: 2}} 792 Product.search "apples", aggs: {store_id: {min_doc_count: 2}}
793 ``` 793 ```
794 794
  795 +Script support
  796 +
  797 +```ruby
  798 +Product.search "*", aggs: {color: {script: {source: "'Color: ' + _value"}}}
  799 +```
  800 +
795 Date histogram 801 Date histogram
796 802
797 ```ruby 803 ```ruby
lib/searchkick/query.rb
@@ -736,7 +736,7 @@ module Searchkick @@ -736,7 +736,7 @@ module Searchkick
736 aggs = Hash[aggs.map { |f| [f, {}] }] if aggs.is_a?(Array) # convert to more advanced syntax 736 aggs = Hash[aggs.map { |f| [f, {}] }] if aggs.is_a?(Array) # convert to more advanced syntax
737 aggs.each do |field, agg_options| 737 aggs.each do |field, agg_options|
738 size = agg_options[:limit] ? agg_options[:limit] : 1_000 738 size = agg_options[:limit] ? agg_options[:limit] : 1_000
739 - shared_agg_options = agg_options.slice(:order, :min_doc_count) 739 + shared_agg_options = agg_options.slice(:order, :min_doc_count, :script)
740 740
741 if agg_options[:ranges] 741 if agg_options[:ranges]
742 payload[:aggs][field] = { 742 payload[:aggs][field] = {
test/aggs_test.rb
@@ -34,6 +34,12 @@ class AggsTest < Minitest::Test @@ -34,6 +34,12 @@ class AggsTest < Minitest::Test
34 assert_equal ({2 => 2}), store_agg(aggs: {store_id: {min_doc_count: 2}}) 34 assert_equal ({2 => 2}), store_agg(aggs: {store_id: {min_doc_count: 2}})
35 end 35 end
36 36
  37 + def test_script
  38 + source = "'Color: ' + _value"
  39 + agg = Product.search("Product", aggs: {color: {script: {source: source}}}).aggs["color"]
  40 + assert_equal ({"Color: blue" => 1, "Color: green" => 1, "Color: red" => 1}), buckets_as_hash(agg)
  41 + end
  42 +
37 def test_no_aggs 43 def test_no_aggs
38 assert_nil Product.search("*").aggs 44 assert_nil Product.search("*").aggs
39 end 45 end