Propagate ignore_unmapped to inner_hits (#29261)

In 5.2 `ignore_unmapped` was added to `inner_hits` in order to ignore invalid mapping.
This value was automatically set to the value defined in the parent query (`nested`, `has_child`, `has_parent`) but the refactoring of the parent/child in 5.6 removed this behavior unintentionally.
This commit restores this behavior but also makes sure that we always automatically enforce this value when the query builder is used directly (previously this was only done by the XContent deserialization).

Closes #29071
This commit is contained in:
Jim Ferenczi 2018-03-27 18:55:42 +02:00 committed by GitHub
parent dfc9e721d8
commit 2aaa057387
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 6 deletions

View File

@ -167,6 +167,7 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
public HasChildQueryBuilder innerHit(InnerHitBuilder innerHit) {
this.innerHitBuilder = innerHit;
innerHitBuilder.setIgnoreUnmapped(ignoreUnmapped);
return this;
}
@ -212,6 +213,9 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
*/
public HasChildQueryBuilder ignoreUnmapped(boolean ignoreUnmapped) {
this.ignoreUnmapped = ignoreUnmapped;
if (innerHitBuilder!= null ){
innerHitBuilder.setIgnoreUnmapped(ignoreUnmapped);
}
return this;
}
@ -291,7 +295,6 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
hasChildQueryBuilder.ignoreUnmapped(ignoreUnmapped);
if (innerHitBuilder != null) {
hasChildQueryBuilder.innerHit(innerHitBuilder);
hasChildQueryBuilder.ignoreUnmapped(ignoreUnmapped);
}
return hasChildQueryBuilder;
}

View File

@ -145,6 +145,7 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
public HasParentQueryBuilder innerHit(InnerHitBuilder innerHit) {
this.innerHitBuilder = innerHit;
innerHitBuilder.setIgnoreUnmapped(ignoreUnmapped);
return this;
}
@ -155,6 +156,9 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
*/
public HasParentQueryBuilder ignoreUnmapped(boolean ignoreUnmapped) {
this.ignoreUnmapped = ignoreUnmapped;
if (innerHitBuilder != null) {
innerHitBuilder.setIgnoreUnmapped(ignoreUnmapped);
}
return this;
}

View File

@ -158,8 +158,7 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase<HasChildQue
hqb.innerHit(new InnerHitBuilder()
.setName(randomAlphaOfLengthBetween(1, 10))
.setSize(randomIntBetween(0, 100))
.addSort(new FieldSortBuilder(STRING_FIELD_NAME_2).order(SortOrder.ASC))
.setIgnoreUnmapped(hqb.ignoreUnmapped()));
.addSort(new FieldSortBuilder(STRING_FIELD_NAME_2).order(SortOrder.ASC)));
}
return hqb;
}
@ -345,13 +344,19 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase<HasChildQue
public void testIgnoreUnmapped() throws IOException {
final HasChildQueryBuilder queryBuilder = new HasChildQueryBuilder("unmapped", new MatchAllQueryBuilder(), ScoreMode.None);
queryBuilder.innerHit(new InnerHitBuilder());
assertFalse(queryBuilder.innerHit().isIgnoreUnmapped());
queryBuilder.ignoreUnmapped(true);
assertTrue(queryBuilder.innerHit().isIgnoreUnmapped());
Query query = queryBuilder.toQuery(createShardContext());
assertThat(query, notNullValue());
assertThat(query, instanceOf(MatchNoDocsQuery.class));
final HasChildQueryBuilder failingQueryBuilder = new HasChildQueryBuilder("unmapped", new MatchAllQueryBuilder(), ScoreMode.None);
failingQueryBuilder.innerHit(new InnerHitBuilder());
assertFalse(failingQueryBuilder.innerHit().isIgnoreUnmapped());
failingQueryBuilder.ignoreUnmapped(false);
assertFalse(failingQueryBuilder.innerHit().isIgnoreUnmapped());
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext()));
assertThat(e.getMessage(), containsString("[" + HasChildQueryBuilder.NAME +
"] join field [join_field] doesn't hold [unmapped] as a child"));

View File

@ -132,8 +132,7 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase<HasParentQ
hqb.innerHit(new InnerHitBuilder()
.setName(randomAlphaOfLengthBetween(1, 10))
.setSize(randomIntBetween(0, 100))
.addSort(new FieldSortBuilder(STRING_FIELD_NAME_2).order(SortOrder.ASC))
.setIgnoreUnmapped(hqb.ignoreUnmapped()));
.addSort(new FieldSortBuilder(STRING_FIELD_NAME_2).order(SortOrder.ASC)));
}
return hqb;
}
@ -245,13 +244,19 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase<HasParentQ
public void testIgnoreUnmapped() throws IOException {
final HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder("unmapped", new MatchAllQueryBuilder(), false);
queryBuilder.innerHit(new InnerHitBuilder());
assertFalse(queryBuilder.innerHit().isIgnoreUnmapped());
queryBuilder.ignoreUnmapped(true);
assertTrue(queryBuilder.innerHit().isIgnoreUnmapped());
Query query = queryBuilder.toQuery(createShardContext());
assertThat(query, notNullValue());
assertThat(query, instanceOf(MatchNoDocsQuery.class));
final HasParentQueryBuilder failingQueryBuilder = new HasParentQueryBuilder("unmapped", new MatchAllQueryBuilder(), false);
failingQueryBuilder.innerHit(new InnerHitBuilder());
assertFalse(failingQueryBuilder.innerHit().isIgnoreUnmapped());
failingQueryBuilder.ignoreUnmapped(false);
assertFalse(failingQueryBuilder.innerHit().isIgnoreUnmapped());
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext()));
assertThat(e.getMessage(),
containsString("[has_parent] join field [join_field] doesn't hold [unmapped] as a parent"));

View File

@ -132,6 +132,7 @@ public class NestedQueryBuilder extends AbstractQueryBuilder<NestedQueryBuilder>
public NestedQueryBuilder innerHit(InnerHitBuilder innerHitBuilder) {
this.innerHitBuilder = innerHitBuilder;
innerHitBuilder.setIgnoreUnmapped(ignoreUnmapped);
return this;
}
@ -149,6 +150,9 @@ public class NestedQueryBuilder extends AbstractQueryBuilder<NestedQueryBuilder>
*/
public NestedQueryBuilder ignoreUnmapped(boolean ignoreUnmapped) {
this.ignoreUnmapped = ignoreUnmapped;
if (innerHitBuilder != null) {
innerHitBuilder.setIgnoreUnmapped(ignoreUnmapped);
}
return this;
}

View File

@ -347,7 +347,8 @@ public class NestedQueryBuilderTests extends AbstractQueryTestCase<NestedQueryBu
});
innerHitBuilders.clear();
NestedQueryBuilder query2 = new NestedQueryBuilder("path", new MatchAllQueryBuilder(), ScoreMode.None);
query2.innerHit(leafInnerHits.setIgnoreUnmapped(true));
query2.ignoreUnmapped(true);
query2.innerHit(leafInnerHits);
query2.extractInnerHitBuilders(innerHitBuilders);
assertThat(innerHitBuilders.size(), Matchers.equalTo(1));
assertTrue(innerHitBuilders.containsKey(leafInnerHits.getName()));