From e218ead19ed1677fb453f4a75759e2abfd75094a Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Thu, 6 Jun 2013 17:13:49 +0200 Subject: [PATCH] ChildrenQuery and ParentQuery now take into account documents that have been marked. Closes #3144 --- .../index/search/child/ChildrenQuery.java | 3 +- .../index/search/child/ParentQuery.java | 3 +- .../child/SimpleChildQuerySearchTests.java | 81 +++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java b/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java index 85f47f84e56..7e53b9e9ab8 100644 --- a/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java @@ -32,6 +32,7 @@ import org.apache.lucene.util.ToStringUtils; import org.elasticsearch.ElasticSearchIllegalStateException; import org.elasticsearch.common.CacheRecycler; import org.elasticsearch.common.bytes.HashedBytesArray; +import org.elasticsearch.common.lucene.search.ApplyAcceptedDocsFilter; import org.elasticsearch.index.cache.id.IdReaderTypeCache; import org.elasticsearch.search.internal.SearchContext; @@ -65,7 +66,7 @@ public class ChildrenQuery extends Query implements SearchContext.Rewrite { this.searchContext = searchContext; this.parentType = parentType; this.childType = childType; - this.parentFilter = parentFilter; + this.parentFilter = new ApplyAcceptedDocsFilter(parentFilter); this.originalChildQuery = childQuery; this.scoreType = scoreType; } diff --git a/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java b/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java index be2fabb282b..5c605eda85a 100644 --- a/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java @@ -30,6 +30,7 @@ import org.elasticsearch.ElasticSearchIllegalStateException; import org.elasticsearch.common.CacheRecycler; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.HashedBytesArray; +import org.elasticsearch.common.lucene.search.ApplyAcceptedDocsFilter; import org.elasticsearch.common.lucene.search.NoopCollector; import org.elasticsearch.index.cache.id.IdReaderTypeCache; import org.elasticsearch.search.internal.SearchContext; @@ -57,7 +58,7 @@ public class ParentQuery extends Query implements SearchContext.Rewrite { this.searchContext = searchContext; this.originalParentQuery = parentQuery; this.parentType = parentType; - this.childrenFilter = childrenFilter; + this.childrenFilter = new ApplyAcceptedDocsFilter(childrenFilter); } private ParentQuery(ParentQuery unwritten, Query rewrittenParentQuery) { 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 f31e5b07099..186c94476e7 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 @@ -31,6 +31,7 @@ import org.elasticsearch.search.facet.terms.TermsFacet; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.integration.AbstractSharedClusterTest; +import org.hamcrest.Matchers; import org.testng.annotations.Test; import java.io.IOException; @@ -1318,4 +1319,84 @@ public class SimpleChildQuerySearchTests extends AbstractSharedClusterTest { } } + @Test + // See also issue: https://github.com/elasticsearch/elasticsearch/issues/3144 + public void testReIndexingParentAndChildDocuments() 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().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet(); + client().admin().indices().preparePutMapping("test").setType("child").setSource(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", "x").setParent("p2").execute().actionGet(); + client().prepareIndex("test", "child", "c4").setSource("c_field", "x").setParent("p2").execute().actionGet(); + + client().admin().indices().prepareRefresh("test").execute().actionGet(); + + SearchResponse searchResponse = client().prepareSearch("test") + .setQuery(hasChildQuery("child", termQuery("c_field", "yellow")).scoreType("sum")) + .execute().actionGet(); + assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0)); + assertThat(searchResponse.getFailedShards(), equalTo(0)); + assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1")); + assertThat(searchResponse.getHits().getAt(0).sourceAsString(), containsString("\"p_value1\"")); + + searchResponse = client().prepareSearch("test") + .setQuery( + boolQuery() + .must(matchQuery("c_field", "x")) + .must(hasParentQuery("parent", termQuery("p_field", "p_value2")).scoreType("score")) + ) + .execute().actionGet(); + assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0)); + assertThat(searchResponse.getFailedShards(), equalTo(0)); + assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); + assertThat(searchResponse.getHits().getAt(0).id(), equalTo("c3")); + assertThat(searchResponse.getHits().getAt(1).id(), equalTo("c4")); + + // re-index + for (int i = 0; i < 10; i++) { + client().prepareIndex("test", "parent", "p1").setSource("p_field", "p_value1").execute().actionGet(); + client().prepareIndex("test", "child", "d" + i).setSource("c_field", "red").setParent("p1").execute().actionGet(); + client().prepareIndex("test", "parent", "p2").setSource("p_field", "p_value2").execute().actionGet(); + client().prepareIndex("test", "child", "c3").setSource("c_field", "x").setParent("p2").execute().actionGet(); + client().admin().indices().prepareRefresh("test").execute().actionGet(); + } + + searchResponse = client().prepareSearch("test") + .setQuery(hasChildQuery("child", termQuery("c_field", "yellow")).scoreType("sum")) + .execute().actionGet(); + assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0)); + assertThat(searchResponse.getFailedShards(), equalTo(0)); + assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1")); + assertThat(searchResponse.getHits().getAt(0).sourceAsString(), containsString("\"p_value1\"")); + + searchResponse = client().prepareSearch("test") + .setQuery( + boolQuery() + .must(matchQuery("c_field", "x")) + .must(hasParentQuery("parent", termQuery("p_field", "p_value2")).scoreType("score")) + ) + .execute().actionGet(); + assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0)); + assertThat(searchResponse.getFailedShards(), equalTo(0)); + assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); + assertThat(searchResponse.getHits().getAt(0).id(), Matchers.anyOf(equalTo("c3"), equalTo("c4"))); + assertThat(searchResponse.getHits().getAt(1).id(), Matchers.anyOf(equalTo("c3"), equalTo("c4"))); + } + }