diff --git a/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java b/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java index eae12def5e4..0093363d244 100644 --- a/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java @@ -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 diff --git a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java index 71a289331f8..f3fe48f6682 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java @@ -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(); diff --git a/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java index 1881109c9b6..65d41a85227 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java @@ -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. */ diff --git a/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java b/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java index f40f253e3ae..62210655a00 100644 --- a/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java +++ b/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java @@ -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