Skip to content

Commit 8bbda23

Browse files
committed
Add include_type_name to the put index templates API.
1 parent 324213c commit 8bbda23

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed

rest-api-spec/src/main/resources/rest-api-spec/api/indices.put_template.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
}
1414
},
1515
"params": {
16+
"include_type_name": {
17+
"type" : "boolean",
18+
"description" : "Whether a type should be returned in the body of the mappings."
19+
},
1620
"order": {
1721
"type" : "number",
1822
"description" : "The order for this template when merging multiple matching ones (higher numbers are merged later, overriding the lower numbers)"

rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_template/10_basic.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,87 @@
11
---
22
"Put template":
3+
- skip:
4+
version: " - 6.99.99"
5+
reason: include_type_name is not supported before 7.0.0
36

47
- do:
58
indices.put_template:
9+
include_type_name: false
610
name: test
711
body:
812
index_patterns: test-*
913
settings:
1014
number_of_shards: 1
1115
number_of_replicas: 0
16+
mappings:
17+
properties:
18+
field:
19+
type: keyword
1220

1321
- do:
1422
indices.get_template:
23+
include_type_name: false
1524
name: test
1625
flat_settings: true
1726

1827
- match: {test.index_patterns: ["test-*"]}
1928
- match: {test.settings: {index.number_of_shards: '1', index.number_of_replicas: '0'}}
29+
- match: {test.mappings: {properties: {field: {type: keyword}}}}
2030

2131
---
2232
"Put multiple template":
33+
- skip:
34+
version: " - 6.99.99"
35+
reason: include_type_name was introduced in 7.0.0
2336

2437
- do:
2538
indices.put_template:
39+
include_type_name: false
2640
name: test
2741
body:
2842
index_patterns: [test-*, test2-*]
2943
settings:
3044
number_of_shards: 1
3145
number_of_replicas: 0
46+
mappings:
47+
properties:
48+
field:
49+
type: text
3250

3351
- do:
3452
indices.get_template:
53+
include_type_name: false
3554
name: test
3655
flat_settings: true
3756

3857
- match: {test.index_patterns: ["test-*", "test2-*"]}
3958
- match: {test.settings: {index.number_of_shards: '1', index.number_of_replicas: '0'}}
59+
- match: {test.mappings: {properties: {field: {type: text}}}}
60+
61+
---
62+
"Put template with empty mappings":
63+
- skip:
64+
version: " - 6.99.99"
65+
reason: include_type_name was introduced in 7.0.0
66+
67+
- do:
68+
indices.put_template:
69+
include_type_name: false
70+
name: test
71+
body:
72+
index_patterns: test-*
73+
settings:
74+
number_of_shards: 1
75+
number_of_replicas: 0
76+
mappings: {}
77+
78+
- do:
79+
indices.get_template:
80+
include_type_name: false
81+
name: test
82+
flat_settings: true
83+
84+
- match: {test.mappings: {}}
4085

4186
---
4287
"Put template with aliases":
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
"Put template":
3+
- do:
4+
indices.put_template:
5+
name: test
6+
body:
7+
index_patterns: test-*
8+
settings:
9+
number_of_shards: 1
10+
number_of_replicas: 0
11+
mappings:
12+
_doc:
13+
properties:
14+
field:
15+
type: keyword
16+
17+
- do:
18+
indices.get_template:
19+
name: test
20+
flat_settings: true
21+
22+
- match: {test.index_patterns: ["test-*"]}
23+
- match: {test.settings: {index.number_of_shards: '1', index.number_of_replicas: '0'}}
24+
- match: {test.mappings: {_doc: {properties: {field: {type: keyword}}}}}
25+
26+
---
27+
"Put multiple template":
28+
- do:
29+
indices.put_template:
30+
name: test
31+
body:
32+
index_patterns: [test-*, test2-*]
33+
settings:
34+
number_of_shards: 1
35+
number_of_replicas: 0
36+
mappings:
37+
_doc:
38+
properties:
39+
field:
40+
type: text
41+
42+
- do:
43+
indices.get_template:
44+
name: test
45+
flat_settings: true
46+
47+
- match: {test.index_patterns: ["test-*", "test2-*"]}
48+
- match: {test.settings: {index.number_of_shards: '1', index.number_of_replicas: '0'}}
49+
- match: {test.mappings: {_doc: {properties: {field: {type: text}}}}}
50+
51+
---
52+
"Put template with empty mappings":
53+
- do:
54+
indices.put_template:
55+
name: test
56+
body:
57+
index_patterns: test-*
58+
settings:
59+
number_of_shards: 1
60+
number_of_replicas: 0
61+
mappings: {}
62+
63+
- do:
64+
indices.get_template:
65+
name: test
66+
flat_settings: true
67+
68+
- match: {test.mappings: {}}

server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestPutIndexTemplateAction.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.elasticsearch.common.Strings;
2626
import org.elasticsearch.common.logging.DeprecationLogger;
2727
import org.elasticsearch.common.settings.Settings;
28+
import org.elasticsearch.common.xcontent.XContentHelper;
29+
import org.elasticsearch.index.mapper.MapperService;
2830
import org.elasticsearch.rest.BaseRestHandler;
2931
import org.elasticsearch.rest.RestController;
3032
import org.elasticsearch.rest.RestRequest;
@@ -33,6 +35,8 @@
3335
import java.io.IOException;
3436
import java.util.Arrays;
3537
import java.util.Collections;
38+
import java.util.HashMap;
39+
import java.util.Map;
3640

3741
public class RestPutIndexTemplateAction extends BaseRestHandler {
3842

@@ -63,7 +67,16 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
6367
putRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putRequest.masterNodeTimeout()));
6468
putRequest.create(request.paramAsBoolean("create", false));
6569
putRequest.cause(request.param("cause", ""));
66-
putRequest.source(request.requiredContent(), request.getXContentType());
70+
71+
boolean includeTypeName = request.paramAsBoolean(INCLUDE_TYPE_NAME_PARAMETER, true);
72+
Map<String, Object> sourceAsMap = XContentHelper.convertToMap(request.requiredContent(), false,
73+
request.getXContentType()).v2();
74+
if (includeTypeName == false && sourceAsMap.containsKey("mappings")) {
75+
Map<String, Object> newSourceAsMap = new HashMap<>(sourceAsMap);
76+
newSourceAsMap.put("mappings", Collections.singletonMap(MapperService.SINGLE_MAPPING_NAME, sourceAsMap.get("mappings")));
77+
sourceAsMap = newSourceAsMap;
78+
}
79+
putRequest.source(sourceAsMap);
6780
return channel -> client.admin().indices().putTemplate(putRequest, new RestToXContentListener<>(channel));
6881
}
6982

0 commit comments

Comments
 (0)