Skip to content

Commit

Permalink
inner hits: Protected against specifying a size larger than the total…
Browse files Browse the repository at this point in the history
… amount of documents in an index.

Closes #13394
  • Loading branch information
martijnvg committed Sep 9, 2015
1 parent 319b272 commit 703a3ef
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Expand Up @@ -124,7 +124,7 @@ public TopDocs topDocs(SearchContext context, FetchSubPhase.HitContext hitContex
if (size() == 0) {
return new TopDocs(context.searcher().count(q), Lucene.EMPTY_SCORE_DOCS, 0);
} else {
int topN = from() + size();
int topN = Math.min(from() + size(), context.searcher().getIndexReader().maxDoc());
TopDocsCollector topDocsCollector;
if (sort() != null) {
try {
Expand Down Expand Up @@ -299,7 +299,7 @@ public TopDocs topDocs(SearchContext context, FetchSubPhase.HitContext hitContex
final int count = context.searcher().count(q);
return new TopDocs(count, Lucene.EMPTY_SCORE_DOCS, 0);
} else {
int topN = from() + size();
int topN = Math.min(from() + size(), context.searcher().getIndexReader().maxDoc());
TopDocsCollector topDocsCollector;
if (sort() != null) {
topDocsCollector = TopFieldCollector.create(sort(), topN, true, trackScores(), trackScores());
Expand Down
Expand Up @@ -19,6 +19,8 @@

package org.elasticsearch.search.innerhits;

import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.ArrayUtil;
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.action.index.IndexRequestBuilder;
Expand Down Expand Up @@ -1131,4 +1133,38 @@ public void matchesQueries_parentChildInnerHits() throws Exception {
assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name2"));
}

@Test
public void testDontExplode() throws Exception {
assertAcked(prepareCreate("index1").addMapping("child", "_parent", "type=parent"));
List<IndexRequestBuilder> requests = new ArrayList<>();
requests.add(client().prepareIndex("index1", "parent", "1").setSource("{}"));
requests.add(client().prepareIndex("index1", "child", "1").setParent("1").setSource("field", "value1"));
indexRandom(true, requests);

SearchResponse response = client().prepareSearch("index1")
.setQuery(hasChildQuery("child", matchQuery("field", "value1")).innerHit(new QueryInnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1)))
.addSort("_uid", SortOrder.ASC)
.get();
assertNoFailures(response);
assertHitCount(response, 1);

assertAcked(prepareCreate("index2").addMapping("type", "nested", "type=nested"));
client().prepareIndex("index2", "type", "1").setSource(jsonBuilder().startObject()
.startArray("nested")
.startObject()
.field("field", "value1")
.endObject()
.endArray()
.endObject())
.setRefresh(true)
.get();

response = client().prepareSearch("index2")
.setQuery(nestedQuery("nested", matchQuery("nested.field", "value1")).innerHit(new QueryInnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1)))
.addSort("_uid", SortOrder.ASC)
.get();
assertNoFailures(response);
assertHitCount(response, 1);
}

}

0 comments on commit 703a3ef

Please sign in to comment.