Merge branch 'pr/16923'

This commit is contained in:
Martijn van Groningen 2016-03-03 15:58:38 +01:00
commit 7a3da93551
3 changed files with 30 additions and 40 deletions

View File

@ -132,7 +132,7 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
} }
DocumentMapper parentDocMapper = context.getMapperService().documentMapper(type); DocumentMapper parentDocMapper = context.getMapperService().documentMapper(type);
if (parentDocMapper == null) { 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"); + "] is not a valid type");
} }
@ -152,49 +152,36 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
} }
} }
Set<String> parentTypes = new HashSet<>(5); Set<String> childTypes = new HashSet<>();
parentTypes.add(parentDocMapper.type());
ParentChildIndexFieldData parentChildIndexFieldData = null; ParentChildIndexFieldData parentChildIndexFieldData = null;
for (DocumentMapper documentMapper : context.getMapperService().docMappers(false)) { for (DocumentMapper documentMapper : context.getMapperService().docMappers(false)) {
ParentFieldMapper parentFieldMapper = documentMapper.parentFieldMapper(); ParentFieldMapper parentFieldMapper = documentMapper.parentFieldMapper();
if (parentFieldMapper.active()) { if (parentFieldMapper.active() && type.equals(parentFieldMapper.type())) {
DocumentMapper parentTypeDocumentMapper = context.getMapperService().documentMapper(parentFieldMapper.type()); childTypes.add(documentMapper.type());
parentChildIndexFieldData = context.getForField(parentFieldMapper.fieldType()); 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 (childTypes.isEmpty()) {
if (parentTypes.size() == 1) { throw new QueryShardException(context, "[" + NAME + "] no child types found for type [" + type + "]");
DocumentMapper documentMapper = context.getMapperService().documentMapper(parentTypes.iterator().next()); }
if (documentMapper != null) {
parentTypeQuery = documentMapper.typeFilter(); Query childrenQuery;
} if (childTypes.size() == 1) {
DocumentMapper documentMapper = context.getMapperService().documentMapper(childTypes.iterator().next());
childrenQuery = documentMapper.typeFilter();
} else { } else {
BooleanQuery.Builder parentsFilter = new BooleanQuery.Builder(); BooleanQuery.Builder childrenFilter = new BooleanQuery.Builder();
for (String parentTypeStr : parentTypes) { for (String childrenTypeStr : childTypes) {
DocumentMapper documentMapper = context.getMapperService().documentMapper(parentTypeStr); DocumentMapper documentMapper = context.getMapperService().documentMapper(childrenTypeStr);
if (documentMapper != null) { childrenFilter.add(documentMapper.typeFilter(), BooleanClause.Occur.SHOULD);
parentsFilter.add(documentMapper.typeFilter(), BooleanClause.Occur.SHOULD);
}
} }
parentTypeQuery = parentsFilter.build(); childrenQuery = childrenFilter.build();
}
if (parentTypeQuery == null) {
return null;
} }
// wrap the query with type query // wrap the query with type query
innerQuery = Queries.filtered(innerQuery, parentDocMapper.typeFilter()); innerQuery = Queries.filtered(innerQuery, parentDocMapper.typeFilter());
Query childrenFilter = Queries.not(parentTypeQuery); return new HasChildQueryBuilder.LateParsingQuery(childrenQuery,
return new HasChildQueryBuilder.LateParsingQuery(childrenFilter,
innerQuery, innerQuery,
HasChildQueryBuilder.DEFAULT_MIN_CHILDREN, HasChildQueryBuilder.DEFAULT_MIN_CHILDREN,
HasChildQueryBuilder.DEFAULT_MAX_CHILDREN, HasChildQueryBuilder.DEFAULT_MAX_CHILDREN,

View File

@ -73,6 +73,8 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase<HasParentQ
DATE_FIELD_NAME, "type=date", DATE_FIELD_NAME, "type=date",
OBJECT_FIELD_NAME, "type=object" OBJECT_FIELD_NAME, "type=object"
).string()), MapperService.MergeReason.MAPPING_UPDATE, false); ).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 @Override
@ -132,20 +134,26 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase<HasParentQ
} }
} }
public void testIllegalValues() { public void testIllegalValues() throws IOException {
QueryBuilder query = RandomQueryBuilder.createQuery(random()); QueryBuilder query = RandomQueryBuilder.createQuery(random());
try { try {
new HasParentQueryBuilder(null, query); new HasParentQueryBuilder(null, query);
fail("must not be null"); fail("must not be null");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
} }
try { try {
new HasParentQueryBuilder("foo", null); new HasParentQueryBuilder("foo", null);
fail("must not be null"); fail("must not be null");
} catch (IllegalArgumentException ex) { } 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]"));
} }
} }

View File

@ -758,11 +758,11 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
assertNoFailures(response); assertNoFailures(response);
assertThat(response.getHits().totalHits(), equalTo(0L)); 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); assertNoFailures(response);
assertThat(response.getHits().totalHits(), equalTo(0L)); 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(); .get();
assertNoFailures(response); assertNoFailures(response);
assertThat(response.getHits().totalHits(), equalTo(0L)); assertThat(response.getHits().totalHits(), equalTo(0L));
@ -1894,11 +1894,6 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
fail(); fail();
} catch (SearchPhaseExecutionException e) { } catch (SearchPhaseExecutionException e) {
} }
SearchResponse response = client().prepareSearch("test")
.setQuery(QueryBuilders.hasParentQuery("parent", matchAllQuery()))
.get();
assertHitCount(response, 0);
} }
static HasChildQueryBuilder hasChildQuery(String type, QueryBuilder queryBuilder) { static HasChildQueryBuilder hasChildQuery(String type, QueryBuilder queryBuilder) {