Skip to content

Commit 324213c

Browse files
committed
Add include_type_name to the get index templates API.
1 parent e3138c0 commit 324213c

File tree

5 files changed

+127
-12
lines changed

5 files changed

+127
-12
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
}
1717
},
1818
"params": {
19+
"include_type_name": {
20+
"type" : "boolean",
21+
"description" : "Whether a type should be returned in the body of the mappings."
22+
},
1923
"flat_settings": {
2024
"type": "boolean",
2125
"description": "Return settings in flat format (default: false)"

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
setup:
2+
- skip:
3+
version: " - 6.99.99"
4+
reason: include_type_name is not supported before 7.0.0
25
- do:
36
indices.put_template:
47
name: test
@@ -7,16 +10,44 @@ setup:
710
settings:
811
number_of_shards: 1
912
number_of_replicas: 0
13+
mappings:
14+
_doc:
15+
properties:
16+
field:
17+
type: keyword
1018

1119
---
1220
"Get template":
1321

1422
- do:
1523
indices.get_template:
24+
include_type_name: false
1625
name: test
1726

1827
- match: {test.index_patterns: ["test-*"]}
1928
- match: {test.settings: {index: {number_of_shards: '1', number_of_replicas: '0'}}}
29+
- match: {test.mappings: {properties: {field: {type: keyword}}}}
30+
31+
---
32+
"Get template with no mappings":
33+
34+
- do:
35+
indices.put_template:
36+
name: test_no_mappings
37+
body:
38+
index_patterns: test-*
39+
settings:
40+
number_of_shards: 1
41+
number_of_replicas: 0
42+
43+
- do:
44+
indices.get_template:
45+
include_type_name: false
46+
name: test_no_mappings
47+
48+
- match: {test_no_mappings.index_patterns: ["test-*"]}
49+
- match: {test_no_mappings.settings: {index: {number_of_shards: '1', number_of_replicas: '0'}}}
50+
- match: {test_no_mappings.mappings: {}}
2051

2152
---
2253
"Get all templates":
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
setup:
2+
- do:
3+
indices.put_template:
4+
name: test
5+
body:
6+
index_patterns: test-*
7+
settings:
8+
number_of_shards: 1
9+
number_of_replicas: 0
10+
mappings:
11+
_doc:
12+
properties:
13+
field:
14+
type: keyword
15+
16+
---
17+
"Get template":
18+
19+
- do:
20+
indices.get_template:
21+
name: test
22+
23+
- match: {test.index_patterns: ["test-*"]}
24+
- match: {test.settings: {index: {number_of_shards: '1', number_of_replicas: '0'}}}
25+
- match: {test.mappings: {_doc: {properties: {field: {type: keyword}}}}}
26+
27+
---
28+
"Get template with no mappings":
29+
30+
- do:
31+
indices.put_template:
32+
name: test_no_mappings
33+
body:
34+
index_patterns: test-*
35+
settings:
36+
number_of_shards: 1
37+
number_of_replicas: 0
38+
39+
- do:
40+
indices.get_template:
41+
name: test_no_mappings
42+
43+
- match: {test_no_mappings.index_patterns: ["test-*"]}
44+
- match: {test_no_mappings.settings: {index: {number_of_shards: '1', number_of_replicas: '0'}}}
45+
- match: {test_no_mappings.mappings: {}}

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

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import com.carrotsearch.hppc.cursors.ObjectCursor;
2222
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
23-
2423
import org.apache.logging.log4j.LogManager;
2524
import org.elasticsearch.ElasticsearchParseException;
2625
import org.elasticsearch.Version;
@@ -42,6 +41,8 @@
4241
import org.elasticsearch.common.xcontent.XContentFactory;
4342
import org.elasticsearch.common.xcontent.XContentHelper;
4443
import org.elasticsearch.common.xcontent.XContentParser;
44+
import org.elasticsearch.index.mapper.MapperService;
45+
import org.elasticsearch.rest.BaseRestHandler;
4546

4647
import java.io.IOException;
4748
import java.util.ArrayList;
@@ -342,6 +343,8 @@ public static void toXContent(IndexTemplateMetaData indexTemplateMetaData, XCont
342343
public static void toInnerXContent(IndexTemplateMetaData indexTemplateMetaData, XContentBuilder builder, ToXContent.Params params)
343344
throws IOException {
344345

346+
boolean includeTypeName = params.paramAsBoolean(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, true);
347+
345348
builder.field("order", indexTemplateMetaData.order());
346349
if (indexTemplateMetaData.version() != null) {
347350
builder.field("version", indexTemplateMetaData.version());
@@ -353,18 +356,35 @@ public static void toInnerXContent(IndexTemplateMetaData indexTemplateMetaData,
353356
builder.endObject();
354357

355358
if (params.paramAsBoolean("reduce_mappings", false)) {
356-
builder.startObject("mappings");
357-
for (ObjectObjectCursor<String, CompressedXContent> cursor : indexTemplateMetaData.mappings()) {
358-
byte[] mappingSource = cursor.value.uncompressed();
359-
Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(mappingSource), true).v2();
360-
if (mapping.size() == 1 && mapping.containsKey(cursor.key)) {
361-
// the type name is the root value, reduce it
362-
mapping = (Map<String, Object>) mapping.get(cursor.key);
359+
// The parameter include_type_name is only ever used in the REST API, where reduce_mappings is
360+
// always set to true. We therefore only check for include_type_name in this branch.
361+
if (includeTypeName == false) {
362+
Map<String, Object> documentMapping = null;
363+
for (ObjectObjectCursor<String, CompressedXContent> cursor : indexTemplateMetaData.mappings()) {
364+
if (!cursor.key.equals(MapperService.DEFAULT_MAPPING)) {
365+
assert documentMapping == null;
366+
byte[] mappingSource = cursor.value.uncompressed();
367+
Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(mappingSource), true).v2();
368+
documentMapping = reduceMapping(cursor.key, mapping);
369+
}
370+
}
371+
372+
if (documentMapping != null) {
373+
builder.field("mappings", documentMapping);
374+
} else {
375+
builder.startObject("mappings").endObject();
376+
}
377+
} else {
378+
builder.startObject("mappings");
379+
for (ObjectObjectCursor<String, CompressedXContent> cursor : indexTemplateMetaData.mappings()) {
380+
byte[] mappingSource = cursor.value.uncompressed();
381+
Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(mappingSource), true).v2();
382+
mapping = reduceMapping(cursor.key, mapping);
383+
builder.field(cursor.key);
384+
builder.map(mapping);
363385
}
364-
builder.field(cursor.key);
365-
builder.map(mapping);
386+
builder.endObject();
366387
}
367-
builder.endObject();
368388
} else {
369389
builder.startArray("mappings");
370390
for (ObjectObjectCursor<String, CompressedXContent> cursor : indexTemplateMetaData.mappings()) {
@@ -381,6 +401,16 @@ public static void toInnerXContent(IndexTemplateMetaData indexTemplateMetaData,
381401
builder.endObject();
382402
}
383403

404+
@SuppressWarnings("unchecked")
405+
private static Map<String, Object> reduceMapping(String type, Map<String, Object> mapping) {
406+
if (mapping.size() == 1 && mapping.containsKey(type)) {
407+
// the type name is the root value, reduce it
408+
return (Map<String, Object>) mapping.get(type);
409+
} else {
410+
return mapping;
411+
}
412+
}
413+
384414
public static IndexTemplateMetaData fromXContent(XContentParser parser, String templateName) throws IOException {
385415
Builder builder = new Builder(templateName);
386416

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
import org.elasticsearch.client.node.NodeClient;
2525
import org.elasticsearch.common.Strings;
2626
import org.elasticsearch.common.settings.Settings;
27+
import org.elasticsearch.common.util.set.Sets;
2728
import org.elasticsearch.rest.BaseRestHandler;
2829
import org.elasticsearch.rest.RestController;
2930
import org.elasticsearch.rest.RestRequest;
3031
import org.elasticsearch.rest.RestStatus;
3132
import org.elasticsearch.rest.action.RestToXContentListener;
3233

3334
import java.io.IOException;
35+
import java.util.Collections;
3436
import java.util.Set;
3537

3638
import static org.elasticsearch.rest.RestRequest.Method.GET;
@@ -43,6 +45,9 @@
4345
*/
4446
public class RestGetIndexTemplateAction extends BaseRestHandler {
4547

48+
private static final Set<String> RESPONSE_PARAMETERS = Collections.unmodifiableSet(Sets.union(
49+
Collections.singleton(INCLUDE_TYPE_NAME_PARAMETER), Settings.FORMAT_PARAMS));
50+
4651
public RestGetIndexTemplateAction(final Settings settings, final RestController controller) {
4752
super(settings);
4853
controller.registerHandler(GET, "/_template", this);
@@ -79,7 +84,7 @@ protected RestStatus getStatus(final GetIndexTemplatesResponse response) {
7984

8085
@Override
8186
protected Set<String> responseParams() {
82-
return Settings.FORMAT_PARAMS;
87+
return RESPONSE_PARAMETERS;
8388
}
8489

8590
}

0 commit comments

Comments
 (0)