Skip to content

Commit

Permalink
Fix signature changes in o.a.l.search.BooleanQuery
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Ross <[email protected]>
  • Loading branch information
andrross committed Nov 13, 2024
1 parent 0fafd24 commit ce3af73
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,15 +300,15 @@ static void assertLateParsingQuery(Query query, String type, String id) throws I
BooleanQuery booleanQuery = (BooleanQuery) lateParsingQuery.getInnerQuery();
assertThat(booleanQuery.clauses().size(), equalTo(2));
// check the inner ids query, we have to call rewrite to get to check the type it's executed against
assertThat(booleanQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.MUST));
assertThat(booleanQuery.clauses().get(0).getQuery(), instanceOf(TermInSetQuery.class));
TermInSetQuery termsQuery = (TermInSetQuery) booleanQuery.clauses().get(0).getQuery();
assertThat(booleanQuery.clauses().get(0).occur(), equalTo(BooleanClause.Occur.MUST));
assertThat(booleanQuery.clauses().get(0).query(), instanceOf(TermInSetQuery.class));
TermInSetQuery termsQuery = (TermInSetQuery) booleanQuery.clauses().get(0).query();
// The query is of type MultiTermQueryConstantScoreBlendedWrapper and is sealed inside Apache Lucene,
// no access to inner queries without using the reflection, falling back to stringified query comparison
assertThat(termsQuery.toString(), equalTo("_id:([ff 69 64])"));
// check the type filter
assertThat(booleanQuery.clauses().get(1).getOccur(), equalTo(BooleanClause.Occur.FILTER));
assertEquals(new TermQuery(new Term("join_field", type)), booleanQuery.clauses().get(1).getQuery());
assertThat(booleanQuery.clauses().get(1).occur(), equalTo(BooleanClause.Occur.FILTER));
assertEquals(new TermQuery(new Term("join_field", type)), booleanQuery.clauses().get(1).query());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,14 @@ public void testMinShouldMatchFilterWithoutShouldClauses() throws Exception {
assertThat(booleanQuery.getMinimumNumberShouldMatch(), equalTo(0));
assertThat(booleanQuery.clauses().size(), equalTo(1));
BooleanClause booleanClause = booleanQuery.clauses().get(0);
assertThat(booleanClause.getOccur(), equalTo(BooleanClause.Occur.FILTER));
assertThat(booleanClause.occur(), equalTo(BooleanClause.Occur.FILTER));
assertThat(booleanClause.getQuery(), instanceOf(BooleanQuery.class));
BooleanQuery innerBooleanQuery = (BooleanQuery) booleanClause.getQuery();
// we didn't set minimum should match initially, there are no should clauses so it should be 0
assertThat(innerBooleanQuery.getMinimumNumberShouldMatch(), equalTo(0));
assertThat(innerBooleanQuery.clauses().size(), equalTo(1));
BooleanClause innerBooleanClause = innerBooleanQuery.clauses().get(0);
assertThat(innerBooleanClause.getOccur(), equalTo(BooleanClause.Occur.MUST));
assertThat(innerBooleanClause.occur(), equalTo(BooleanClause.Occur.MUST));
assertThat(innerBooleanClause.getQuery(), instanceOf(MatchAllDocsQuery.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected void doAssertLuceneQuery(ExistsQueryBuilder queryBuilder, Query query,
assertThat(booleanQuery.clauses().size(), equalTo(childFields.size()));
for (int i = 0; i < childFields.size(); i++) {
BooleanClause booleanClause = booleanQuery.clauses().get(i);
assertThat(booleanClause.getOccur(), equalTo(BooleanClause.Occur.SHOULD));
assertThat(booleanClause.occur(), equalTo(BooleanClause.Occur.SHOULD));
}
} else if (context.getMapperService().fieldType(field).hasDocValues()) {
assertThat(constantScoreQuery.getQuery(), instanceOf(DocValuesFieldExistsQuery.class));
Expand All @@ -115,7 +115,7 @@ protected void doAssertLuceneQuery(ExistsQueryBuilder queryBuilder, Query query,
assertThat(booleanQuery.clauses().size(), equalTo(mappedFields.size()));
for (int i = 0; i < mappedFields.size(); i++) {
BooleanClause booleanClause = booleanQuery.clauses().get(i);
assertThat(booleanClause.getOccur(), equalTo(BooleanClause.Occur.SHOULD));
assertThat(booleanClause.occur(), equalTo(BooleanClause.Occur.SHOULD));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected void doAssertLuceneQuery(MatchBoolPrefixQueryBuilder queryBuilder, Que
// all queries except the last should be TermQuery or SynonymQuery
final Set<Query> allQueriesExceptLast = IntStream.range(0, booleanQuery.clauses().size() - 1)
.mapToObj(booleanQuery.clauses()::get)
.map(BooleanClause::getQuery)
.map(BooleanClause::query)
.collect(Collectors.toSet());
assertThat(
allQueriesExceptLast,
Expand All @@ -147,13 +147,13 @@ protected void doAssertLuceneQuery(MatchBoolPrefixQueryBuilder queryBuilder, Que
});

// the last query should be PrefixQuery
final Query shouldBePrefixQuery = booleanQuery.clauses().get(booleanQuery.clauses().size() - 1).getQuery();
final Query shouldBePrefixQuery = booleanQuery.clauses().get(booleanQuery.clauses().size() - 1).query();
assertThat(shouldBePrefixQuery, instanceOf(PrefixQuery.class));

if (queryBuilder.minimumShouldMatch() != null) {
final int optionalClauses = (int) booleanQuery.clauses()
.stream()
.filter(clause -> clause.getOccur() == BooleanClause.Occur.SHOULD)
.filter(clause -> clause.occur() == BooleanClause.Occur.SHOULD)
.count();
final int expected = Queries.calculateMinShouldMatch(optionalClauses, queryBuilder.minimumShouldMatch());
assertThat(booleanQuery.getMinimumNumberShouldMatch(), equalTo(expected));
Expand Down Expand Up @@ -304,7 +304,7 @@ private static void assertBooleanQuery(Query actual, List<Query> expectedClauseQ
assertThat(actualBooleanQuery.clauses(), everyItem(hasProperty("occur", equalTo(BooleanClause.Occur.SHOULD))));

for (int i = 0; i < actualBooleanQuery.clauses().size(); i++) {
final Query clauseQuery = actualBooleanQuery.clauses().get(i).getQuery();
final Query clauseQuery = actualBooleanQuery.clauses().get(i).query();
assertThat(clauseQuery, equalTo(expectedClauseQueries.get(i)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.PhraseQuery;
Expand Down Expand Up @@ -192,7 +193,7 @@ protected void doAssertLuceneQuery(MatchQueryBuilder queryBuilder, Query query,
// calculate expected minimumShouldMatch value
int optionalClauses = 0;
for (BooleanClause c : bq.clauses()) {
if (c.getOccur() == BooleanClause.Occur.SHOULD) {
if (c.occur() == BooleanClause.Occur.SHOULD) {
optionalClauses++;
}
}
Expand Down Expand Up @@ -545,9 +546,9 @@ public void testAliasWithSynonyms() throws Exception {
public void testMaxBooleanClause() {
MatchQuery query = new MatchQuery(createShardContext());
query.setAnalyzer(new MockGraphAnalyzer(createGiantGraph(40)));
expectThrows(BooleanQuery.TooManyClauses.class, () -> query.parse(Type.PHRASE, TEXT_FIELD_NAME, ""));
expectThrows(IndexSearcher.TooManyClauses.class, () -> query.parse(Type.PHRASE, TEXT_FIELD_NAME, ""));
query.setAnalyzer(new MockGraphAnalyzer(createGiantGraphMultiTerms()));
expectThrows(BooleanQuery.TooManyClauses.class, () -> query.parse(Type.PHRASE, TEXT_FIELD_NAME, ""));
expectThrows(IndexSearcher.TooManyClauses.class, () -> query.parse(Type.PHRASE, TEXT_FIELD_NAME, ""));
}

private static class MockGraphAnalyzer extends Analyzer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,10 @@ public void testNestedDepthAllowed() throws Exception {
OpenSearchToParentBlockJoinQuery blockJoinQuery = (OpenSearchToParentBlockJoinQuery) queryBuilder.toQuery(context);
Optional<BooleanClause> childLeg = ((BooleanQuery) blockJoinQuery.getChildQuery()).clauses()
.stream()
.filter(c -> c.getOccur() == BooleanClause.Occur.MUST)
.filter(c -> c.occur() == BooleanClause.Occur.MUST)
.findFirst();
assertTrue(childLeg.isPresent());
assertEquals(new MatchAllDocsQuery(), childLeg.get().getQuery());
assertEquals(new MatchAllDocsQuery(), childLeg.get().query());
};
check.accept(createShardContext());
doWithDepth(randomIntBetween(1, 20), check);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -989,9 +989,9 @@ public void testToQueryBooleanQueryMultipleBoosts() throws Exception {
assertThat(query, instanceOf(BooleanQuery.class));
BooleanQuery booleanQuery = (BooleanQuery) query;
assertThat(booleanQuery.getMinimumNumberShouldMatch(), equalTo(2));
assertThat(booleanQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.SHOULD));
assertThat(booleanQuery.clauses().get(0).occur(), equalTo(BooleanClause.Occur.SHOULD));
assertThat(booleanQuery.clauses().get(0).getQuery(), equalTo(new TermQuery(new Term(TEXT_FIELD_NAME, "foo"))));
assertThat(booleanQuery.clauses().get(1).getOccur(), equalTo(BooleanClause.Occur.SHOULD));
assertThat(booleanQuery.clauses().get(1).occur(), equalTo(BooleanClause.Occur.SHOULD));
assertThat(booleanQuery.clauses().get(1).getQuery(), equalTo(new TermQuery(new Term(TEXT_FIELD_NAME, "bar"))));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ protected void doAssertLuceneQuery(SimpleQueryStringBuilder queryBuilder, Query
private static int shouldClauses(BooleanQuery query) {
int result = 0;
for (BooleanClause c : query.clauses()) {
if (c.getOccur() == BooleanClause.Occur.SHOULD) {
if (c.occur() == BooleanClause.Occur.SHOULD) {
result++;
}
}
Expand Down

0 comments on commit ce3af73

Please sign in to comment.