|
82 | 82 | import static org.hamcrest.Matchers.equalTo; |
83 | 83 | import static org.hamcrest.Matchers.is; |
84 | 84 | import static org.hamcrest.Matchers.sameInstance; |
| 85 | +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; |
85 | 86 |
|
86 | 87 | public class QueryAnalyzerTests extends ESTestCase { |
87 | 88 |
|
@@ -1506,4 +1507,135 @@ public void testIntervalQueries() { |
1506 | 1507 | assertTermsEqual(result.extractions, new Term("field", "a")); |
1507 | 1508 | } |
1508 | 1509 |
|
| 1510 | + public void testCombinedRangeAndTermWithMinimumShouldMatch() { |
| 1511 | + |
| 1512 | + Query disj = new BooleanQuery.Builder() |
| 1513 | + .add(IntPoint.newRangeQuery("i", 0, 10), Occur.SHOULD) |
| 1514 | + .add(new TermQuery(new Term("f", "v1")), Occur.SHOULD) |
| 1515 | + .add(new TermQuery(new Term("f", "v1")), Occur.SHOULD) |
| 1516 | + .setMinimumNumberShouldMatch(2) |
| 1517 | + .build(); |
| 1518 | + |
| 1519 | + Result r = analyze(disj, Version.CURRENT); |
| 1520 | + assertThat(r.minimumShouldMatch, equalTo(1)); |
| 1521 | + assertThat(r.extractions, hasSize(2)); |
| 1522 | + assertFalse(r.matchAllDocs); |
| 1523 | + assertFalse(r.verified); |
| 1524 | + |
| 1525 | + Query q = new BooleanQuery.Builder() |
| 1526 | + .add(IntPoint.newRangeQuery("i", 0, 10), Occur.SHOULD) |
| 1527 | + .add(new TermQuery(new Term("f", "v1")), Occur.SHOULD) |
| 1528 | + .add(new TermQuery(new Term("f", "v1")), Occur.SHOULD) |
| 1529 | + .add(new TermQuery(new Term("f", "v1")), Occur.FILTER) |
| 1530 | + .setMinimumNumberShouldMatch(2) |
| 1531 | + .build(); |
| 1532 | + |
| 1533 | + Result result = analyze(q, Version.CURRENT); |
| 1534 | + assertThat(result.minimumShouldMatch, equalTo(1)); |
| 1535 | + assertThat(result.extractions.size(), equalTo(2)); |
| 1536 | + assertFalse(result.verified); |
| 1537 | + assertFalse(result.matchAllDocs); |
| 1538 | + |
| 1539 | + q = new BooleanQuery.Builder() |
| 1540 | + .add(q, Occur.MUST) |
| 1541 | + .add(q, Occur.MUST) |
| 1542 | + .build(); |
| 1543 | + |
| 1544 | + result = analyze(q, Version.CURRENT); |
| 1545 | + assertThat(result.minimumShouldMatch, equalTo(1)); |
| 1546 | + assertThat(result.extractions.size(), equalTo(2)); |
| 1547 | + assertFalse(result.verified); |
| 1548 | + assertFalse(result.matchAllDocs); |
| 1549 | + |
| 1550 | + Query q2 = new BooleanQuery.Builder() |
| 1551 | + .add(new TermQuery(new Term("f", "v1")), Occur.FILTER) |
| 1552 | + .add(IntPoint.newRangeQuery("i", 15, 20), Occur.SHOULD) |
| 1553 | + .add(new TermQuery(new Term("f", "v2")), Occur.SHOULD) |
| 1554 | + .add(new TermQuery(new Term("f", "v2")), Occur.MUST) |
| 1555 | + .setMinimumNumberShouldMatch(1) |
| 1556 | + .build(); |
| 1557 | + |
| 1558 | + result = analyze(q2, Version.CURRENT); |
| 1559 | + assertThat(result.minimumShouldMatch, equalTo(2)); |
| 1560 | + assertThat(result.extractions, hasSize(3)); |
| 1561 | + assertFalse(result.verified); |
| 1562 | + assertFalse(result.matchAllDocs); |
| 1563 | + |
| 1564 | + // multiple range queries on different fields |
| 1565 | + Query q3 = new BooleanQuery.Builder() |
| 1566 | + .add(IntPoint.newRangeQuery("i", 15, 20), Occur.SHOULD) |
| 1567 | + .add(IntPoint.newRangeQuery("i2", 15, 20), Occur.SHOULD) |
| 1568 | + .add(new TermQuery(new Term("f", "v1")), Occur.SHOULD) |
| 1569 | + .add(new TermQuery(new Term("f", "v2")), Occur.MUST) |
| 1570 | + .setMinimumNumberShouldMatch(1) |
| 1571 | + .build(); |
| 1572 | + result = analyze(q3, Version.CURRENT); |
| 1573 | + assertThat(result.minimumShouldMatch, equalTo(2)); |
| 1574 | + assertThat(result.extractions, hasSize(4)); |
| 1575 | + assertFalse(result.verified); |
| 1576 | + assertFalse(result.matchAllDocs); |
| 1577 | + |
| 1578 | + // multiple disjoint range queries on the same field |
| 1579 | + Query q4 = new BooleanQuery.Builder() |
| 1580 | + .add(IntPoint.newRangeQuery("i", 15, 20), Occur.SHOULD) |
| 1581 | + .add(IntPoint.newRangeQuery("i", 25, 30), Occur.SHOULD) |
| 1582 | + .add(IntPoint.newRangeQuery("i", 35, 40), Occur.SHOULD) |
| 1583 | + .add(new TermQuery(new Term("f", "v1")), Occur.SHOULD) |
| 1584 | + .add(new TermQuery(new Term("f", "v2")), Occur.MUST) |
| 1585 | + .setMinimumNumberShouldMatch(1) |
| 1586 | + .build(); |
| 1587 | + result = analyze(q4, Version.CURRENT); |
| 1588 | + assertThat(result.minimumShouldMatch, equalTo(2)); |
| 1589 | + assertThat(result.extractions, hasSize(5)); |
| 1590 | + assertFalse(result.verified); |
| 1591 | + assertFalse(result.matchAllDocs); |
| 1592 | + |
| 1593 | + // multiple conjunction range queries on the same field |
| 1594 | + Query q5 = new BooleanQuery.Builder() |
| 1595 | + .add(new BooleanQuery.Builder() |
| 1596 | + .add(IntPoint.newRangeQuery("i", 15, 20), Occur.MUST) |
| 1597 | + .add(IntPoint.newRangeQuery("i", 25, 30), Occur.MUST) |
| 1598 | + .build(), Occur.MUST) |
| 1599 | + .add(IntPoint.newRangeQuery("i", 35, 40), Occur.MUST) |
| 1600 | + .add(new TermQuery(new Term("f", "v2")), Occur.MUST) |
| 1601 | + .build(); |
| 1602 | + result = analyze(q5, Version.CURRENT); |
| 1603 | + assertThat(result.minimumShouldMatch, equalTo(2)); |
| 1604 | + assertThat(result.extractions, hasSize(4)); |
| 1605 | + assertFalse(result.verified); |
| 1606 | + assertFalse(result.matchAllDocs); |
| 1607 | + |
| 1608 | + // multiple conjunction range queries on different fields |
| 1609 | + Query q6 = new BooleanQuery.Builder() |
| 1610 | + .add(new BooleanQuery.Builder() |
| 1611 | + .add(IntPoint.newRangeQuery("i", 15, 20), Occur.MUST) |
| 1612 | + .add(IntPoint.newRangeQuery("i2", 25, 30), Occur.MUST) |
| 1613 | + .build(), Occur.MUST) |
| 1614 | + .add(IntPoint.newRangeQuery("i", 35, 40), Occur.MUST) |
| 1615 | + .add(new TermQuery(new Term("f", "v2")), Occur.MUST) |
| 1616 | + .build(); |
| 1617 | + result = analyze(q6, Version.CURRENT); |
| 1618 | + assertThat(result.minimumShouldMatch, equalTo(3)); |
| 1619 | + assertThat(result.extractions, hasSize(4)); |
| 1620 | + assertFalse(result.verified); |
| 1621 | + assertFalse(result.matchAllDocs); |
| 1622 | + |
| 1623 | + // mixed term and range conjunctions |
| 1624 | + Query q7 = new BooleanQuery.Builder() |
| 1625 | + .add(new BooleanQuery.Builder() |
| 1626 | + .add(IntPoint.newRangeQuery("i", 1, 2), Occur.MUST) |
| 1627 | + .add(new TermQuery(new Term("f", "1")), Occur.MUST) |
| 1628 | + .build(), Occur.MUST) |
| 1629 | + .add(new BooleanQuery.Builder() |
| 1630 | + .add(IntPoint.newRangeQuery("i", 1, 2), Occur.MUST) |
| 1631 | + .add(new TermQuery(new Term("f", "2")), Occur.MUST) |
| 1632 | + .build(), Occur.MUST) |
| 1633 | + .build(); |
| 1634 | + result = analyze(q7, Version.CURRENT); |
| 1635 | + assertThat(result.minimumShouldMatch, equalTo(3)); |
| 1636 | + assertThat(result.extractions, hasSize(3)); |
| 1637 | + assertFalse(result.verified); |
| 1638 | + assertFalse(result.matchAllDocs); |
| 1639 | + } |
| 1640 | + |
1509 | 1641 | } |
0 commit comments