Skip to content

Commit fcae55a

Browse files
authored
Fix query analyzer logic for mixed conjunctions of terms and ranges (#49803)
When the query analyzer examines a conjunction containing both terms and ranges, it should only include ranges in the minimum_should_match calculation if there are no other range queries on that same field within the conjunction. This is because we cannot build a selection query over disjoint ranges on the same field, and it is not easy to check if two range queries have an overlap. The current logic to calculate this just sets minimum_should_match to 1 or 0, dependent on whether or not the current range is over a field that has already been seen. However, this can be incorrect in the case that there are terms in the same match group which adjust the minimum_should_match downwards. Instead, the logic should be changed to match the terms extraction, whereby we adjust minimum_should_match downwards if we have already seen a range field. Fixes #49684
1 parent 2605c7c commit fcae55a

File tree

2 files changed

+168
-30
lines changed

2 files changed

+168
-30
lines changed

modules/percolator/src/main/java/org/elasticsearch/percolator/QueryAnalyzer.java

+36-30
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private static Result handleConjunction(List<Result> conjunctionsWithUnknowns) {
232232
List<Result> conjunctions = conjunctionsWithUnknowns.stream().filter(r -> r.isUnknown() == false).collect(Collectors.toList());
233233
if (conjunctions.isEmpty()) {
234234
if (conjunctionsWithUnknowns.isEmpty()) {
235-
throw new IllegalArgumentException("Must have at least on conjunction sub result");
235+
throw new IllegalArgumentException("Must have at least one conjunction sub result");
236236
}
237237
return conjunctionsWithUnknowns.get(0); // all conjunctions are unknown, so just return the first one
238238
}
@@ -247,47 +247,53 @@ private static Result handleConjunction(List<Result> conjunctionsWithUnknowns) {
247247
int msm = 0;
248248
boolean verified = conjunctionsWithUnknowns.size() == conjunctions.size();
249249
boolean matchAllDocs = true;
250-
boolean hasDuplicateTerms = false;
251250
Set<QueryExtraction> extractions = new HashSet<>();
252251
Set<String> seenRangeFields = new HashSet<>();
253252
for (Result result : conjunctions) {
254-
// In case that there are duplicate query extractions we need to be careful with
255-
// incrementing msm,
256-
// because that could lead to valid matches not becoming candidate matches:
257-
// query: (field:val1 AND field:val2) AND (field:val2 AND field:val3)
258-
// doc: field: val1 val2 val3
259-
// So lets be protective and decrease the msm:
253+
260254
int resultMsm = result.minimumShouldMatch;
261255
for (QueryExtraction queryExtraction : result.extractions) {
262256
if (queryExtraction.range != null) {
263257
// In case of range queries each extraction does not simply increment the
264-
// minimum_should_match
265-
// for that percolator query like for a term based extraction, so that can lead
266-
// to more false
267-
// positives for percolator queries with range queries than term based queries.
268-
// The is because the way number fields are extracted from the document to be
269-
// percolated.
270-
// Per field a single range is extracted and if a percolator query has two or
271-
// more range queries
272-
// on the same field, then the minimum should match can be higher than clauses
273-
// in the CoveringQuery.
274-
// Therefore right now the minimum should match is incremented once per number
275-
// field when processing
276-
// the percolator query at index time.
277-
if (seenRangeFields.add(queryExtraction.range.fieldName)) {
278-
resultMsm = 1;
279-
} else {
280-
resultMsm = 0;
258+
// minimum_should_match for that percolator query like for a term based extraction,
259+
// so that can lead to more false positives for percolator queries with range queries
260+
// than term based queries.
261+
// This is because the way number fields are extracted from the document to be
262+
// percolated. Per field a single range is extracted and if a percolator query has two or
263+
// more range queries on the same field, then the minimum should match can be higher than clauses
264+
// in the CoveringQuery. Therefore right now the minimum should match is only incremented once per
265+
// number field when processing the percolator query at index time.
266+
// For multiple ranges within a single extraction (ie from an existing conjunction or disjunction)
267+
// then this will already have been taken care of, so we only check against fieldnames from
268+
// previously processed extractions, and don't add to the seenRangeFields list until all
269+
// extractions from this result are processed
270+
if (seenRangeFields.contains(queryExtraction.range.fieldName)) {
271+
resultMsm = Math.max(0, resultMsm - 1);
272+
verified = false;
281273
}
282274
}
283-
284-
if (extractions.contains(queryExtraction)) {
285-
resultMsm = Math.max(0, resultMsm - 1);
286-
verified = false;
275+
else {
276+
// In case that there are duplicate term query extractions we need to be careful with
277+
// incrementing msm, because that could lead to valid matches not becoming candidate matches:
278+
// query: (field:val1 AND field:val2) AND (field:val2 AND field:val3)
279+
// doc: field: val1 val2 val3
280+
// So lets be protective and decrease the msm:
281+
if (extractions.contains(queryExtraction)) {
282+
resultMsm = Math.max(0, resultMsm - 1);
283+
verified = false;
284+
}
287285
}
288286
}
289287
msm += resultMsm;
290288

289+
// add range fields from this Result to the seenRangeFields set so that minimumShouldMatch is correctly
290+
// calculated for subsequent Results
291+
result.extractions.stream()
292+
.map(e -> e.range)
293+
.filter(Objects::nonNull)
294+
.map(e -> e.fieldName)
295+
.forEach(seenRangeFields::add);
296+
291297
if (result.verified == false
292298
// If some inner extractions are optional, the result can't be verified
293299
|| result.minimumShouldMatch < result.extractions.size()) {
@@ -299,7 +305,7 @@ private static Result handleConjunction(List<Result> conjunctionsWithUnknowns) {
299305
if (matchAllDocs) {
300306
return new Result(matchAllDocs, verified);
301307
} else {
302-
return new Result(verified, extractions, hasDuplicateTerms ? 1 : msm);
308+
return new Result(verified, extractions, msm);
303309
}
304310
}
305311

modules/percolator/src/test/java/org/elasticsearch/percolator/QueryAnalyzerTests.java

+132
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import static org.elasticsearch.percolator.QueryAnalyzer.analyze;
7979
import static org.hamcrest.Matchers.equalTo;
8080
import static org.hamcrest.Matchers.is;
81+
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
8182

8283
public class QueryAnalyzerTests extends ESTestCase {
8384

@@ -1208,4 +1209,135 @@ public void testIntervalQueries() {
12081209
assertTermsEqual(result.extractions, new Term("field", "a"));
12091210
}
12101211

1212+
public void testCombinedRangeAndTermWithMinimumShouldMatch() {
1213+
1214+
Query disj = new BooleanQuery.Builder()
1215+
.add(IntPoint.newRangeQuery("i", 0, 10), Occur.SHOULD)
1216+
.add(new TermQuery(new Term("f", "v1")), Occur.SHOULD)
1217+
.add(new TermQuery(new Term("f", "v1")), Occur.SHOULD)
1218+
.setMinimumNumberShouldMatch(2)
1219+
.build();
1220+
1221+
Result r = analyze(disj, Version.CURRENT);
1222+
assertThat(r.minimumShouldMatch, equalTo(1));
1223+
assertThat(r.extractions, hasSize(2));
1224+
assertFalse(r.matchAllDocs);
1225+
assertFalse(r.verified);
1226+
1227+
Query q = new BooleanQuery.Builder()
1228+
.add(IntPoint.newRangeQuery("i", 0, 10), Occur.SHOULD)
1229+
.add(new TermQuery(new Term("f", "v1")), Occur.SHOULD)
1230+
.add(new TermQuery(new Term("f", "v1")), Occur.SHOULD)
1231+
.add(new TermQuery(new Term("f", "v1")), Occur.FILTER)
1232+
.setMinimumNumberShouldMatch(2)
1233+
.build();
1234+
1235+
Result result = analyze(q, Version.CURRENT);
1236+
assertThat(result.minimumShouldMatch, equalTo(1));
1237+
assertThat(result.extractions.size(), equalTo(2));
1238+
assertFalse(result.verified);
1239+
assertFalse(result.matchAllDocs);
1240+
1241+
q = new BooleanQuery.Builder()
1242+
.add(q, Occur.MUST)
1243+
.add(q, Occur.MUST)
1244+
.build();
1245+
1246+
result = analyze(q, Version.CURRENT);
1247+
assertThat(result.minimumShouldMatch, equalTo(1));
1248+
assertThat(result.extractions.size(), equalTo(2));
1249+
assertFalse(result.verified);
1250+
assertFalse(result.matchAllDocs);
1251+
1252+
Query q2 = new BooleanQuery.Builder()
1253+
.add(new TermQuery(new Term("f", "v1")), Occur.FILTER)
1254+
.add(IntPoint.newRangeQuery("i", 15, 20), Occur.SHOULD)
1255+
.add(new TermQuery(new Term("f", "v2")), Occur.SHOULD)
1256+
.add(new TermQuery(new Term("f", "v2")), Occur.MUST)
1257+
.setMinimumNumberShouldMatch(1)
1258+
.build();
1259+
1260+
result = analyze(q2, Version.CURRENT);
1261+
assertThat(result.minimumShouldMatch, equalTo(2));
1262+
assertThat(result.extractions, hasSize(3));
1263+
assertFalse(result.verified);
1264+
assertFalse(result.matchAllDocs);
1265+
1266+
// multiple range queries on different fields
1267+
Query q3 = new BooleanQuery.Builder()
1268+
.add(IntPoint.newRangeQuery("i", 15, 20), Occur.SHOULD)
1269+
.add(IntPoint.newRangeQuery("i2", 15, 20), Occur.SHOULD)
1270+
.add(new TermQuery(new Term("f", "v1")), Occur.SHOULD)
1271+
.add(new TermQuery(new Term("f", "v2")), Occur.MUST)
1272+
.setMinimumNumberShouldMatch(1)
1273+
.build();
1274+
result = analyze(q3, Version.CURRENT);
1275+
assertThat(result.minimumShouldMatch, equalTo(2));
1276+
assertThat(result.extractions, hasSize(4));
1277+
assertFalse(result.verified);
1278+
assertFalse(result.matchAllDocs);
1279+
1280+
// multiple disjoint range queries on the same field
1281+
Query q4 = new BooleanQuery.Builder()
1282+
.add(IntPoint.newRangeQuery("i", 15, 20), Occur.SHOULD)
1283+
.add(IntPoint.newRangeQuery("i", 25, 30), Occur.SHOULD)
1284+
.add(IntPoint.newRangeQuery("i", 35, 40), Occur.SHOULD)
1285+
.add(new TermQuery(new Term("f", "v1")), Occur.SHOULD)
1286+
.add(new TermQuery(new Term("f", "v2")), Occur.MUST)
1287+
.setMinimumNumberShouldMatch(1)
1288+
.build();
1289+
result = analyze(q4, Version.CURRENT);
1290+
assertThat(result.minimumShouldMatch, equalTo(2));
1291+
assertThat(result.extractions, hasSize(5));
1292+
assertFalse(result.verified);
1293+
assertFalse(result.matchAllDocs);
1294+
1295+
// multiple conjunction range queries on the same field
1296+
Query q5 = new BooleanQuery.Builder()
1297+
.add(new BooleanQuery.Builder()
1298+
.add(IntPoint.newRangeQuery("i", 15, 20), Occur.MUST)
1299+
.add(IntPoint.newRangeQuery("i", 25, 30), Occur.MUST)
1300+
.build(), Occur.MUST)
1301+
.add(IntPoint.newRangeQuery("i", 35, 40), Occur.MUST)
1302+
.add(new TermQuery(new Term("f", "v2")), Occur.MUST)
1303+
.build();
1304+
result = analyze(q5, Version.CURRENT);
1305+
assertThat(result.minimumShouldMatch, equalTo(2));
1306+
assertThat(result.extractions, hasSize(4));
1307+
assertFalse(result.verified);
1308+
assertFalse(result.matchAllDocs);
1309+
1310+
// multiple conjunction range queries on different fields
1311+
Query q6 = new BooleanQuery.Builder()
1312+
.add(new BooleanQuery.Builder()
1313+
.add(IntPoint.newRangeQuery("i", 15, 20), Occur.MUST)
1314+
.add(IntPoint.newRangeQuery("i2", 25, 30), Occur.MUST)
1315+
.build(), Occur.MUST)
1316+
.add(IntPoint.newRangeQuery("i", 35, 40), Occur.MUST)
1317+
.add(new TermQuery(new Term("f", "v2")), Occur.MUST)
1318+
.build();
1319+
result = analyze(q6, Version.CURRENT);
1320+
assertThat(result.minimumShouldMatch, equalTo(3));
1321+
assertThat(result.extractions, hasSize(4));
1322+
assertFalse(result.verified);
1323+
assertFalse(result.matchAllDocs);
1324+
1325+
// mixed term and range conjunctions
1326+
Query q7 = new BooleanQuery.Builder()
1327+
.add(new BooleanQuery.Builder()
1328+
.add(IntPoint.newRangeQuery("i", 1, 2), Occur.MUST)
1329+
.add(new TermQuery(new Term("f", "1")), Occur.MUST)
1330+
.build(), Occur.MUST)
1331+
.add(new BooleanQuery.Builder()
1332+
.add(IntPoint.newRangeQuery("i", 1, 2), Occur.MUST)
1333+
.add(new TermQuery(new Term("f", "2")), Occur.MUST)
1334+
.build(), Occur.MUST)
1335+
.build();
1336+
result = analyze(q7, Version.CURRENT);
1337+
assertThat(result.minimumShouldMatch, equalTo(3));
1338+
assertThat(result.extractions, hasSize(3));
1339+
assertFalse(result.verified);
1340+
assertFalse(result.matchAllDocs);
1341+
}
1342+
12111343
}

0 commit comments

Comments
 (0)