Skip to content

Commit d7bb656

Browse files
committed
Fixed string/symbol mismatch between where and agg_where
1 parent d2f74fa commit d7bb656

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

lib/searchkick/query.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,6 @@ def set_aggregations(payload, filters, post_filters)
872872
}
873873
end
874874

875-
# TODO handle string/symbol mismatch between where and agg_where
876875
agg_where = ensure_permitted(agg_options[:where] || {})
877876
if options[:smart_aggs] != false && options[:where]
878877
agg_where = smart_agg_filters(ensure_permitted(options[:where]), field.to_s, agg_where)
@@ -906,6 +905,7 @@ def set_aggregations(payload, filters, post_filters)
906905

907906
def smart_agg_filters(where, field, agg_where)
908907
smart_where = {}
908+
agg_where_keys = agg_where.except(:_and, :_or, :_not).to_h { |k, v| [k.to_s, true] }
909909
where.each do |f, v|
910910
case f
911911
when :_and
@@ -917,7 +917,7 @@ def smart_agg_filters(where, field, agg_where)
917917
# TODO figure out agg_where overrides
918918
smart_where[f] = smart_agg_filters(v, field, {})
919919
else
920-
if f.to_s != field
920+
if f.to_s != field && !agg_where_keys.include?(f.to_s)
921921
smart_where[f] = v
922922
end
923923
end

test/aggs_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ def test_smart_aggs
153153
assert_aggs ({"store_id" => {2 => 1}}), where: {color: "blue"}, aggs: {store_id: {where: {in_stock: false, color: "red"}}}
154154
assert_aggs ({"store_id" => {2 => 1}}), where: {color: "blue"}, aggs: {store_id: {where: {in_stock: false, color: "red"}}}, smart_aggs: false
155155

156+
assert_aggs ({"store_id" => {2 => 1}}), where: {color: "blue"}, aggs: {store_id: {where: {in_stock: false, "color" => "red"}}}
157+
assert_aggs ({"store_id" => {2 => 1}}), where: {"color" => "blue"}, aggs: {store_id: {where: {in_stock: false, color: "red"}}}
158+
156159
assert_aggs ({"store_id" => {1 => 1, 2 => 2}}), where: {store_id: 2}, aggs: [:store_id]
157160
assert_aggs ({"store_id" => {1 => 1, 2 => 2}}), where: {store_id: 2}, aggs: [:store_id], smart_aggs: false
158161

0 commit comments

Comments
 (0)