Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
"Create a typeless index while there is a typed template":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the change is a bit subtle, I wonder if it'd be worth adding some unit tests in IndexCreationTaskTests? In particular, reading the code I had to think a bit about what would happen if no mappings were provided in the create index requests, or if the template or create request had more than one type.


- skip:
version: " - 6.99.99"
reason: needs change to be backported to 6.7

- do:
indices.put_template:
include_type_name: true
name: test_template
body:
index_patterns: test-*
mappings:
my_type:
properties:
foo:
type: keyword

- do:
indices.create:
include_type_name: false
index: test-1
body:
mappings:
properties:
bar:
type: "long"

- do:
indices.get_mapping:
include_type_name: true
index: test-1

- is_true: test-1.mappings._doc # the index creation call won
- is_false: test-1.mappings.my_type
- is_true: test-1.mappings._doc.properties.foo
- is_true: test-1.mappings._doc.properties.bar

---
"Create a typed index while there is a typeless template":

- skip:
version: " - 6.99.99"
reason: needs change to be backported to 6.7

- do:
indices.put_template:
include_type_name: false
name: test_template
body:
index_patterns: test-*
mappings:
properties:
foo:
type: keyword

- do:
indices.create:
include_type_name: true
index: test-1
body:
mappings:
my_type:
properties:
bar:
type: "long"

- do:
indices.get_mapping:
include_type_name: true
index: test-1

- is_true: test-1.mappings.my_type # the index creation call won
- is_false: test-1.mappings._doc
- is_true: test-1.mappings.my_type.properties.foo
- is_true: test-1.mappings.my_type.properties.bar
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,28 @@ public ClusterState execute(ClusterState currentState) throws Exception {
if (mappings.containsKey(cursor.key)) {
XContentHelper.mergeDefaults(mappings.get(cursor.key),
MapperService.parseMapping(xContentRegistry, mappingString));
} else if (mappings.size() == 1 && cursor.key.equals(MapperService.SINGLE_MAPPING_NAME)) {
// Typeless template with typed mapping
Map<String, Object> templateMapping = MapperService.parseMapping(xContentRegistry, mappingString);
assert templateMapping.size() == 1 : templateMapping;
assert cursor.key.equals(templateMapping.keySet().iterator().next()) :
cursor.key + " != " + templateMapping;
Map.Entry<String, Map<String, Object>> mappingEntry = mappings.entrySet().iterator().next();
templateMapping = Collections.singletonMap(
mappingEntry.getKey(), // reuse type name from the mapping
templateMapping.values().iterator().next()); // but actual mappings from the template
XContentHelper.mergeDefaults(mappingEntry.getValue(), templateMapping);
} else if (template.mappings().size() == 1 && mappings.containsKey(MapperService.SINGLE_MAPPING_NAME)) {
// Typed template with typeless mapping
Map<String, Object> templateMapping = MapperService.parseMapping(xContentRegistry, mappingString);
assert templateMapping.size() == 1 : templateMapping;
assert cursor.key.equals(templateMapping.keySet().iterator().next()) :
cursor.key + " != " + templateMapping;
Map<String, Object> mapping = mappings.get(MapperService.SINGLE_MAPPING_NAME);
templateMapping = Collections.singletonMap(
MapperService.SINGLE_MAPPING_NAME, // make template mapping typeless
templateMapping.values().iterator().next());
XContentHelper.mergeDefaults(mapping, templateMapping);
} else {
mappings.put(cursor.key,
MapperService.parseMapping(xContentRegistry, mappingString));
Expand Down