Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use QueryVisitor when extracting PercolatorQuery list for highlighting #53728

Merged
merged 2 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@

import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.ReaderUtil;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.DisjunctionMaxQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.document.DocumentField;
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.FetchSubPhase;
Expand Down Expand Up @@ -139,33 +134,18 @@ public void hitsExecute(SearchContext context, SearchHit[] hits) throws IOExcept
}

static List<PercolateQuery> locatePercolatorQuery(Query query) {
if (query instanceof PercolateQuery) {
return Collections.singletonList((PercolateQuery) query);
} else if (query instanceof BooleanQuery) {
List<PercolateQuery> percolateQueries = new ArrayList<>();
for (BooleanClause clause : ((BooleanQuery) query).clauses()) {
List<PercolateQuery> result = locatePercolatorQuery(clause.getQuery());
if (result.isEmpty() == false) {
percolateQueries.addAll(result);
}
}
return percolateQueries;
} else if (query instanceof DisjunctionMaxQuery) {
List<PercolateQuery> percolateQueries = new ArrayList<>();
for (Query disjunct : ((DisjunctionMaxQuery) query).getDisjuncts()) {
List<PercolateQuery> result = locatePercolatorQuery(disjunct);
if (result.isEmpty() == false) {
percolateQueries.addAll(result);
if (query == null) {
return Collections.emptyList();
}
List<PercolateQuery> queries = new ArrayList<>();
query.visit(new QueryVisitor() {
@Override
public void visitLeaf(Query query) {
if (query instanceof PercolateQuery) {
queries.add((PercolateQuery)query);
}
}
return percolateQueries;
} else if (query instanceof ConstantScoreQuery) {
return locatePercolatorQuery(((ConstantScoreQuery) query).getQuery());
} else if (query instanceof BoostQuery) {
return locatePercolatorQuery(((BoostQuery) query).getQuery());
} else if (query instanceof FunctionScoreQuery) {
return locatePercolatorQuery(((FunctionScoreQuery) query).getSubQuery());
}
return Collections.emptyList();
});
return queries;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Collections;

import static java.util.Collections.emptyMap;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
Expand Down Expand Up @@ -99,8 +100,11 @@ public void testLocatePercolatorQuery() {
bq.add(percolateQuery, BooleanClause.Occur.FILTER);
bq.add(percolateQuery2, BooleanClause.Occur.FILTER);
assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()).size(), equalTo(2));
assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()).get(0), sameInstance(percolateQuery));
assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()).get(1), sameInstance(percolateQuery2));
assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()),
containsInAnyOrder(sameInstance(percolateQuery), sameInstance(percolateQuery2)));

assertNotNull(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(null));
assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(null).size(), equalTo(0));
}

}