diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index 4ecf6b1..e9d2abf 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -77,12 +77,14 @@ module Searchkick def aggs @aggs ||= begin - response["aggregations"].dup.each do |field, filtered_agg| - buckets = filtered_agg[field] - # move the buckets one level above into the field hash - if buckets - filtered_agg.delete(field) - filtered_agg.merge!(buckets) + if response["aggregations"] + response["aggregations"].dup.each do |field, filtered_agg| + buckets = filtered_agg[field] + # move the buckets one level above into the field hash + if buckets + filtered_agg.delete(field) + filtered_agg.merge!(buckets) + end end end end diff --git a/test/aggs_test.rb b/test/aggs_test.rb index 48ec3e8..0553169 100644 --- a/test/aggs_test.rb +++ b/test/aggs_test.rb @@ -25,6 +25,10 @@ class AggsTest < Minitest::Test assert_equal ({1 => 1, 2 => 2}), store_agg({aggs: {store_id_new: {field: "store_id"}}}, "store_id_new") end + def test_no_aggs + assert_nil Product.search("*").aggs + end + def test_limit agg = Product.search("Product", aggs: {store_id: {limit: 1}}).aggs["store_id"] assert_equal 1, agg["buckets"].size -- libgit2 0.21.0