Skip to content

Commit cde8809

Browse files
authored
Fix sibling should-producing filters collapsing into one OR group (#1353)
## Problem `minimum_should_match: 1` applies to the entire `should` array of a bool node, so a node can hold only one group of `should` clauses. Two sibling filters that must be ANDed together can each produce such a group, and both groups went into the same array. `(A OR B) AND (C OR D)` became `A OR B OR C OR D`. `FilterInterpreter` merged the clauses of an `any_satisfy` sub-filter into the shared parent node ([`filter_interpreter.rb:187`](https://github.com/block/elasticgraph/blob/0fbeffff/elasticgraph-graphql/lib/elastic_graph/graphql/filtering/filter_interpreter.rb#L187)), and [`build_bool_hash`](https://github.com/block/elasticgraph/blob/0fbeffff/elasticgraph-graphql/lib/elastic_graph/graphql/filtering/filter_interpreter.rb#L353) then stamped one `minimum_should_match: 1` over the combined node. `BooleanQuery#merge_into` did the same for `any_of`. This filter: ```ruby { "any_of" => [ {"name" => {"equal_to_any_of" => ["x"]}}, {"name" => {"equal_to_any_of" => ["y"]}} ], "ages" => {"any_satisfy" => {"any_of" => [{"gt" => 30}, {"lt" => 5}]}} } ``` must mean `(name = x OR name = y) AND (age > 30 OR age < 5)`. It generated one flat `should` array with all four clauses, so a document with `name = "z"` and `ages = [40]` was a search hit. There was no error and no log entry. The `required_matching_clause_count > 1` guard in `process_any_satisfy_filter_expression_on_scalar_list` inspects one `any_satisfy` sub-filter alone, so it cannot detect a collision with a sibling filter. Four legal, public filter forms collapsed: 1. `any_of` next to `any_satisfy: {any_of: ...}` — the case above. 2. Two `any_satisfy: {any_of: ...}` filters on different scalar list fields. 3. An `any_satisfy: {any_of: ...}` client filter next to an `any_of` internal filter. 4. An `any_of` client filter next to an `any_of` internal filter. Cases 3 and 4 need no `any_satisfy`. `DatastoreQuery` passes `client_filters + internal_filters` to one `build_query` call, so the clauses share one bool node. An internal filter must restrict the client's results, and a relationship `additional_filter` or a `query_interceptor` filter can contain `any_of`. When it did, the restriction became one more OR branch. ## Fix `BooleanQuery#merge_into` now nests a `should` group in its own bool query under `filter` when the target node already has a group: ```ruby def merge_into(bool_node) if occurrence == :should && bool_node.key?(:should) BooleanQuery.filter({bool: {minimum_should_match: 1, should: clauses}}).merge_into(bool_node) else bool_node[occurrence].concat(clauses) end end ``` The nested group is required on its own, which restores the AND. It routes through `BooleanQuery.filter`, so the existing unwrap keeps a single-clause group cacheable. `process_any_satisfy_filter_expression_on_scalar_list` merged its clauses directly into the parent node and bypassed `merge_into`, so it now routes a `should` group through `merge_into`. The `required_matching_clause_count > 1` guard is unchanged: when the guard passes and the sub-filter has `should` clauses, that group is the whole sub-filter, because any other clause would raise the count above 1. The first group stays on the shared node and each later group nests, so a query without a collision keeps its current shape and stays flat. No existing spec expectation changed. For the filter above, the query is now: ```json {"bool": { "minimum_should_match": 1, "should": [ {"bool": {"filter": [{"terms": {"name": ["x"]}}]}}, {"bool": {"filter": [{"terms": {"name": ["y"]}}]}} ], "filter": [ {"bool": {"minimum_should_match": 1, "should": [ {"bool": {"filter": [{"range": {"ages": {"gt": 30}}}]}}, {"bool": {"filter": [{"range": {"ages": {"lt": 5}}}]}} ]}} ] }} ``` ## Specs The specs came first and failed before the fix. - `spec/unit/.../datastore_query/filtering_spec.rb` — the generated query for each of the four cases, plus three clauses in one filter, the single-clause unwrap, and the equivalent `all_of` form. - `spec/integration/.../datastore_query/filtering_spec.rb` — the documents the datastore returns for cases 1 and 3. Before the fix, both returned `["t1", "t2", "t3"]` where only `t1` matches the filter. ## Verification - `script/run_specs` — 5263 examples, 0 failures. - `script/type_check` — no type error. - `script/lint` — 894 files, no offenses.
1 parent b98f719 commit cde8809

5 files changed

Lines changed: 277 additions & 1 deletion

File tree

‎elasticgraph-graphql/lib/elastic_graph/graphql/filtering/boolean_query.rb‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,18 @@ def self.should(*clauses)
4747
end
4848

4949
def merge_into(bool_node)
50-
bool_node[occurrence].concat(clauses)
50+
if occurrence == :should && bool_node.key?(:should)
51+
# `minimum_should_match: 1` applies to the entire `should` array of a bool node, so a
52+
# node can hold only one group of `should` clauses. `bool_node` already has such a group,
53+
# and our clauses are a separate group which must be ANDed with it--not ORed into it.
54+
# (Merging them into one array would turn `(A OR B) AND (C OR D)` into `A OR B OR C OR D`.)
55+
#
56+
# So, we nest our clauses in their own bool query and add that as a `filter` clause, where
57+
# it is required on its own.
58+
BooleanQuery.filter({bool: {minimum_should_match: 1, should: clauses}}).merge_into(bool_node)
59+
else
60+
bool_node[occurrence].concat(clauses)
61+
end
5162
end
5263

5364
ALWAYS_FALSE_FILTER = filter({match_none: {}})

‎elasticgraph-graphql/lib/elastic_graph/graphql/filtering/filter_interpreter.rb‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ def process_any_satisfy_filter_expression_on_scalar_list(bool_node, filter, fiel
183183
raise ::GraphQL::ExecutionError, "`#{formatted_filter}` is not supported because it produces " \
184184
"multiple filtering clauses under `#{schema_names.any_satisfy}`, which doesn't work as expected. " \
185185
"Remove one or more of your `#{schema_names.any_satisfy}` predicates and try again."
186+
elsif processed_bool_query.key?(:should)
187+
# Our sub-filter is a group of `should` clauses (from an `any_of`), and only one of them
188+
# must match. We merge it with `BooleanQuery#merge_into` rather than merging the clauses
189+
# directly, so that it does not get ORed into a `should` group that a sibling filter has
190+
# already put on `bool_node`.
191+
#
192+
# Note: `build_bool_hash` gives `processed_bool_query` a default proc that assigns `[]`, so
193+
# `key?` is required here. `processed_bool_query[:should]` would return a truthy `[]` for
194+
# every sub-filter and would also add the key.
195+
BooleanQuery.should(*processed_bool_query.fetch(:should)).merge_into(bool_node)
186196
else
187197
bool_node.update(processed_bool_query) do |_, existing_clauses, any_satisfy_clauses|
188198
existing_clauses + any_satisfy_clauses

‎elasticgraph-graphql/spec/acceptance/search_spec.rb‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,7 @@ module ElasticGraph
10281028
build(
10291029
:team,
10301030
id: "t1",
1031+
current_name: "Aces",
10311032
details: build(:team_details, count: 5),
10321033
past_names: ["Pilots", "Pink Sox"],
10331034
won_championships_at: [],
@@ -1043,6 +1044,7 @@ module ElasticGraph
10431044
build(
10441045
:team,
10451046
id: "t2",
1047+
current_name: "Bees",
10461048
details: build(:team_details, count: 15),
10471049
past_names: ["Pink Sox", "Ace Piloteers"],
10481050
won_championships_at: ["2013-11-27T02:30:00Z", "2013-11-27T22:30:00Z"],
@@ -1061,6 +1063,7 @@ module ElasticGraph
10611063
build(
10621064
:team,
10631065
id: "t3",
1066+
current_name: "Cyclones",
10641067
details: build(:team_details, count: 4),
10651068
past_names: ["Pilots"],
10661069
won_championships_at: ["2003-10-27T19:30:00Z"],
@@ -1080,6 +1083,7 @@ module ElasticGraph
10801083
:team,
10811084
details: build(:team_details, count: 12),
10821085
id: "t4",
1086+
current_name: "Dukes",
10831087
past_names: ["Pill Bugs"],
10841088
won_championships_at: ["2005-10-27T12:30:00Z"],
10851089
forbes_valuations: [42],
@@ -1142,6 +1146,18 @@ module ElasticGraph
11421146
# t1 has 200_000_000; t3 has 0
11431147
expect(results).to eq [{"id" => "t1"}, {"id" => "t3"}]
11441148

1149+
# Verify `any_of: [...]` next to a sibling `any_satisfy: {any_of: [...]}`. Each filter produces its own
1150+
# group of `should` clauses, and the two groups must both be true of a document.
1151+
results = query_teams_with(filter: {
1152+
any_of: [
1153+
{current_name: {equal_to_any_of: ["Aces"]}},
1154+
{current_name: {equal_to_any_of: ["Bees"]}}
1155+
],
1156+
forbes_valuations: {any_satisfy: {any_of: [{gt: 150_000_000}, {lt: 5}]}}
1157+
})
1158+
# t1 and t2 match the `any_of`; t1 and t3 match the `any_satisfy`. Only t1 matches both.
1159+
expect(results).to eq [{"id" => "t1"}]
1160+
11451161
# Verify we can use the `any_satisfy` filter operator on a list-of-nested objects correctly.
11461162
# Also, verify that the sub-objects are considered independently. Team t3 has a player with the name
11471163
# "Babe Truth" and a player with the nickname "Bambino", but they aren't the same player so it should

‎elasticgraph-graphql/spec/integration/elastic_graph/graphql/datastore_query/filtering_spec.rb‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,60 @@ def search_datastore(**options, &before_msearch)
544544
end
545545
end
546546

547+
# Both filters below have two sibling parts that must be ANDed together, and each part produces
548+
# `should` clauses. `minimum_should_match: 1` applies to the entire `should` array of a bool
549+
# node, so the two groups must not share one array. If they did, `(A OR B) AND (C OR D)` would
550+
# become `A OR B OR C OR D`, and t2 and t3 would also match here.
551+
describe "sibling filters that each produce `should` clauses" do
552+
it "requires a document to satisfy both an `any_of` filter and a sibling `any_satisfy: {any_of: ...}` filter" do
553+
index_into(
554+
graphql,
555+
t1 = build(:team, id: "t1", current_name: "Yankees", forbes_valuations: [5_000_000]),
556+
build(:team, id: "t2", current_name: "Cardinals", forbes_valuations: [5_000_000]),
557+
build(:team, id: "t3", current_name: "Dodgers", forbes_valuations: [1_000_000]),
558+
build(:team, id: "t4", current_name: "Cardinals", forbes_valuations: [1_000_000])
559+
)
560+
561+
# `(name = Yankees OR name = Dodgers) AND (valuation > 3M OR valuation < 100K)`
562+
filter = {
563+
"any_of" => [
564+
{"current_name" => {"equal_to_any_of" => ["Yankees"]}},
565+
{"current_name" => {"equal_to_any_of" => ["Dodgers"]}}
566+
],
567+
"forbes_valuations" => {"any_satisfy" => {"any_of" => [{"gt" => 3_000_000}, {"lt" => 100_000}]}}
568+
}
569+
570+
expect(search_with_freeform_filter(filter)).to match_array(ids_of(t1))
571+
end
572+
573+
it "requires a document to satisfy both a client filter and an internal filter that each produce `should` clauses" do
574+
index_into(
575+
graphql,
576+
t1 = build(:team, id: "t1", current_name: "Yankees", forbes_valuations: [5_000_000]),
577+
build(:team, id: "t2", current_name: "Cardinals", forbes_valuations: [5_000_000]),
578+
build(:team, id: "t3", current_name: "Dodgers", forbes_valuations: [1_000_000]),
579+
build(:team, id: "t4", current_name: "Cardinals", forbes_valuations: [1_000_000])
580+
)
581+
582+
# An internal filter (such as a relationship `additional_filter`) must restrict the client's
583+
# results, so this is the same `(name = ...) AND (valuation ...)` filter as above.
584+
results = ids_of(search_datastore(
585+
client_filters: [{"forbes_valuations" => {"any_satisfy" => {"any_of" => [{"gt" => 3_000_000}, {"lt" => 100_000}]}}}],
586+
internal_filters: [{"any_of" => [
587+
{"current_name" => {"equal_to_any_of" => ["Yankees"]}},
588+
{"current_name" => {"equal_to_any_of" => ["Dodgers"]}}
589+
]}],
590+
sort: []
591+
).to_a)
592+
593+
expect(results).to match_array(ids_of(t1))
594+
end
595+
596+
def search_datastore(**options, &before_msearch)
597+
super(index_def_name: "teams", **options, &before_msearch)
598+
end
599+
end
600+
547601
# Note: a `count` filter gets translated into `__counts` (to distinguish it from a schema field named `count`),
548602
# and that translation happens as the query is being built, so we use `__counts` in our example filters here.
549603
describe "`count` filtering on a list" do

‎elasticgraph-graphql/spec/unit/elastic_graph/graphql/datastore_query/filtering_spec.rb‎

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,191 @@ def test_operator_for(require_all_terms, expected_operator)
10391039
end
10401040
end
10411041

1042+
# `minimum_should_match: 1` applies to the entire `should` array of a bool node, so a node can
1043+
# hold only one group of `should` clauses. Each filter below has two sibling parts that must be
1044+
# ANDed together, and each part produces such a group. The first group stays on the shared bool
1045+
# node, and each later group gets nested in its own bool query under `filter`, where it is
1046+
# required on its own. Without that nesting, `(A OR B) AND (C OR D)` would become
1047+
# `A OR B OR C OR D`, which matches documents the filter excludes.
1048+
describe "sibling filters that each produce `should` clauses" do
1049+
it "ANDs an `any_satisfy: {any_of: ...}` filter with a sibling `any_of` filter" do
1050+
query = new_query(client_filter: {
1051+
"any_of" => [
1052+
{"name" => {"equal_to_any_of" => ["x"]}},
1053+
{"name" => {"equal_to_any_of" => ["y"]}}
1054+
],
1055+
"ages" => {"any_satisfy" => {"any_of" => [{"gt" => 30}, {"lt" => 5}]}}
1056+
})
1057+
1058+
# `(name = x OR name = y) AND (age > 30 OR age < 5)`
1059+
expect(datastore_body_of(query)).to query_datastore_with({bool: {
1060+
minimum_should_match: 1,
1061+
should: [
1062+
{bool: {filter: [{terms: {"name" => ["x"]}}]}},
1063+
{bool: {filter: [{terms: {"name" => ["y"]}}]}}
1064+
],
1065+
filter: [
1066+
{bool: {minimum_should_match: 1, should: [
1067+
{bool: {filter: [{range: {"ages" => {gt: 30}}}]}},
1068+
{bool: {filter: [{range: {"ages" => {lt: 5}}}]}}
1069+
]}}
1070+
]
1071+
}})
1072+
end
1073+
1074+
it "ANDs two sibling `any_satisfy: {any_of: ...}` filters on different fields" do
1075+
query = new_query(client_filter: {
1076+
"tags" => {"any_satisfy" => {"any_of" => [{"equal_to_any_of" => ["a"]}, {"equal_to_any_of" => ["b"]}]}},
1077+
"ages" => {"any_satisfy" => {"any_of" => [{"gt" => 30}, {"lt" => 5}]}}
1078+
})
1079+
1080+
# `(tag = a OR tag = b) AND (age > 30 OR age < 5)`
1081+
expect(datastore_body_of(query)).to query_datastore_with({bool: {
1082+
minimum_should_match: 1,
1083+
should: [
1084+
{bool: {filter: [{terms: {"tags" => ["a"]}}]}},
1085+
{bool: {filter: [{terms: {"tags" => ["b"]}}]}}
1086+
],
1087+
filter: [
1088+
{bool: {minimum_should_match: 1, should: [
1089+
{bool: {filter: [{range: {"ages" => {gt: 30}}}]}},
1090+
{bool: {filter: [{range: {"ages" => {lt: 5}}}]}}
1091+
]}}
1092+
]
1093+
}})
1094+
end
1095+
1096+
it "ANDs an `any_satisfy: {any_of: ...}` client filter with an `any_of` internal filter" do
1097+
query = new_query(
1098+
client_filter: {"ages" => {"any_satisfy" => {"any_of" => [{"gt" => 30}, {"lt" => 5}]}}},
1099+
internal_filters: [{"any_of" => [
1100+
{"name" => {"equal_to_any_of" => ["x"]}},
1101+
{"name" => {"equal_to_any_of" => ["y"]}}
1102+
]}]
1103+
)
1104+
1105+
# `(age > 30 OR age < 5) AND (name = x OR name = y)`
1106+
expect(datastore_body_of(query)).to query_datastore_with({bool: {
1107+
minimum_should_match: 1,
1108+
should: [
1109+
{bool: {filter: [{range: {"ages" => {gt: 30}}}]}},
1110+
{bool: {filter: [{range: {"ages" => {lt: 5}}}]}}
1111+
],
1112+
filter: [
1113+
{bool: {minimum_should_match: 1, should: [
1114+
{bool: {filter: [{terms: {"name" => ["x"]}}]}},
1115+
{bool: {filter: [{terms: {"name" => ["y"]}}]}}
1116+
]}}
1117+
]
1118+
}})
1119+
end
1120+
1121+
it "ANDs an `any_of` client filter with an `any_of` internal filter" do
1122+
query = new_query(
1123+
client_filter: {"any_of" => [
1124+
{"name" => {"equal_to_any_of" => ["x"]}},
1125+
{"name" => {"equal_to_any_of" => ["y"]}}
1126+
]},
1127+
internal_filters: [{"any_of" => [
1128+
{"name" => {"equal_to_any_of" => ["y"]}},
1129+
{"name" => {"equal_to_any_of" => ["z"]}}
1130+
]}]
1131+
)
1132+
1133+
# `(name = x OR name = y) AND (name = y OR name = z)`
1134+
expect(datastore_body_of(query)).to query_datastore_with({bool: {
1135+
minimum_should_match: 1,
1136+
should: [
1137+
{bool: {filter: [{terms: {"name" => ["x"]}}]}},
1138+
{bool: {filter: [{terms: {"name" => ["y"]}}]}}
1139+
],
1140+
filter: [
1141+
{bool: {minimum_should_match: 1, should: [
1142+
{bool: {filter: [{terms: {"name" => ["y"]}}]}},
1143+
{bool: {filter: [{terms: {"name" => ["z"]}}]}}
1144+
]}}
1145+
]
1146+
}})
1147+
end
1148+
1149+
it "unwraps a nested group that has a single `should` clause, so that the datastore can cache it" do
1150+
query = new_query(
1151+
client_filter: {"any_of" => [
1152+
{"name" => {"equal_to_any_of" => ["x"]}},
1153+
{"name" => {"equal_to_any_of" => ["y"]}}
1154+
]},
1155+
internal_filters: [{"any_of" => [{"name" => {"equal_to_any_of" => ["y"]}}]}]
1156+
)
1157+
1158+
# `(name = x OR name = y) AND name = y`
1159+
expect(datastore_body_of(query)).to query_datastore_with({bool: {
1160+
minimum_should_match: 1,
1161+
should: [
1162+
{bool: {filter: [{terms: {"name" => ["x"]}}]}},
1163+
{bool: {filter: [{terms: {"name" => ["y"]}}]}}
1164+
],
1165+
filter: [
1166+
{bool: {filter: [{terms: {"name" => ["y"]}}]}}
1167+
]
1168+
}})
1169+
end
1170+
1171+
it "ANDs three sibling filters that each produce `should` clauses" do
1172+
query = new_query(
1173+
client_filter: {
1174+
"any_of" => [
1175+
{"name" => {"equal_to_any_of" => ["x"]}},
1176+
{"name" => {"equal_to_any_of" => ["y"]}}
1177+
],
1178+
"ages" => {"any_satisfy" => {"any_of" => [{"gt" => 30}, {"lt" => 5}]}}
1179+
},
1180+
internal_filters: [{"tags" => {"any_satisfy" => {"any_of" => [{"equal_to_any_of" => ["a"]}, {"equal_to_any_of" => ["b"]}]}}}]
1181+
)
1182+
1183+
# `(name = x OR name = y) AND (age > 30 OR age < 5) AND (tag = a OR tag = b)`
1184+
expect(datastore_body_of(query)).to query_datastore_with({bool: {
1185+
minimum_should_match: 1,
1186+
should: [
1187+
{bool: {filter: [{terms: {"name" => ["x"]}}]}},
1188+
{bool: {filter: [{terms: {"name" => ["y"]}}]}}
1189+
],
1190+
filter: [
1191+
{bool: {minimum_should_match: 1, should: [
1192+
{bool: {filter: [{range: {"ages" => {gt: 30}}}]}},
1193+
{bool: {filter: [{range: {"ages" => {lt: 5}}}]}}
1194+
]}},
1195+
{bool: {minimum_should_match: 1, should: [
1196+
{bool: {filter: [{terms: {"tags" => ["a"]}}]}},
1197+
{bool: {filter: [{terms: {"tags" => ["b"]}}]}}
1198+
]}}
1199+
]
1200+
}})
1201+
end
1202+
1203+
it "produces the same results for the equivalent `all_of` filter" do
1204+
query = new_query(client_filter: {
1205+
"all_of" => [
1206+
{"any_of" => [
1207+
{"name" => {"equal_to_any_of" => ["x"]}},
1208+
{"name" => {"equal_to_any_of" => ["y"]}}
1209+
]},
1210+
{"ages" => {"any_satisfy" => {"any_of" => [{"gt" => 30}, {"lt" => 5}]}}}
1211+
]
1212+
})
1213+
1214+
expect(datastore_body_of(query)).to query_datastore_with({bool: {filter: [
1215+
{bool: {minimum_should_match: 1, should: [
1216+
{bool: {filter: [{terms: {"name" => ["x"]}}]}},
1217+
{bool: {filter: [{terms: {"name" => ["y"]}}]}}
1218+
]}},
1219+
{bool: {minimum_should_match: 1, should: [
1220+
{bool: {filter: [{range: {"ages" => {gt: 30}}}]}},
1221+
{bool: {filter: [{range: {"ages" => {lt: 5}}}]}}
1222+
]}}
1223+
]}})
1224+
end
1225+
end
1226+
10421227
# Note: a `count` filter gets translated into `__counts` (to distinguish it from a schema field named `count`),
10431228
# and that translation happens as the query is being built, so we use `__counts` in our example filters here.
10441229
describe "`count` operator on a list" do

0 commit comments

Comments
 (0)