Commit 77729d368e7527f92d810aee5e17c3d2944bc095
1 parent
e1035066
Exists in
master
and in
21 other branches
Do not limit by default, and do not modify response
Showing
3 changed files
with
20 additions
and
12 deletions
Show diff stats
lib/searchkick/query.rb
... | ... | @@ -346,9 +346,12 @@ module Searchkick |
346 | 346 | end |
347 | 347 | |
348 | 348 | aggs.each do |field, agg_options| |
349 | + size = agg_options[:limit] ? agg_options[:limit] : 100_000 | |
350 | + | |
349 | 351 | payload[:aggs][field] = { |
350 | 352 | terms: { |
351 | - field: agg_options[:field] || field | |
353 | + field: agg_options[:field] || field, | |
354 | + size: size | |
352 | 355 | } |
353 | 356 | } |
354 | 357 | |
... | ... | @@ -363,11 +366,7 @@ module Searchkick |
363 | 366 | } |
364 | 367 | }, |
365 | 368 | aggs: { |
366 | - field => { | |
367 | - terms: { | |
368 | - field: field | |
369 | - } | |
370 | - } | |
369 | + field => payload[:aggs][field] | |
371 | 370 | } |
372 | 371 | } |
373 | 372 | end | ... | ... |
lib/searchkick/results.rb
... | ... | @@ -76,12 +76,14 @@ module Searchkick |
76 | 76 | end |
77 | 77 | |
78 | 78 | def aggs |
79 | - response["aggregations"].each do |field, filtered_agg| | |
80 | - buckets = filtered_agg[field] | |
81 | - # move the buckets one level above into the field hash | |
82 | - if buckets | |
83 | - filtered_agg.delete(field) | |
84 | - filtered_agg.merge!(buckets) | |
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) | |
86 | + end | |
85 | 87 | end |
86 | 88 | end |
87 | 89 | end | ... | ... |
test/aggs_test.rb
... | ... | @@ -27,6 +27,13 @@ class TestAggs < Minitest::Test |
27 | 27 | assert_equal ({1 => 1, 2 => 2}), store_agg({aggs: {store_id_new: {field: "store_id"}}}, "store_id_new") |
28 | 28 | end |
29 | 29 | |
30 | + def test_limit | |
31 | + agg = Product.search("Product", aggs: {store_id: {limit: 1}}).aggs["store_id"] | |
32 | + assert_equal 1, agg["buckets"].size | |
33 | + # assert_equal 3, agg["doc_count"] | |
34 | + assert_equal 1, agg["sum_other_doc_count"] | |
35 | + end | |
36 | + | |
30 | 37 | def test_where_no_smart_aggs |
31 | 38 | assert_equal ({2 => 2}), store_agg(where: {color: "red"}, aggs: {store_id: {where: {in_stock: false}}}) |
32 | 39 | assert_equal ({2 => 2}), store_agg(where: {color: "blue"}, aggs: {store_id: {where: {in_stock: false}}}) | ... | ... |