Commit ff4c8042d7c5bf55ab9cb2d332a1629e92cc5e03
Committed by
Andrew Kane
1 parent
64615cb6
Exists in
master
and in
19 other branches
Support cardinality metric aggregator (#876)
Showing
2 changed files
with
20 additions
and
0 deletions
Show diff stats
lib/searchkick/query.rb
... | ... | @@ -643,6 +643,12 @@ module Searchkick |
643 | 643 | interval: interval |
644 | 644 | } |
645 | 645 | } |
646 | + elsif agg_options[:cardinality] | |
647 | + payload[:aggs][field] = { | |
648 | + cardinality: { | |
649 | + field: agg_options[:cardinality][:field] || field | |
650 | + } | |
651 | + } | |
646 | 652 | else |
647 | 653 | payload[:aggs][field] = { |
648 | 654 | terms: { | ... | ... |
test/aggs_test.rb
... | ... | @@ -116,6 +116,20 @@ class AggsTest < Minitest::Test |
116 | 116 | assert_equal 4, products.aggs["products_per_year"]["buckets"].size |
117 | 117 | end |
118 | 118 | |
119 | + def test_aggs_cardinality | |
120 | + products = | |
121 | + Product.search("*", { | |
122 | + aggs: { | |
123 | + total_stores: { | |
124 | + cardinality: { | |
125 | + field: :store_id | |
126 | + } | |
127 | + } | |
128 | + } | |
129 | + }) | |
130 | + assert_equal 3, products.aggs["total_stores"]["value"] | |
131 | + end | |
132 | + | |
119 | 133 | protected |
120 | 134 | |
121 | 135 | def buckets_as_hash(agg) | ... | ... |