Skip to content

Commit 36a5b8c

Browse files
committed
Improved smart aggs behavior with _and
1 parent 2a6a707 commit 36a5b8c

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

lib/searchkick/relation.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,11 @@ def where!(value)
575575
check_loaded
576576
value = ensure_permitted(value)
577577
if @options[:where]
578-
if @options[:where][:_and].is_a?(Array)
579-
merge_option(:where, {_and: @options[:where][:_and] + [value]})
580578
# keep simple when possible for smart aggs
581-
elsif !@options[:where].keys.intersect?(value.keys)
579+
if !@options[:where].keys.intersect?(value.keys)
582580
merge_option(:where, value)
581+
elsif @options[:where][:_and].is_a?(Array)
582+
merge_option(:where, {_and: @options[:where][:_and] + [value]})
583583
else
584584
@options[:where] = {_and: [@options[:where], value]}
585585
end

test/aggs_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ def test_smart_aggs_relation
255255

256256
assert_aggs ({"store_id" => {1 => 1, 2 => 1}}), Product.search("Product").where(store_id: 2).where(price: {gt: 5}).aggs(:store_id)
257257
assert_aggs ({"store_id" => {1 => 1, 2 => 1}}), Product.search("Product").where(store_id: 2, price: {gt: 5}).aggs(:store_id)
258+
assert_aggs ({"store_id" => {1 => 1, 2 => 1}}), Product.search("Product").where(_and: [{price: {gt: 5}}]).where(store_id: 2).aggs(:store_id)
258259
assert_aggs ({"store_id" => {2 => 2}}), Product.search("Product").where(color: "red").aggs(store_id: {where: {in_stock: false}}).smart_aggs(false)
259260
end
260261

0 commit comments

Comments
 (0)