diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/controller/SearchPhaseController.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/controller/SearchPhaseController.java index 87a36d8d885..49c18697970 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/controller/SearchPhaseController.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/controller/SearchPhaseController.java @@ -20,7 +20,11 @@ package org.elasticsearch.search.controller; import org.apache.lucene.index.Term; -import org.apache.lucene.search.*; +import org.apache.lucene.search.FieldDoc; +import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.ShardFieldDocSortedHitQueue; +import org.apache.lucene.search.SortField; +import org.apache.lucene.search.TopFieldDocs; import org.apache.lucene.util.PriorityQueue; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.collect.Iterables; @@ -256,7 +260,13 @@ public class SearchPhaseController extends AbstractComponent { boolean sorted = false; int sortScoreIndex = -1; - QuerySearchResult querySearchResult = Iterables.get(queryResults.values(), 0).queryResult(); + QuerySearchResult querySearchResult; + try { + querySearchResult = Iterables.get(queryResults.values(), 0).queryResult(); + } catch (IndexOutOfBoundsException e) { + // no results, return an empty response + return InternalSearchResponse.EMPTY; + } if (querySearchResult.topDocs() instanceof TopFieldDocs) { sorted = true; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchResponse.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchResponse.java index 040c526749c..aaf476150fb 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchResponse.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchResponse.java @@ -43,6 +43,8 @@ public class InternalSearchResponse implements Streamable, ToXContent { private boolean timedOut; + public static final InternalSearchResponse EMPTY = new InternalSearchResponse(new InternalSearchHits(new InternalSearchHit[0], 0, 0), null, false); + private InternalSearchResponse() { }