move sort optimization when sorting by _score to sort parse element from the query phase execution
This commit is contained in:
parent
3c88eacb01
commit
a4e4235d93
|
@ -176,20 +176,6 @@ public class QueryPhase implements SearchPhase {
|
||||||
// if 0 was asked, change it to 1 since 0 is not allowed
|
// if 0 was asked, change it to 1 since 0 is not allowed
|
||||||
numDocs = 1;
|
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) {
|
if (searchContext.searchType() == SearchType.COUNT) {
|
||||||
CountCollector countCollector = new CountCollector();
|
CountCollector countCollector = new CountCollector();
|
||||||
|
@ -207,7 +193,7 @@ public class QueryPhase implements SearchPhase {
|
||||||
// all is well
|
// all is well
|
||||||
}
|
}
|
||||||
topDocs = scanCollector.topDocs();
|
topDocs = scanCollector.topDocs();
|
||||||
} else if (sort) {
|
} else if (searchContext.sort() != null) {
|
||||||
topDocs = searchContext.searcher().search(query, null, numDocs, searchContext.sort());
|
topDocs = searchContext.searcher().search(query, null, numDocs, searchContext.sort());
|
||||||
} else {
|
} else {
|
||||||
topDocs = searchContext.searcher().search(query, numDocs);
|
topDocs = searchContext.searcher().search(query, numDocs);
|
||||||
|
|
|
@ -76,9 +76,23 @@ public class SortParseElement implements SearchParseElement {
|
||||||
addCompoundSortField(parser, context, sortFields);
|
addCompoundSortField(parser, context, sortFields);
|
||||||
}
|
}
|
||||||
if (!sortFields.isEmpty()) {
|
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()])));
|
context.sort(new Sort(sortFields.toArray(new SortField[sortFields.size()])));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void addCompoundSortField(XContentParser parser, SearchContext context, List<SortField> sortFields) throws Exception {
|
private void addCompoundSortField(XContentParser parser, SearchContext context, List<SortField> sortFields) throws Exception {
|
||||||
XContentParser.Token token;
|
XContentParser.Token token;
|
||||||
|
|
Loading…
Reference in New Issue