Merge branch 'pr/16923'
This commit is contained in:
commit
7a3da93551
|
@ -132,7 +132,7 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
|
|||
}
|
||||
DocumentMapper parentDocMapper = context.getMapperService().documentMapper(type);
|
||||
if (parentDocMapper == null) {
|
||||
throw new QueryShardException(context, "[has_parent] query configured 'parent_type' [" + type
|
||||
throw new QueryShardException(context, "[" + NAME + "] query configured 'parent_type' [" + type
|
||||
+ "] is not a valid type");
|
||||
}
|
||||
|
||||
|
@ -152,49 +152,36 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
|
|||
}
|
||||
}
|
||||
|
||||
Set<String> parentTypes = new HashSet<>(5);
|
||||
parentTypes.add(parentDocMapper.type());
|
||||
Set<String> childTypes = new HashSet<>();
|
||||
ParentChildIndexFieldData parentChildIndexFieldData = null;
|
||||
for (DocumentMapper documentMapper : context.getMapperService().docMappers(false)) {
|
||||
ParentFieldMapper parentFieldMapper = documentMapper.parentFieldMapper();
|
||||
if (parentFieldMapper.active()) {
|
||||
DocumentMapper parentTypeDocumentMapper = context.getMapperService().documentMapper(parentFieldMapper.type());
|
||||
if (parentFieldMapper.active() && type.equals(parentFieldMapper.type())) {
|
||||
childTypes.add(documentMapper.type());
|
||||
parentChildIndexFieldData = context.getForField(parentFieldMapper.fieldType());
|
||||
if (parentTypeDocumentMapper == null) {
|
||||
// Only add this, if this parentFieldMapper (also a parent) isn't a child of another parent.
|
||||
parentTypes.add(parentFieldMapper.type());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (parentChildIndexFieldData == null) {
|
||||
throw new QueryShardException(context, "[has_parent] no _parent field configured");
|
||||
}
|
||||
|
||||
Query parentTypeQuery = null;
|
||||
if (parentTypes.size() == 1) {
|
||||
DocumentMapper documentMapper = context.getMapperService().documentMapper(parentTypes.iterator().next());
|
||||
if (documentMapper != null) {
|
||||
parentTypeQuery = documentMapper.typeFilter();
|
||||
}
|
||||
if (childTypes.isEmpty()) {
|
||||
throw new QueryShardException(context, "[" + NAME + "] no child types found for type [" + type + "]");
|
||||
}
|
||||
|
||||
Query childrenQuery;
|
||||
if (childTypes.size() == 1) {
|
||||
DocumentMapper documentMapper = context.getMapperService().documentMapper(childTypes.iterator().next());
|
||||
childrenQuery = documentMapper.typeFilter();
|
||||
} else {
|
||||
BooleanQuery.Builder parentsFilter = new BooleanQuery.Builder();
|
||||
for (String parentTypeStr : parentTypes) {
|
||||
DocumentMapper documentMapper = context.getMapperService().documentMapper(parentTypeStr);
|
||||
if (documentMapper != null) {
|
||||
parentsFilter.add(documentMapper.typeFilter(), BooleanClause.Occur.SHOULD);
|
||||
}
|
||||
BooleanQuery.Builder childrenFilter = new BooleanQuery.Builder();
|
||||
for (String childrenTypeStr : childTypes) {
|
||||
DocumentMapper documentMapper = context.getMapperService().documentMapper(childrenTypeStr);
|
||||
childrenFilter.add(documentMapper.typeFilter(), BooleanClause.Occur.SHOULD);
|
||||
}
|
||||
parentTypeQuery = parentsFilter.build();
|
||||
}
|
||||
|
||||
if (parentTypeQuery == null) {
|
||||
return null;
|
||||
childrenQuery = childrenFilter.build();
|
||||
}
|
||||
|
||||
// wrap the query with type query
|
||||
innerQuery = Queries.filtered(innerQuery, parentDocMapper.typeFilter());
|
||||
Query childrenFilter = Queries.not(parentTypeQuery);
|
||||
return new HasChildQueryBuilder.LateParsingQuery(childrenFilter,
|
||||
return new HasChildQueryBuilder.LateParsingQuery(childrenQuery,
|
||||
innerQuery,
|
||||
HasChildQueryBuilder.DEFAULT_MIN_CHILDREN,
|
||||
HasChildQueryBuilder.DEFAULT_MAX_CHILDREN,
|
||||
|
|
|
@ -73,6 +73,8 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase<HasParentQ
|
|||
DATE_FIELD_NAME, "type=date",
|
||||
OBJECT_FIELD_NAME, "type=object"
|
||||
).string()), MapperService.MergeReason.MAPPING_UPDATE, false);
|
||||
mapperService.merge("just_a_type", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("just_a_type"
|
||||
).string()), MapperService.MergeReason.MAPPING_UPDATE, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -132,20 +134,26 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase<HasParentQ
|
|||
}
|
||||
}
|
||||
|
||||
public void testIllegalValues() {
|
||||
public void testIllegalValues() throws IOException {
|
||||
QueryBuilder query = RandomQueryBuilder.createQuery(random());
|
||||
try {
|
||||
new HasParentQueryBuilder(null, query);
|
||||
fail("must not be null");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
|
||||
}
|
||||
|
||||
try {
|
||||
new HasParentQueryBuilder("foo", null);
|
||||
fail("must not be null");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
}
|
||||
|
||||
QueryShardContext context = createShardContext();
|
||||
HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder("just_a_type", new MatchAllQueryBuilder());
|
||||
try {
|
||||
queryBuilder.doToQuery(context);
|
||||
} catch (QueryShardException e) {
|
||||
assertThat(e.getMessage(), equalTo("[has_parent] no child types found for type [just_a_type]"));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -758,11 +758,11 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||
assertNoFailures(response);
|
||||
assertThat(response.getHits().totalHits(), equalTo(0L));
|
||||
|
||||
response = client().prepareSearch("test").setQuery(QueryBuilders.hasParentQuery("child", matchQuery("text", "value"))).get();
|
||||
response = client().prepareSearch("test").setQuery(QueryBuilders.hasParentQuery("parent", matchQuery("text", "value"))).get();
|
||||
assertNoFailures(response);
|
||||
assertThat(response.getHits().totalHits(), equalTo(0L));
|
||||
|
||||
response = client().prepareSearch("test").setQuery(QueryBuilders.hasParentQuery("child", matchQuery("text", "value")).score(true))
|
||||
response = client().prepareSearch("test").setQuery(QueryBuilders.hasParentQuery("parent", matchQuery("text", "value")).score(true))
|
||||
.get();
|
||||
assertNoFailures(response);
|
||||
assertThat(response.getHits().totalHits(), equalTo(0L));
|
||||
|
@ -1894,11 +1894,6 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||
fail();
|
||||
} catch (SearchPhaseExecutionException e) {
|
||||
}
|
||||
|
||||
SearchResponse response = client().prepareSearch("test")
|
||||
.setQuery(QueryBuilders.hasParentQuery("parent", matchAllQuery()))
|
||||
.get();
|
||||
assertHitCount(response, 0);
|
||||
}
|
||||
|
||||
static HasChildQueryBuilder hasChildQuery(String type, QueryBuilder queryBuilder) {
|
||||
|
|
Loading…
Reference in New Issue