Optimize search execution for suggest-only requests

We skip context preprocessing and only execute
suggest phase for suggest-only requests
This commit is contained in:
Areek Zillur 2016-03-18 14:02:34 -04:00
parent 5ed2bb5f18
commit b0437c3f22
4 changed files with 25 additions and 0 deletions

View File

@ -729,6 +729,14 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ
return ext;
}
/**
* @return true if the source only has suggest
*/
public boolean hasOnlySuggest() {
return suggestBuilder != null
&& queryBuilder == null && aggregations == null;
}
/**
* Rewrites this search source builder into its primitive form. e.g. by
* rewriting the QueryBuilder. If the builder did not change the identity

View File

@ -191,6 +191,9 @@ public class DefaultSearchContext extends SearchContext {
*/
@Override
public void preProcess() {
if (hasOnlySuggest() ) {
return;
}
if (scrollContext == null) {
long from = from() == -1 ? 0 : from();
long size = size() == -1 ? 10 : size();

View File

@ -353,6 +353,14 @@ public abstract class SearchContext implements Releasable {
}
}
/**
* @return true if the request contains only suggest
*/
public final boolean hasOnlySuggest() {
return request().source() != null
&& request().source().hasOnlySuggest();
}
/**
* Looks up the given field, but does not restrict to fields in the types set on this context.
*/

View File

@ -116,6 +116,12 @@ public class QueryPhase implements SearchPhase {
@Override
public void execute(SearchContext searchContext) throws QueryPhaseExecutionException {
if (searchContext.hasOnlySuggest()) {
suggestPhase.execute(searchContext);
// TODO: fix this once we can fetch docs for suggestions
searchContext.queryResult().topDocs(new TopDocs(0, Lucene.EMPTY_SCORE_DOCS, 0));
return;
}
// Pre-process aggregations as late as possible. In the case of a DFS_Q_T_F
// request, preProcess is called on the DFS phase phase, this is why we pre-process them
// here to make sure it happens during the QUERY phase