Commit 1be5621590681e78c3700c4486eeef4d4734fd67
1 parent
8bad332f
Exists in
master
and in
21 other branches
Return nil when no aggs - closes #542
Showing
2 changed files
with
12 additions
and
6 deletions
Show diff stats
lib/searchkick/results.rb
... | ... | @@ -77,12 +77,14 @@ module Searchkick |
77 | 77 | |
78 | 78 | def aggs |
79 | 79 | @aggs ||= begin |
80 | - response["aggregations"].dup.each do |field, filtered_agg| | |
81 | - buckets = filtered_agg[field] | |
82 | - # move the buckets one level above into the field hash | |
83 | - if buckets | |
84 | - filtered_agg.delete(field) | |
85 | - filtered_agg.merge!(buckets) | |
80 | + if response["aggregations"] | |
81 | + response["aggregations"].dup.each do |field, filtered_agg| | |
82 | + buckets = filtered_agg[field] | |
83 | + # move the buckets one level above into the field hash | |
84 | + if buckets | |
85 | + filtered_agg.delete(field) | |
86 | + filtered_agg.merge!(buckets) | |
87 | + end | |
86 | 88 | end |
87 | 89 | end |
88 | 90 | end | ... | ... |
test/aggs_test.rb
... | ... | @@ -25,6 +25,10 @@ class AggsTest < Minitest::Test |
25 | 25 | assert_equal ({1 => 1, 2 => 2}), store_agg({aggs: {store_id_new: {field: "store_id"}}}, "store_id_new") |
26 | 26 | end |
27 | 27 | |
28 | + def test_no_aggs | |
29 | + assert_nil Product.search("*").aggs | |
30 | + end | |
31 | + | |
28 | 32 | def test_limit |
29 | 33 | agg = Product.search("Product", aggs: {store_id: {limit: 1}}).aggs["store_id"] |
30 | 34 | assert_equal 1, agg["buckets"].size | ... | ... |