Optimize scrolls for constant-score queries.

We currently optimize scroll when sort=_doc because docs are returned in order.
But documents are also returned in order when sorting by score and the query
gives constant scores. This optimization has the nice side-effect of also
optimizing scrolls with the default `match_all` query.
This commit is contained in:
Adrien Grand 2015-09-03 16:09:58 +02:00
parent 4b0182932a
commit 745b977ce7
1 changed files with 13 additions and 1 deletions

View File

@ -112,6 +112,18 @@ public class QueryPhase implements SearchPhase {
aggregationPhase.execute(searchContext);
}
private static boolean returnsDocsInOrder(Query query, Sort sort) {
if (sort == null || Sort.RELEVANCE.equals(sort)) {
// sort by score
// queries that return constant scores will return docs in index
// order since Lucene tie-breaks on the doc id
return query.getClass() == ConstantScoreQuery.class
|| query.getClass() == MatchAllDocsQuery.class;
} else {
return Sort.INDEXORDER.equals(sort);
}
}
/**
* In a package-private method so that it can be tested without having to
* wire everything (mapperService, etc.)
@ -165,7 +177,7 @@ public class QueryPhase implements SearchPhase {
numDocs = Math.min(searchContext.size(), totalNumDocs);
lastEmittedDoc = scrollContext.lastEmittedDoc;
if (Sort.INDEXORDER.equals(searchContext.sort())) {
if (returnsDocsInOrder(query, searchContext.sort())) {
if (scrollContext.totalHits == -1) {
// first round
assert scrollContext.lastEmittedDoc == null;