Skip to content

Commit 7e60cf3

Browse files
authored
Move parent_id query to the parent-join module (#25072)
This change moves the parent_id query to the parent-join module and handles the case when only the parent-join field can be declared on an index (index with single type on). If single type is off it uses the legacy parent join field mapper and switch to the new one otherwise (default in 6). Relates #20257
1 parent 7ec39ac commit 7e60cf3

File tree

21 files changed

+360
-98
lines changed

21 files changed

+360
-98
lines changed

core/src/main/java/org/elasticsearch/index/query/QueryBuilders.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -471,14 +471,6 @@ public static MoreLikeThisQueryBuilder moreLikeThisQuery(Item[] likeItems) {
471471
return moreLikeThisQuery(null, null, likeItems);
472472
}
473473

474-
/**
475-
* Constructs a new parent id query that returns all child documents of the specified type that
476-
* point to the specified id.
477-
*/
478-
public static ParentIdQueryBuilder parentId(String type, String id) {
479-
return new ParentIdQueryBuilder(type, id);
480-
}
481-
482474
public static NestedQueryBuilder nestedQuery(String path, QueryBuilder query, ScoreMode scoreMode) {
483475
return new NestedQueryBuilder(path, query, scoreMode);
484476
}

core/src/main/java/org/elasticsearch/search/SearchModule.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import org.elasticsearch.index.query.MoreLikeThisQueryBuilder;
5353
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
5454
import org.elasticsearch.index.query.NestedQueryBuilder;
55-
import org.elasticsearch.index.query.ParentIdQueryBuilder;
5655
import org.elasticsearch.index.query.PrefixQueryBuilder;
5756
import org.elasticsearch.index.query.QueryBuilder;
5857
import org.elasticsearch.index.query.QueryParseContext;
@@ -743,7 +742,6 @@ private void registerQueryParsers(List<SearchPlugin> plugins) {
743742
registerQuery(new QuerySpec<>(GeoPolygonQueryBuilder.NAME, GeoPolygonQueryBuilder::new, GeoPolygonQueryBuilder::fromXContent));
744743
registerQuery(new QuerySpec<>(ExistsQueryBuilder.NAME, ExistsQueryBuilder::new, ExistsQueryBuilder::fromXContent));
745744
registerQuery(new QuerySpec<>(MatchNoneQueryBuilder.NAME, MatchNoneQueryBuilder::new, MatchNoneQueryBuilder::fromXContent));
746-
registerQuery(new QuerySpec<>(ParentIdQueryBuilder.NAME, ParentIdQueryBuilder::new, ParentIdQueryBuilder::fromXContent));
747745

748746
if (ShapesAvailability.JTS_AVAILABLE && ShapesAvailability.SPATIAL4J_AVAILABLE) {
749747
registerQuery(new QuerySpec<>(GeoShapeQueryBuilder.NAME, GeoShapeQueryBuilder::new, GeoShapeQueryBuilder::fromXContent));

core/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ public void testExceptionUsingAnalyzerOnNumericField() {
430430

431431
@Override
432432
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
433-
mapperService.merge("t_boost", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("t_boost",
433+
mapperService.merge("doc", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("doc",
434434
"string_boost", "type=text,boost=4").string()), MapperService.MergeReason.MAPPING_UPDATE, false);
435435
}
436436

core/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private static String[] randomStringFields() {
9797

9898
private Item generateRandomItem() {
9999
String index = randomBoolean() ? getIndex().getName() : null;
100-
String type = getRandomType(); // set to one type to avoid ambiguous types
100+
String type = "doc";
101101
// indexed item or artificial document
102102
Item item;
103103
if (randomBoolean()) {

core/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class NestedQueryBuilderTests extends AbstractQueryTestCase<NestedQueryBu
5555

5656
@Override
5757
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
58-
mapperService.merge("nested_doc", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("nested_doc",
58+
mapperService.merge("doc", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("doc",
5959
STRING_FIELD_NAME, "type=text",
6060
INT_FIELD_NAME, "type=integer",
6161
DOUBLE_FIELD_NAME, "type=double",

core/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -858,19 +858,19 @@ public void testExistsFieldQuery() throws Exception {
858858

859859
public void testDisabledFieldNamesField() throws Exception {
860860
QueryShardContext context = createShardContext();
861-
context.getMapperService().merge("new_type",
861+
context.getMapperService().merge("doc",
862862
new CompressedXContent(
863-
PutMappingRequest.buildFromSimplifiedDef("new_type",
863+
PutMappingRequest.buildFromSimplifiedDef("doc",
864864
"foo", "type=text",
865865
"_field_names", "enabled=false").string()),
866866
MapperService.MergeReason.MAPPING_UPDATE, true);
867867
QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder("foo:*");
868868
Query query = queryBuilder.toQuery(context);
869869
Query expected = new WildcardQuery(new Term("foo", "*"));
870870
assertThat(query, equalTo(expected));
871-
context.getMapperService().merge("new_type",
871+
context.getMapperService().merge("doc",
872872
new CompressedXContent(
873-
PutMappingRequest.buildFromSimplifiedDef("new_type",
873+
PutMappingRequest.buildFromSimplifiedDef("doc",
874874
"foo", "type=text",
875875
"_field_names", "enabled=true").string()),
876876
MapperService.MergeReason.MAPPING_UPDATE, true);

core/src/test/java/org/elasticsearch/index/query/SpanTermQueryBuilderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void testParseFailsWithMultipleFields() throws IOException {
133133

134134
public void testWithMetaDataField() throws IOException {
135135
QueryShardContext context = createShardContext();
136-
for (String field : new String[]{"_type", "_all"}) {
136+
for (String field : new String[]{"field1", "field2"}) {
137137
SpanTermQueryBuilder spanTermQueryBuilder = new SpanTermQueryBuilder(field, "toto");
138138
Query query = spanTermQueryBuilder.toQuery(context);
139139
Query expected = new SpanTermQuery(new Term(field, "toto"));

core/src/test/java/org/elasticsearch/index/query/TypeQueryBuilderTests.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.index.query;
2121

22+
import org.apache.lucene.search.MatchAllDocsQuery;
2223
import org.apache.lucene.search.MatchNoDocsQuery;
2324
import org.apache.lucene.search.Query;
2425
import org.apache.lucene.util.BytesRef;
@@ -28,19 +29,27 @@
2829

2930
import java.io.IOException;
3031

32+
import static org.hamcrest.Matchers.anyOf;
33+
import static org.hamcrest.Matchers.equalTo;
34+
35+
3136
public class TypeQueryBuilderTests extends AbstractQueryTestCase<TypeQueryBuilder> {
3237

3338
@Override
3439
protected TypeQueryBuilder doCreateTestQueryBuilder() {
35-
return new TypeQueryBuilder(getRandomType());
40+
return new TypeQueryBuilder("doc");
3641
}
3742

3843
@Override
3944
protected void doAssertLuceneQuery(TypeQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
4045
if (createShardContext().getMapperService().documentMapper(queryBuilder.type()) == null) {
4146
assertEquals(new MatchNoDocsQuery(), query);
4247
} else {
43-
assertEquals(new TypeFieldMapper.TypesQuery(new BytesRef(queryBuilder.type())), query);
48+
assertThat(query,
49+
anyOf(
50+
equalTo(new TypeFieldMapper.TypesQuery(new BytesRef(queryBuilder.type()))),
51+
equalTo(new MatchAllDocsQuery()))
52+
);
4453
}
4554
}
4655

core/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void testIllegalArguments() {
8989
public void testEmptyValue() throws IOException {
9090
QueryShardContext context = createShardContext();
9191
context.setAllowUnmappedFields(true);
92-
WildcardQueryBuilder wildcardQueryBuilder = new WildcardQueryBuilder(getRandomType(), "");
92+
WildcardQueryBuilder wildcardQueryBuilder = new WildcardQueryBuilder("doc", "");
9393
assertEquals(wildcardQueryBuilder.toQuery(context).getClass(), WildcardQuery.class);
9494
}
9595

@@ -129,7 +129,7 @@ public void testParseFailsWithMultipleFields() throws IOException {
129129

130130
public void testWithMetaDataField() throws IOException {
131131
QueryShardContext context = createShardContext();
132-
for (String field : new String[]{"_type", "_all"}) {
132+
for (String field : new String[]{"field1", "field2"}) {
133133
WildcardQueryBuilder wildcardQueryBuilder = new WildcardQueryBuilder(field, "toto");
134134
Query query = wildcardQueryBuilder.toQuery(context);
135135
Query expected = new WildcardQuery(new Term(field, "toto"));

core/src/test/java/org/elasticsearch/search/SearchModuleTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ public List<PipelineAggregationSpec> getPipelineAggregations() {
279279
"more_like_this",
280280
"multi_match",
281281
"nested",
282-
"parent_id",
283282
"prefix",
284283
"query_string",
285284
"range",

0 commit comments

Comments
 (0)