Skip to content

Commit 3ef5e4c

Browse files
authored
[Rest Compatible Api] include_type_name parameter (#70966)
This commit allows to use the include_type_name parameter with the compatible rest api. The support for include_type_name was previously removed in #48632 relates #51816 types removal meta issue #54160
1 parent f9c5f55 commit 3ef5e4c

File tree

21 files changed

+691
-133
lines changed

21 files changed

+691
-133
lines changed

rest-api-spec/build.gradle

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,9 @@ tasks.named("yamlRestCompatTest").configure {
146146
'explain/21_source_filtering_with_types/Source filtering',
147147
'explain/31_query_string_with_types/explain with query_string parameters',
148148
'explain/40_mix_typeless_typeful/Explain with typeless API on an index that has types',
149-
'field_caps/10_basic/Get date_nanos field caps',
150149
'field_caps/30_filter/Field caps with index filter',
151-
'get/100_mix_typeless_typeful/GET with typeless API on an index that has types',// failing due to include_type_name #48632
152-
'get/21_stored_fields_with_types/Stored fields', // failing due to include_type_name #48632
153-
'get/71_source_filtering_with_types/Source filtering',// failing due to include_type_name #48632
150+
// WILL NOT BE FIXED - failing due to not recognising missing type (the type path param is ignored)
151+
'get/100_mix_typeless_typeful/GET with typeless API on an index that has types',
154152
'get_source/11_basic_with_types/Basic with types',
155153
'get_source/16_default_values_with_types/Default values',
156154
'get_source/41_routing_with_types/Routing',
@@ -160,43 +158,35 @@ tasks.named("yamlRestCompatTest").configure {
160158
'get_source/81_missing_with_types/Missing document with ignore',
161159
'get_source/86_source_missing_with_types/Missing document source with catch',
162160
'get_source/86_source_missing_with_types/Missing document source with ignore',
163-
'index/70_mix_typeless_typeful/Index call that introduces new field mappings', // failing due to include_type_name #48632
164-
'index/70_mix_typeless_typeful/Index with typeless API on an index that has types', // failing due to include_type_name #48632
165161
'indices.clone/10_basic/Clone index via API',
166-
'indices.create/10_basic/Create index with explicit _doc type',
167162
'indices.create/10_basic/Create index without soft deletes',
168-
'indices.create/11_basic_with_types/Create index',
169-
'indices.create/11_basic_with_types/Create index with aliases',
163+
// 5 below await retrofitting Removes typed URLs from mapping APIs #41676
170164
'indices.create/11_basic_with_types/Create index with mappings',
171-
'indices.create/11_basic_with_types/Create index with settings',
172-
'indices.create/11_basic_with_types/Create index with wait_for_active_shards set to all',
173-
'indices.create/11_basic_with_types/Create index with write aliases',
174165
'indices.create/20_mix_typeless_typeful/Create a typed index while there is a typeless template',
175166
'indices.create/20_mix_typeless_typeful/Create a typeless index while there is a typed template',
176167
'indices.create/20_mix_typeless_typeful/Implicitly create a typed index while there is a typeless template',
177168
'indices.create/20_mix_typeless_typeful/Implicitly create a typeless index while there is a typed template',
169+
//
178170
'indices.flush/10_basic/Flush stats',
179171
'indices.flush/10_basic/Index synced flush rest test',
180172
'indices.forcemerge/10_basic/Check deprecation warning when incompatible only_expunge_deletes and max_num_segments values are both set',
181-
'indices.get/11_basic_with_types/Test include_type_name',
182-
'indices.get/11_basic_with_types/Test include_type_name dafaults to false',
183-
'indices.get_field_mapping/10_basic/Get field mapping with local is deprecated',
184-
'indices.get_field_mapping/11_basic_with_types/Get field mapping by index only',
185-
'indices.get_field_mapping/11_basic_with_types/Get field mapping by type & field',
186-
'indices.get_field_mapping/11_basic_with_types/Get field mapping by type & field, with another field that doesn\'t exist',
187-
'indices.get_field_mapping/11_basic_with_types/Get field mapping should work without index specifying type and fields',
188-
'indices.get_field_mapping/11_basic_with_types/Get field mapping with include_defaults',
189-
'indices.get_field_mapping/11_basic_with_types/Get field mapping with no index and type',
190-
'indices.get_field_mapping/21_missing_field_with_types/Return empty object if field doesn\'t exist, but type and index do',
191-
'indices.get_field_mapping/30_missing_type/Raise 404 when type doesn\'t exist',
192-
'indices.get_field_mapping/51_field_wildcards_with_types/Get field mapping should work using \'*\' for indices and types',
193-
'indices.get_field_mapping/51_field_wildcards_with_types/Get field mapping should work using \'_all\' for indices and types',
194-
'indices.get_field_mapping/51_field_wildcards_with_types/Get field mapping should work using comma_separated values for indices and types',
195-
'indices.get_field_mapping/51_field_wildcards_with_types/Get field mapping with * for fields',
196-
'indices.get_field_mapping/51_field_wildcards_with_types/Get field mapping with *t1 for fields',
197-
'indices.get_field_mapping/51_field_wildcards_with_types/Get field mapping with t* for fields',
198-
'indices.get_field_mapping/51_field_wildcards_with_types/Get field mapping with wildcarded relative names',
199-
'indices.get_field_mapping/60_mix_typeless_typeful/GET mapping with typeless API on an index that has types',
173+
'indices.get_field_mapping/10_basic/Get field mapping with local is deprecated',// awaits #41676
174+
'indices.get_field_mapping/11_basic_with_types/Get field mapping by index only',// awaits #41676
175+
'indices.get_field_mapping/11_basic_with_types/Get field mapping by type & field',// awaits #41676
176+
'indices.get_field_mapping/11_basic_with_types/Get field mapping by type & field, with another field that doesn\'t exist',// awaits #41676
177+
'indices.get_field_mapping/11_basic_with_types/Get field mapping should work without index specifying type and fields',// awaits #41676
178+
'indices.get_field_mapping/11_basic_with_types/Get field mapping with include_defaults',// awaits #41676
179+
'indices.get_field_mapping/11_basic_with_types/Get field mapping with no index and type',// awaits #41676
180+
'indices.get_field_mapping/21_missing_field_with_types/Return empty object if field doesn\'t exist, but type and index do',// awaits #41676
181+
'indices.get_field_mapping/30_missing_type/Raise 404 when type doesn\'t exist',// awaits #41676
182+
'indices.get_field_mapping/51_field_wildcards_with_types/Get field mapping should work using \'*\' for indices and types',// awaits #41676
183+
'indices.get_field_mapping/51_field_wildcards_with_types/Get field mapping should work using \'_all\' for indices and types',// awaits #41676
184+
'indices.get_field_mapping/51_field_wildcards_with_types/Get field mapping should work using comma_separated values for indices and types',// awaits #41676
185+
'indices.get_field_mapping/51_field_wildcards_with_types/Get field mapping with * for fields',// awaits #41676
186+
'indices.get_field_mapping/51_field_wildcards_with_types/Get field mapping with *t1 for fields',// awaits #41676
187+
'indices.get_field_mapping/51_field_wildcards_with_types/Get field mapping with t* for fields',// awaits #41676
188+
'indices.get_field_mapping/51_field_wildcards_with_types/Get field mapping with wildcarded relative names',// awaits #41676
189+
'indices.get_field_mapping/60_mix_typeless_typeful/GET mapping with typeless API on an index that has types',// awaits #41676
200190
'indices.get_mapping/11_basic_with_types/Get /*/_mapping/{type}',
201191
'indices.get_mapping/11_basic_with_types/Get /_all/_mapping/{type}',
202192
'indices.get_mapping/11_basic_with_types/Get /_mapping',
@@ -217,8 +207,6 @@ tasks.named("yamlRestCompatTest").configure {
217207
'indices.get_mapping/40_aliases/Getting mapping for aliases should return the real index as key',
218208
'indices.get_mapping/61_empty_with_types/Check empty mapping when getting all mappings via /_mapping',
219209
'indices.get_mapping/70_mix_typeless_typeful/GET mapping with typeless API on an index that has types',
220-
'indices.get_template/11_basic_with_types/Get template',
221-
'indices.get_template/11_basic_with_types/Get template with no mappings',
222210
'indices.open/10_basic/?wait_for_active_shards default is deprecated',
223211
'indices.open/10_basic/?wait_for_active_shards=index-setting',
224212
'indices.put_mapping/10_basic/Put mappings with explicit _doc type',
@@ -233,15 +221,9 @@ tasks.named("yamlRestCompatTest").configure {
233221
'indices.put_mapping/all_path_options_with_types/put mapping in prefix* index',
234222
'indices.put_mapping/all_path_options_with_types/put mapping with blank index',
235223
'indices.put_mapping/all_path_options_with_types/put one mapping per index',
236-
'indices.put_template/10_basic/Put template with explicit _doc type',
237-
'indices.put_template/11_basic_with_types/Put multiple template',
238-
'indices.put_template/11_basic_with_types/Put template',
224+
// there is a small distinction between empty mappings and no mappings at all. The code to implement this test was refactored #54003
225+
// not fixing this in #70966
239226
'indices.put_template/11_basic_with_types/Put template with empty mappings',
240-
'indices.rollover/10_basic/Rollover index via API',
241-
'indices.rollover/20_max_doc_condition/Max docs rollover conditions matches only primary shards',
242-
'indices.rollover/30_max_size_condition/Rollover with max_size condition',
243-
'indices.rollover/40_mapping/Mappings with explicit _doc type',
244-
'indices.rollover/41_mapping_with_types/Typeless mapping',
245227
'indices.segments/10_basic/basic segments test',
246228
'indices.segments/10_basic/closed segments test',
247229
'indices.shard_stores/10_basic/basic index test',
@@ -302,8 +284,6 @@ tasks.named("yamlRestCompatTest").configure {
302284
'search.aggregation/10_histogram/Deprecated _time order',
303285
'search.aggregation/200_top_hits_metric/top_hits aggregation with sequence numbers',
304286
'search.aggregation/20_terms/Deprecated _term order',
305-
'search.aggregation/280_geohash_grid/Basic test',
306-
'search.aggregation/290_geotile_grid/Basic test',
307287
'search.aggregation/51_filter_with_types/Filter aggs with terms lookup and ensure it\'s cached',
308288
'search.inner_hits/10_basic/Nested doc version and seqIDs',
309289
'search.inner_hits/10_basic/Nested inner hits',
@@ -363,6 +343,9 @@ tasks.named("transformV7RestTests").configure({ task ->
363343
task.replaceMatch("nodes.\$node_id.roles.8", "ml", "node_info role test")
364344
task.replaceMatch("nodes.\$node_id.roles.9", "remote_cluster_client", "node_info role test")
365345
task.removeMatch("nodes.\$node_id.roles.10", "node_info role test")
346+
task.replaceIsTrue("test_index.mappings.type_1", "test_index.mappings._doc")
347+
task.replaceIsFalse("test_index.mappings.type_1", "test_index.mappings._doc")
348+
task.replaceIsFalse("test-1.mappings.my_type", "test-1.mappings._doc")
366349
})
367350

368351
tasks.register('enforceYamlTestConvention').configure {

server/src/main/java/org/elasticsearch/action/admin/indices/get/GetIndexResponse.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.action.ActionResponse;
1313
import org.elasticsearch.cluster.metadata.AliasMetadata;
1414
import org.elasticsearch.cluster.metadata.MappingMetadata;
15+
import org.elasticsearch.common.RestApiVersion;
1516
import org.elasticsearch.common.Strings;
1617
import org.elasticsearch.common.collect.ImmutableOpenMap;
1718
import org.elasticsearch.common.io.stream.StreamInput;
@@ -26,6 +27,9 @@
2627
import java.util.List;
2728
import java.util.Objects;
2829

30+
import static org.elasticsearch.rest.BaseRestHandler.DEFAULT_INCLUDE_TYPE_NAME_POLICY;
31+
import static org.elasticsearch.rest.BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER;
32+
2933
/**
3034
* A response for a get index action.
3135
*/
@@ -192,7 +196,14 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
192196
if (indexMappings == null) {
193197
builder.startObject("mappings").endObject();
194198
} else {
195-
builder.field("mappings", indexMappings.sourceAsMap());
199+
if (builder.getRestApiVersion() == RestApiVersion.V_7 &&
200+
params.paramAsBoolean(INCLUDE_TYPE_NAME_PARAMETER, DEFAULT_INCLUDE_TYPE_NAME_POLICY)) {
201+
builder.startObject("mappings");
202+
builder.field(MapperService.SINGLE_MAPPING_NAME, indexMappings.sourceAsMap());
203+
builder.endObject();
204+
} else {
205+
builder.field("mappings", indexMappings.sourceAsMap());
206+
}
196207
}
197208

198209
builder.startObject("settings");

server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsResponse.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.Version;
1212
import org.elasticsearch.action.ActionResponse;
1313
import org.elasticsearch.common.ParseField;
14+
import org.elasticsearch.common.RestApiVersion;
1415
import org.elasticsearch.common.bytes.BytesReference;
1516
import org.elasticsearch.common.io.stream.StreamInput;
1617
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -22,6 +23,7 @@
2223
import org.elasticsearch.common.xcontent.XContentType;
2324
import org.elasticsearch.index.mapper.Mapper;
2425
import org.elasticsearch.index.mapper.MapperService;
26+
import org.elasticsearch.rest.BaseRestHandler;
2527

2628
import java.io.IOException;
2729
import java.io.InputStream;
@@ -30,6 +32,7 @@
3032
import java.util.Objects;
3133

3234
import static java.util.Collections.unmodifiableMap;
35+
import static org.elasticsearch.rest.BaseRestHandler.DEFAULT_INCLUDE_TYPE_NAME_POLICY;
3336

3437
/**
3538
* Response object for {@link GetFieldMappingsRequest} API
@@ -84,15 +87,23 @@ public FieldMappingMetadata fieldMappings(String index, String field) {
8487

8588
@Override
8689
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
90+
8791
builder.startObject();
8892
for (Map.Entry<String, Map<String, FieldMappingMetadata>> indexEntry : mappings.entrySet()) {
8993
builder.startObject(indexEntry.getKey());
9094
builder.startObject(MAPPINGS.getPreferredName());
91-
9295
if (indexEntry.getValue() != null) {
93-
addFieldMappingsToBuilder(builder, params, indexEntry.getValue());
96+
if (builder.getRestApiVersion() == RestApiVersion.V_7 &&
97+
params.paramAsBoolean(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, DEFAULT_INCLUDE_TYPE_NAME_POLICY)) {
98+
builder.startObject(MapperService.SINGLE_MAPPING_NAME);
99+
addFieldMappingsToBuilder(builder, params, indexEntry.getValue());
100+
builder.endObject();
101+
} else {
102+
addFieldMappingsToBuilder(builder, params, indexEntry.getValue());
103+
}
94104
}
95105

106+
96107
builder.endObject();
97108
builder.endObject();
98109
}

server/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequest.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.action.support.IndicesOptions;
1515
import org.elasticsearch.action.support.master.AcknowledgedRequest;
1616
import org.elasticsearch.common.ParseField;
17+
import org.elasticsearch.common.RestApiVersion;
1718
import org.elasticsearch.common.io.stream.StreamInput;
1819
import org.elasticsearch.common.io.stream.StreamOutput;
1920
import org.elasticsearch.common.unit.ByteSizeValue;
@@ -37,7 +38,7 @@
3738
*/
3839
public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implements IndicesRequest {
3940

40-
private static final ObjectParser<RolloverRequest, Void> PARSER = new ObjectParser<>("rollover");
41+
private static final ObjectParser<RolloverRequest, Boolean> PARSER = new ObjectParser<>("rollover");
4142
private static final ObjectParser<Map<String, Condition<?>>, Void> CONDITION_PARSER = new ObjectParser<>("conditions");
4243

4344
private static final ParseField CONDITIONS = new ParseField("conditions");
@@ -67,14 +68,32 @@ public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implem
6768
CONDITIONS, ObjectParser.ValueType.OBJECT);
6869
PARSER.declareField((parser, request, context) -> request.createIndexRequest.settings(parser.map()),
6970
CreateIndexRequest.SETTINGS, ObjectParser.ValueType.OBJECT);
71+
PARSER.declareField((parser, request, includeTypeName) -> {
72+
if (includeTypeName) {
73+
//expecting one type only
74+
for (Map.Entry<String, Object> mappingsEntry : parser.map().entrySet()) {
75+
request.createIndexRequest.mapping((Map<String, Object>) mappingsEntry.getValue());
76+
}
77+
} else {
78+
// a type is not included, add a dummy _doc type
79+
Map<String, Object> mappings = parser.map();
80+
if (MapperService.isMappingSourceTyped(MapperService.SINGLE_MAPPING_NAME, mappings)) {
81+
throw new IllegalArgumentException("The mapping definition cannot be nested under a type " +
82+
"[" + MapperService.SINGLE_MAPPING_NAME + "] unless include_type_name is set to true.");
83+
}
84+
request.createIndexRequest.mapping(mappings);
85+
}
86+
}, CreateIndexRequest.MAPPINGS.forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.V_7)), ObjectParser.ValueType.OBJECT);
7087
PARSER.declareField((parser, request, context) -> {
7188
// a type is not included, add a dummy _doc type
7289
Map<String, Object> mappings = parser.map();
7390
if (MapperService.isMappingSourceTyped(MapperService.SINGLE_MAPPING_NAME, mappings)) {
91+
7492
throw new IllegalArgumentException("The mapping definition cannot be nested under a type");
7593
}
7694
request.createIndexRequest.mapping(mappings);
77-
}, CreateIndexRequest.MAPPINGS, ObjectParser.ValueType.OBJECT);
95+
}, CreateIndexRequest.MAPPINGS.forRestApiVersion(RestApiVersion.onOrAfter(RestApiVersion.V_8)), ObjectParser.ValueType.OBJECT);
96+
7897
PARSER.declareField((parser, request, context) -> request.createIndexRequest.aliases(parser.map()),
7998
CreateIndexRequest.ALIASES, ObjectParser.ValueType.OBJECT);
8099
}
@@ -238,7 +257,7 @@ public CreateIndexRequest getCreateIndexRequest() {
238257
}
239258

240259
// param isTypeIncluded decides how mappings should be parsed from XContent
241-
public void fromXContent(XContentParser parser) throws IOException {
242-
PARSER.parse(parser, this, null);
260+
public void fromXContent(boolean isTypeIncluded, XContentParser parser) throws IOException {
261+
PARSER.parse(parser, this, isTypeIncluded);
243262
}
244263
}

server/src/main/java/org/elasticsearch/action/admin/indices/template/get/GetIndexTemplatesResponse.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import org.elasticsearch.action.ActionResponse;
1111
import org.elasticsearch.cluster.metadata.IndexTemplateMetadata;
12+
import org.elasticsearch.common.RestApiVersion;
1213
import org.elasticsearch.common.io.stream.StreamInput;
1314
import org.elasticsearch.common.io.stream.StreamOutput;
1415
import org.elasticsearch.common.xcontent.ToXContent;
@@ -20,6 +21,7 @@
2021
import java.util.Objects;
2122

2223
import static java.util.Collections.singletonMap;
24+
import static org.elasticsearch.rest.BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER;
2325

2426
public class GetIndexTemplatesResponse extends ActionResponse implements ToXContentObject {
2527

@@ -65,7 +67,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
6567

6668
builder.startObject();
6769
for (IndexTemplateMetadata indexTemplateMetadata : getIndexTemplates()) {
68-
IndexTemplateMetadata.Builder.toXContent(indexTemplateMetadata, builder, params);
70+
if(builder.getRestApiVersion() == RestApiVersion.V_7 &&
71+
params.paramAsBoolean(INCLUDE_TYPE_NAME_PARAMETER, false)) {
72+
IndexTemplateMetadata.Builder.toXContentWithTypes(indexTemplateMetadata, builder, params);
73+
} else {
74+
IndexTemplateMetadata.Builder.toXContent(indexTemplateMetadata, builder, params);
75+
}
6976
}
7077
builder.endObject();
7178
return builder;

server/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateMetadata.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
import java.util.Objects;
3838
import java.util.Set;
3939

40+
import static org.elasticsearch.common.RestApiVersion.V_8;
41+
import static org.elasticsearch.common.RestApiVersion.onOrAfter;
42+
43+
4044
public class IndexTemplateMetadata extends AbstractDiffable<IndexTemplateMetadata> {
4145

4246
private final String name;
@@ -378,7 +382,9 @@ private static void toInnerXContent(IndexTemplateMetadata indexTemplateMetadata,
378382
indexTemplateMetadata.settings().toXContent(builder, params);
379383
builder.endObject();
380384

381-
includeTypeName &= (params.paramAsBoolean("reduce_mappings", false) == false);
385+
if(builder.getRestApiVersion().matches(onOrAfter(V_8))) {
386+
includeTypeName &= (params.paramAsBoolean("reduce_mappings", false) == false);
387+
}
382388

383389
CompressedXContent m = indexTemplateMetadata.mappings();
384390
if (m != null) {

0 commit comments

Comments
 (0)