diff --git a/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java index a74db00d1f7..37d06f79eb2 100644 --- a/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java @@ -473,7 +473,10 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder QueryBuilder rewrittenQuery = query.rewrite(queryRewriteContext); if (rewrittenQuery != query) { InnerHitBuilder rewrittenInnerHit = InnerHitBuilder.rewrite(innerHitBuilder, rewrittenQuery); - return new NestedQueryBuilder(path, rewrittenQuery, scoreMode, rewrittenInnerHit); + NestedQueryBuilder nestedQuery = new NestedQueryBuilder(path, rewrittenQuery, scoreMode, rewrittenInnerHit); + nestedQuery.ignoreUnmapped(ignoreUnmapped); + return nestedQuery; } return this; } diff --git a/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java index e2c8e8b292c..ac5c93604d8 100644 --- a/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java @@ -331,4 +331,15 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase failingQueryBuilder.toQuery(createShardContext())); assertThat(e.getMessage(), containsString("[" + HasChildQueryBuilder.NAME + "] no mapping found for type [unmapped]")); } + + public void testIgnoreUnmappedWithRewrite() throws IOException { + // WrapperQueryBuilder makes sure we always rewrite + final HasChildQueryBuilder queryBuilder + = new HasChildQueryBuilder("unmapped", new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), ScoreMode.None); + queryBuilder.ignoreUnmapped(true); + QueryShardContext queryShardContext = createShardContext(); + Query query = queryBuilder.rewrite(queryShardContext).toQuery(queryShardContext); + assertThat(query, notNullValue()); + assertThat(query, instanceOf(MatchNoDocsQuery.class)); + } } diff --git a/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java index ffcfe701dce..7e0fc30aa75 100644 --- a/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java @@ -213,4 +213,15 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase failingQueryBuilder.toQuery(createShardContext())); assertThat(e.getMessage(), containsString("[" + NestedQueryBuilder.NAME + "] failed to find nested object under path [unmapped]")); } + + public void testIgnoreUnmappedWithRewrite() throws IOException { + // WrapperQueryBuilder makes sure we always rewrite + final NestedQueryBuilder queryBuilder = + new NestedQueryBuilder("unmapped", new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), ScoreMode.None); + queryBuilder.ignoreUnmapped(true); + QueryShardContext queryShardContext = createShardContext(); + Query query = queryBuilder.rewrite(queryShardContext).toQuery(queryShardContext); + assertThat(query, notNullValue()); + assertThat(query, instanceOf(MatchNoDocsQuery.class)); + } }