Fixed top children query bug reported in issue #2140

Fixed type.
This commit is contained in:
Martijn van Groningen 2012-08-06 13:20:32 +02:00 committed by Shay Banon
parent 346fb9ed83
commit 37e7a54b0e
2 changed files with 51 additions and 1 deletions

View File

@ -109,7 +109,6 @@ public class QueryPhase implements SearchPhase {
for (ScopePhase scopePhase : searchContext.scopePhases()) {
if (scopePhase instanceof ScopePhase.TopDocsPhase) {
ScopePhase.TopDocsPhase topDocsPhase = (ScopePhase.TopDocsPhase) scopePhase;
topDocsPhase.clear();
int numDocs = (searchContext.from() + searchContext.size());
if (numDocs == 0) {
numDocs = 1;
@ -117,6 +116,7 @@ public class QueryPhase implements SearchPhase {
try {
numDocs *= topDocsPhase.factor();
while (true) {
topDocsPhase.clear();
if (topDocsPhase.scope() != null) {
searchContext.searcher().processingScope(topDocsPhase.scope());
}

View File

@ -554,4 +554,54 @@ public class SimpleChildQuerySearchTests extends AbstractNodesTests {
.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();
client.admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).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();
int numberOfParents = 4;
int numberOfChildrenPerParent = 123;
for (int i = 1; i <= numberOfParents; i++) {
String parentId = String.format("p%d", i);
client.prepareIndex("test", "parent", parentId)
.setSource("p_field", String.format("p_value%d", i))
.execute()
.actionGet();
for (int j = 1; j <= numberOfChildrenPerParent; j++) {
client.prepareIndex("test", "child", String.format("%s_c%d", parentId, j))
.setSource(
"c_field1", parentId,
"c_field2", i % 2 == 0 ? "even" : "not_even"
).setParent(parentId)
.execute()
.actionGet();
}
}
client.admin().indices().prepareRefresh().execute().actionGet();
SearchResponse searchResponse = client.prepareSearch("test")
.setQuery(topChildrenQuery("child", termQuery("c_field1", "p3")))
.execute().actionGet();
assertThat("Failures " + Arrays.toString(searchResponse.shardFailures()), searchResponse.shardFailures().length, equalTo(0));
assertThat(searchResponse.failedShards(), equalTo(0));
assertThat(searchResponse.hits().totalHits(), equalTo(1l));
assertThat(searchResponse.hits().getAt(0).id(), equalTo("p3"));
searchResponse = client.prepareSearch("test")
.setQuery(topChildrenQuery("child", termQuery("c_field2", "even")))
.execute().actionGet();
assertThat("Failures " + Arrays.toString(searchResponse.shardFailures()), searchResponse.shardFailures().length, equalTo(0));
assertThat(searchResponse.failedShards(), equalTo(0));
assertThat(searchResponse.hits().totalHits(), equalTo(2l));
assertThat(searchResponse.hits().getAt(0).id(), equalTo("p2"));
}
}