|
78 | 78 | import static org.elasticsearch.percolator.QueryAnalyzer.analyze;
|
79 | 79 | import static org.hamcrest.Matchers.equalTo;
|
80 | 80 | import static org.hamcrest.Matchers.is;
|
| 81 | +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; |
81 | 82 |
|
82 | 83 | public class QueryAnalyzerTests extends ESTestCase {
|
83 | 84 |
|
@@ -1208,4 +1209,135 @@ public void testIntervalQueries() {
|
1208 | 1209 | assertTermsEqual(result.extractions, new Term("field", "a"));
|
1209 | 1210 | }
|
1210 | 1211 |
|
| 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 | + |
1211 | 1343 | }
|
0 commit comments