diff --git a/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java b/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java index 57fe74e112a..54a8117a759 100644 --- a/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java @@ -284,6 +284,10 @@ public class TopChildrenQuery extends Query implements ScopePhase.TopDocsPhase { @Override public int docID() { + if (index == -1) { + return -1; + } + if (index >= docs.length) { return NO_MORE_DOCS; } diff --git a/src/test/java/org/elasticsearch/test/integration/search/child/SimpleChildQuerySearchTests.java b/src/test/java/org/elasticsearch/test/integration/search/child/SimpleChildQuerySearchTests.java index e3505f7928f..c62a3606aa6 100644 --- a/src/test/java/org/elasticsearch/test/integration/search/child/SimpleChildQuerySearchTests.java +++ b/src/test/java/org/elasticsearch/test/integration/search/child/SimpleChildQuerySearchTests.java @@ -32,9 +32,11 @@ import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; -import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Maps.newHashMap; import static org.elasticsearch.index.query.FilterBuilders.hasChildFilter; import static org.elasticsearch.index.query.FilterBuilders.hasParentFilter; @@ -695,6 +697,32 @@ public class SimpleChildQuerySearchTests extends AbstractNodesTests { assertThat("Failures " + Arrays.toString(searchResponse.shardFailures()), searchResponse.shardFailures().length, equalTo(0)); } + @Test + public void testFixAOBEIfTopChildrenIswrappedInMusNotClause() throws Exception { + client.admin().indices().prepareDelete().execute().actionGet(); + + client.admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)).execute().actionGet(); + client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet(); + client.admin().indices().preparePutMapping("test").setType("child").setSource(XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("_parent").field("type", "parent").endObject() + .endObject().endObject()).execute().actionGet(); + + // index simple data + client.prepareIndex("test", "parent", "p1").setSource("p_field", "p_value1").execute().actionGet(); + client.prepareIndex("test", "child", "c1").setSource("c_field", "red").setParent("p1").execute().actionGet(); + client.prepareIndex("test", "child", "c2").setSource("c_field", "yellow").setParent("p1").execute().actionGet(); + client.prepareIndex("test", "parent", "p2").setSource("p_field", "p_value2").execute().actionGet(); + client.prepareIndex("test", "child", "c3").setSource("c_field", "blue").setParent("p2").execute().actionGet(); + client.prepareIndex("test", "child", "c4").setSource("c_field", "red").setParent("p2").execute().actionGet(); + + client.admin().indices().prepareRefresh().execute().actionGet(); + + SearchResponse searchResponse = client.prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH) + .setQuery(boolQuery().mustNot(topChildrenQuery("child", boolQuery().should(queryString("c_field:*"))))) + .execute().actionGet(); + assertThat("Failures " + Arrays.toString(searchResponse.shardFailures()), searchResponse.shardFailures().length, equalTo(0)); + } + @Test public void testTopChildrenReSearchBug() throws Exception { client.admin().indices().prepareDelete().execute().actionGet(); @@ -754,19 +782,19 @@ public class SimpleChildQuerySearchTests extends AbstractNodesTests { client.admin().indices().prepareDelete().execute().actionGet(); client.admin().indices().prepareCreate("test").setSettings( - ImmutableSettings.settingsBuilder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - ).execute().actionGet(); + ImmutableSettings.settingsBuilder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + ).execute().actionGet(); client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet(); client.admin().indices().preparePutMapping("test").setType("child").setSource( XContentFactory.jsonBuilder() .startObject() - .startObject("type") - .startObject("_parent") - .field("type", "parent") - .endObject() - . endObject() + .startObject("type") + .startObject("_parent") + .field("type", "parent") + .endObject() + .endObject() .endObject() ).execute().actionGet();