Commit 684580b03d193dcac1223975aa6a51324dc437f3
1 parent
8fa1bd56
Exists in
relation
and in
1 other branch
Build aggs relation
Showing
2 changed files
with
18 additions
and
1 deletions
Show diff stats
lib/searchkick/relation.rb
@@ -232,7 +232,7 @@ module Searchkick | @@ -232,7 +232,7 @@ module Searchkick | ||
232 | 232 | ||
233 | # TODO merge options | 233 | # TODO merge options |
234 | def aggs!(*args) | 234 | def aggs!(*args) |
235 | - options[:aggs] = args.size == 1 ? args.first : args | 235 | + options[:aggs] = hash_args(options[:aggs], args) |
236 | self | 236 | self |
237 | end | 237 | end |
238 | 238 | ||
@@ -245,6 +245,18 @@ module Searchkick | @@ -245,6 +245,18 @@ module Searchkick | ||
245 | 245 | ||
246 | private | 246 | private |
247 | 247 | ||
248 | + def hash_args(old, new) | ||
249 | + old ||= {} | ||
250 | + new.each do |v| | ||
251 | + if v.is_a?(Hash) | ||
252 | + old.merge!(v) | ||
253 | + else | ||
254 | + old[v] = {} | ||
255 | + end | ||
256 | + end | ||
257 | + old | ||
258 | + end | ||
259 | + | ||
248 | def sanitize_opts(attributes) | 260 | def sanitize_opts(attributes) |
249 | if attributes.respond_to?(:permitted?) | 261 | if attributes.respond_to?(:permitted?) |
250 | raise ActiveModel::ForbiddenAttributesError if !attributes.permitted? | 262 | raise ActiveModel::ForbiddenAttributesError if !attributes.permitted? |
test/aggs_test.rb
@@ -21,6 +21,11 @@ class AggsTest < Minitest::Test | @@ -21,6 +21,11 @@ class AggsTest < Minitest::Test | ||
21 | assert_equal ({1 => 1}), buckets_as_hash(Product.search("Product", relation: true).aggs(store_id: {where: {in_stock: true}}).aggs["store_id"]) | 21 | assert_equal ({1 => 1}), buckets_as_hash(Product.search("Product", relation: true).aggs(store_id: {where: {in_stock: true}}).aggs["store_id"]) |
22 | end | 22 | end |
23 | 23 | ||
24 | + def test_relation | ||
25 | + relation = Product.search("Product", relation: true).aggs(:store_id).aggs(color: {where: {in_stock: true}}) | ||
26 | + assert_equal ["color", "store_id"], relation.aggs.keys.sort | ||
27 | + end | ||
28 | + | ||
24 | def test_order | 29 | def test_order |
25 | agg = Product.search("Product", aggs: {color: {order: {_key: "desc"}}}).aggs["color"] | 30 | agg = Product.search("Product", aggs: {color: {order: {_key: "desc"}}}).aggs["color"] |
26 | assert_equal %w(red green blue), agg["buckets"].map { |b| b["key"] } | 31 | assert_equal %w(red green blue), agg["buckets"].map { |b| b["key"] } |