move sort optimization when sorting by _score to sort parse element from the query phase execution

This commit is contained in:
Shay Banon 2011-12-31 16:24:27 +02:00
parent 3c88eacb01
commit a4e4235d93
2 changed files with 16 additions and 16 deletions

View File

@ -176,20 +176,6 @@ public class QueryPhase implements SearchPhase {
// if 0 was asked, change it to 1 since 0 is not allowed
numDocs = 1;
}
boolean sort = false;
// try and optimize for a case where the sorting is based on score, this is how we work by default!
if (searchContext.sort() != null) {
if (searchContext.sort().getSort().length > 1) {
sort = true;
} else {
SortField sortField = searchContext.sort().getSort()[0];
if (sortField.getType() == SortField.SCORE && !sortField.getReverse()) {
sort = false;
} else {
sort = true;
}
}
}
if (searchContext.searchType() == SearchType.COUNT) {
CountCollector countCollector = new CountCollector();
@ -207,7 +193,7 @@ public class QueryPhase implements SearchPhase {
// all is well
}
topDocs = scanCollector.topDocs();
} else if (sort) {
} else if (searchContext.sort() != null) {
topDocs = searchContext.searcher().search(query, null, numDocs, searchContext.sort());
} else {
topDocs = searchContext.searcher().search(query, numDocs);

View File

@ -76,9 +76,23 @@ public class SortParseElement implements SearchParseElement {
addCompoundSortField(parser, context, sortFields);
}
if (!sortFields.isEmpty()) {
// optimize if we just sort on score non reversed, we don't really need sorting
boolean sort;
if (sortFields.size() > 1) {
sort = true;
} else {
SortField sortField = sortFields.get(0);
if (sortField.getType() == SortField.SCORE && !sortField.getReverse()) {
sort = false;
} else {
sort = true;
}
}
if (sort) {
context.sort(new Sort(sortFields.toArray(new SortField[sortFields.size()])));
}
}
}
private void addCompoundSortField(XContentParser parser, SearchContext context, List<SortField> sortFields) throws Exception {
XContentParser.Token token;