diff --git a/core/src/main/java/org/elasticsearch/action/search/AbstractSearchAsyncAction.java b/core/src/main/java/org/elasticsearch/action/search/AbstractSearchAsyncAction.java index f9103f0cddc..a61384f9595 100644 --- a/core/src/main/java/org/elasticsearch/action/search/AbstractSearchAsyncAction.java +++ b/core/src/main/java/org/elasticsearch/action/search/AbstractSearchAsyncAction.java @@ -322,11 +322,8 @@ abstract class AbstractSearchAsyncAction // we only release search context that we did not fetch from if we are not scrolling if (request.scroll() == null) { for (AtomicArray.Entry entry : queryResults.asList()) { - QuerySearchResult queryResult = entry.value.queryResult().queryResult(); - final TopDocs topDocs = queryResult.topDocs(); - final Suggest suggest = queryResult.suggest(); - if (((topDocs != null && topDocs.scoreDocs.length > 0) // the shard had matches - ||suggest != null && suggest.hasScoreDocs()) // or had suggest docs + QuerySearchResult queryResult = entry.value.queryResult(); + if (queryResult.hasHits() && docIdsToLoad.get(entry.index) == null) { // but none of them made it to the global top docs try { DiscoveryNode node = nodes.get(entry.value.queryResult().shardTarget().nodeId()); diff --git a/core/src/main/java/org/elasticsearch/search/SearchService.java b/core/src/main/java/org/elasticsearch/search/SearchService.java index 4d618eb057a..dce1048a724 100644 --- a/core/src/main/java/org/elasticsearch/search/SearchService.java +++ b/core/src/main/java/org/elasticsearch/search/SearchService.java @@ -269,7 +269,7 @@ public class SearchService extends AbstractLifecycleComponent implements IndexEv loadOrExecuteQueryPhase(request, context); - if (hasHits(context.queryResult()) == false && context.scrollContext() == null) { + if (context.queryResult().hasHits() == false && context.scrollContext() == null) { freeContext(context.id()); } else { contextProcessedSuccessfully(context); @@ -324,7 +324,7 @@ public class SearchService extends AbstractLifecycleComponent implements IndexEv operationListener.onPreQueryPhase(context); long time = System.nanoTime(); queryPhase.execute(context); - if (hasHits(context.queryResult()) == false && context.scrollContext() == null) { + if (context.queryResult().hasHits() == false && context.scrollContext() == null) { // no hits, we can release the context since there will be no fetch phase freeContext(context.id()); } else { @@ -861,11 +861,6 @@ public class SearchService extends AbstractLifecycleComponent implements IndexEv context.docIdsToLoad(docIdsToLoad, 0, docIdsToLoad.length); } - private static boolean hasHits(final QuerySearchResult searchResult) { - return searchResult.topDocs().scoreDocs.length > 0 || - (searchResult.suggest() != null && searchResult.suggest().hasScoreDocs()); - } - private void processScroll(InternalScrollSearchRequest request, SearchContext context) { // process scroll context.from(context.from() + context.size()); diff --git a/core/src/main/java/org/elasticsearch/search/controller/SearchPhaseController.java b/core/src/main/java/org/elasticsearch/search/controller/SearchPhaseController.java index 97f3b191aa9..411e5593229 100644 --- a/core/src/main/java/org/elasticsearch/search/controller/SearchPhaseController.java +++ b/core/src/main/java/org/elasticsearch/search/controller/SearchPhaseController.java @@ -181,7 +181,7 @@ public class SearchPhaseController extends AbstractComponent { } else { // lets see if we only got hits from a single shard, if so, we can optimize... for (AtomicArray.Entry entry : results) { - if (entry.value.queryResult().topDocs().scoreDocs.length > 0) { + if (entry.value.queryResult().hasHits()) { if (result != null) { // we already have one, can't really optimize canOptimize = false; break; diff --git a/core/src/main/java/org/elasticsearch/search/query/QuerySearchResult.java b/core/src/main/java/org/elasticsearch/search/query/QuerySearchResult.java index 5125950a412..e583cfbf13e 100644 --- a/core/src/main/java/org/elasticsearch/search/query/QuerySearchResult.java +++ b/core/src/main/java/org/elasticsearch/search/query/QuerySearchResult.java @@ -188,6 +188,12 @@ public class QuerySearchResult extends QuerySearchResultProvider { return this; } + /** Returns true iff the result has hits */ + public boolean hasHits() { + return (topDocs != null && topDocs.scoreDocs.length > 0) || + (suggest != null && suggest.hasScoreDocs()); + } + public static QuerySearchResult readQuerySearchResult(StreamInput in) throws IOException { QuerySearchResult result = new QuerySearchResult(); result.readFrom(in);