Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types removal - PUT and GET IndexTemplates. #35866

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ static Request putTemplate(PutIndexTemplateRequest putIndexTemplateRequest) thro
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
RequestConverters.Params params = new RequestConverters.Params(request);
params.withMasterTimeout(putIndexTemplateRequest.masterNodeTimeout());
params.putParam("include_type_name", Boolean.toString(putIndexTemplateRequest.isCustomTyped()));
if (putIndexTemplateRequest.create()) {
params.putParam("create", Boolean.TRUE.toString());
}
Expand Down Expand Up @@ -395,6 +396,9 @@ static Request getTemplates(GetIndexTemplatesRequest getIndexTemplatesRequest) {
final RequestConverters.Params params = new RequestConverters.Params(request);
params.withLocal(getIndexTemplatesRequest.isLocal());
params.withMasterTimeout(getIndexTemplatesRequest.getMasterNodeTimeout());
if(getIndexTemplatesRequest.includeTypeNamesInResponse() == false) {
params.putParam("include_type_name", Boolean.toString(getIndexTemplatesRequest.includeTypeNamesInResponse()));
}
return request;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class GetIndexTemplatesRequest implements Validatable {

private TimeValue masterNodeTimeout = TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT;
private boolean local = false;
private boolean includeTypeNamesInResponse = true;

/**
* Create a request to read the content of one or more index templates. If no template names are provided, all templates will be read
Expand Down Expand Up @@ -96,4 +97,13 @@ public boolean isLocal() {
public void setLocal(boolean local) {
this.local = local;
}


public boolean includeTypeNamesInResponse() {
return includeTypeNamesInResponse;
}

public void includeTypeNamesInResponse(boolean includeTypeNamesInResponse) {
this.includeTypeNamesInResponse = includeTypeNamesInResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
import org.elasticsearch.common.ValidationException;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeUnit;
Expand Down Expand Up @@ -1229,32 +1231,74 @@ public void testIndexPutSettingNonExistent() throws IOException {
+ "reason=unknown setting [index.no_idea_what_you_are_talking_about] please check that any required plugins are installed, "
+ "or check the breaking changes documentation for removed settings]"));
}


@SuppressWarnings("unchecked")
@SuppressWarnings("deprecation")
public void testPutCustomTypedTemplateHasWarnings() throws Exception {
PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest()
.name("my-template")
.patterns(Arrays.asList("pattern-1", "name-*"))
.order(10)
.create(randomBoolean())
.mapping("custom_doc_type", "host_name", "type=keyword", "description", "type=text");


ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(putTemplateRequest,
highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync));
assertThat(exception.getDetailedMessage(), containsString("[types removal]"));
assertThat(exception.status(), equalTo(RestStatus.OK));
}

@SuppressWarnings({"unchecked","deprecation"})
public void testPutTemplate() throws Exception {
PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest()
.name("my-template")
.patterns(Arrays.asList("pattern-1", "name-*"))
.order(10)
.create(randomBoolean())
.settings(Settings.builder().put("number_of_shards", "3").put("number_of_replicas", "0"))
.mapping("doc", "host_name", "type=keyword", "description", "type=text")
.simplifiedMapping("host_name", "type=keyword", "description", "type=text")
.alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz"));

IndicesClient indicesClient = highLevelClient().indices();
AcknowledgedResponse putTemplateResponse = execute(putTemplateRequest,
highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync);
indicesClient::putTemplate, indicesClient::putTemplateAsync);
assertThat(putTemplateResponse.isAcknowledged(), equalTo(true));

Map<String, Object> templates = getAsMap("/_template/my-template");

Map<String, Object> templates = getAsMap("/_template/my-template?include_type_name=false");
assertThat(templates.keySet(), hasSize(1));
assertThat(extractValue("my-template.order", templates), equalTo(10));
assertThat(extractRawValues("my-template.index_patterns", templates), contains("pattern-1", "name-*"));
assertThat(extractValue("my-template.settings.index.number_of_shards", templates), equalTo("3"));
assertThat(extractValue("my-template.settings.index.number_of_replicas", templates), equalTo("0"));
assertThat(extractValue("my-template.mappings.doc.properties.host_name.type", templates), equalTo("keyword"));
assertThat(extractValue("my-template.mappings.doc.properties.description.type", templates), equalTo("text"));
assertThat(extractValue("my-template.mappings.properties.host_name.type", templates), equalTo("keyword"));
assertThat(extractValue("my-template.mappings.properties.description.type", templates), equalTo("text"));
assertThat((Map<String, String>) extractValue("my-template.aliases.alias-1", templates), hasEntry("index_routing", "abc"));
assertThat((Map<String, String>) extractValue("my-template.aliases.{index}-write", templates), hasEntry("search_routing", "xyz"));

// Test the typed version of the template has the default doc type name
templates = getAsMap("/_template/my-template");
assertThat(extractValue("my-template.mappings._doc.properties.host_name.type", templates), equalTo("keyword"));
assertThat(extractValue("my-template.mappings._doc.properties.description.type", templates), equalTo("text"));

// Now test with the getIndexTemplatesResponse untyped mappings
GetIndexTemplatesRequest untypedGet = new GetIndexTemplatesRequest("my-template");
untypedGet.includeTypeNamesInResponse(false);
GetIndexTemplatesResponse getTemplate1 = execute(untypedGet, indicesClient::getTemplate, indicesClient::getTemplateAsync);
IndexTemplateMetaData typelessTemplate = getTemplate1.getIndexTemplates().get(0);
ImmutableOpenMap<String, CompressedXContent> mappings = typelessTemplate.getMappings();
assertNull(mappings.get("_doc"));
assertNotNull(mappings.get("properties"));

// Now test with the getIndexTemplatesResponse typed mappings
GetIndexTemplatesRequest typedMappingRequest = new GetIndexTemplatesRequest("my-template");
typedMappingRequest.includeTypeNamesInResponse(true);
getTemplate1 = execute(typedMappingRequest, indicesClient::getTemplate, indicesClient::getTemplateAsync);
IndexTemplateMetaData typedTemplate = getTemplate1.getIndexTemplates().get(0);
mappings = typedTemplate.getMappings();
assertNotNull(mappings.get("_doc"));
assertNull(mappings.get("properties"));
}

public void testPutTemplateBadRequests() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -817,14 +817,16 @@ public void testPutTemplateRequest() throws Exception {
if (ESTestCase.randomBoolean()) {
putTemplateRequest.settings(Settings.builder().put("setting-" + ESTestCase.randomInt(), ESTestCase.randomTimeValue()));
}
Map<String, String> expectedParams = new HashMap<>();
expectedParams.put("include_type_name", "false");
if (ESTestCase.randomBoolean()) {
putTemplateRequest.mapping("doc-" + ESTestCase.randomInt(),
"field-" + ESTestCase.randomInt(), "type=" + ESTestCase.randomFrom("text", "keyword"));
expectedParams.put("include_type_name", "true"); // Custom doc types will set the "include_type_name" = true flag
}
if (ESTestCase.randomBoolean()) {
putTemplateRequest.alias(new Alias("alias-" + ESTestCase.randomInt()));
}
Map<String, String> expectedParams = new HashMap<>();
if (ESTestCase.randomBoolean()) {
expectedParams.put("create", Boolean.TRUE.toString());
putTemplateRequest.create(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2119,31 +2119,31 @@ public void testPutTemplate() throws Exception {

{
// tag::put-template-request-mappings-json
request.mapping("_doc", // <1>
request.mappingNoDocType(
"{\n" +
" \"_doc\": {\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
"}", // <2>
"}", // <1>
XContentType.JSON);
// end::put-template-request-mappings-json
assertTrue(client.indices().putTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
}
{
//tag::put-template-request-mappings-map
Map<String, Object> jsonMap = new HashMap<>();
Map<String, Object> message = new HashMap<>();
message.put("type", "text");
Map<String, Object> properties = new HashMap<>();
properties.put("message", message);
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
jsonMap.put("_doc", mapping);
request.mapping("_doc", jsonMap); // <1>
{
Map<String, Object> properties = new HashMap<>();
{
Map<String, Object> message = new HashMap<>();
message.put("type", "text");
properties.put("message", message);
}
jsonMap.put("properties", properties);
}
request.mapping(jsonMap); // <1>
//end::put-template-request-mappings-map
assertTrue(client.indices().putTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
}
Expand All @@ -2152,28 +2152,24 @@ public void testPutTemplate() throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("_doc");
builder.startObject("properties");
{
builder.startObject("properties");
builder.startObject("message");
{
builder.startObject("message");
{
builder.field("type", "text");
}
builder.endObject();
builder.field("type", "text");
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
request.mapping("_doc", builder); // <1>
request.mappingNoDocType(builder); // <1>
//end::put-template-request-mappings-xcontent
assertTrue(client.indices().putTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
}
{
//tag::put-template-request-mappings-shortcut
request.mapping("_doc", "message", "type=text"); // <1>
request.simplifiedMapping("message", "type=text"); // <1>
//end::put-template-request-mappings-shortcut
assertTrue(client.indices().putTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
}
Expand All @@ -2192,7 +2188,7 @@ public void testPutTemplate() throws Exception {
// end::put-template-request-version

// tag::put-template-whole-source
request.source("{\n" +
request.sourceNoDocTypes("{\n" +
" \"index_patterns\": [\n" +
" \"log-*\",\n" +
" \"pattern-1\"\n" +
Expand All @@ -2202,11 +2198,9 @@ public void testPutTemplate() throws Exception {
" \"number_of_shards\": 1\n" +
" },\n" +
" \"mappings\": {\n" +
" \"_doc\": {\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
" },\n" +
Expand Down Expand Up @@ -2269,13 +2263,11 @@ public void testGetTemplates() throws Exception {
PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
putRequest.patterns(Arrays.asList("pattern-1", "log-*"));
putRequest.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1));
putRequest.mapping("_doc",
putRequest.mappingNoDocType(
"{\n" +
" \"_doc\": {\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
"}", XContentType.JSON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public void onFailure(Exception e) {
client.security().putUserAsync(request, RequestOptions.DEFAULT, listener); // <1>
// end::put-user-execute-async

assertTrue(latch.await(30L, TimeUnit.SECONDS));
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}

Expand Down
3 changes: 1 addition & 2 deletions docs/java-rest/high-level/indices/put_template.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ template's patterns.
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-request-mappings-json]
--------------------------------------------------
<1> The type to define
<2> The mapping for this type, provided as a JSON string
<1> The mapping for this type, provided as a JSON string

The mapping source can be provided in different ways in addition to the
`String` example shown above:
Expand Down
44 changes: 24 additions & 20 deletions docs/reference/indices/templates.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@ For example:

[source,js]
--------------------------------------------------
PUT _template/template_1
PUT _template/template_1?include_type_name=false
{
"index_patterns": ["te*", "bar*"],
"settings": {
"number_of_shards": 1
},
"mappings": {
"_doc": {
"_source": {
"enabled": false
"_source": {
"enabled": false
},
"properties": {
"host_name": {
"type": "keyword"
},
"properties": {
"host_name": {
"type": "keyword"
},
"created_at": {
"type": "date",
"format": "EEE MMM dd HH:mm:ss Z yyyy"
}
"created_at": {
"type": "date",
"format": "EEE MMM dd HH:mm:ss Z yyyy"
}
}
}
Expand All @@ -50,6 +48,11 @@ Defines a template named `template_1`, with a template pattern of `te*` or `bar*
The settings and mappings will be applied to any index name that matches
the `te*` or `bar*` pattern.

NOTE: This mapping example uses a "typeless" format new to 7.0.
Previous versions of elasticsearch included document type names in the "mappings"section
but here we have set the `include_type_name=false` parameter in the URL to use the new format.
The old approach of including type names in mappings is still supported but is now deprecated.

It is also possible to include aliases in an index template as follows:

[source,js]
Expand Down Expand Up @@ -149,31 +152,27 @@ orders overriding them. For example:

[source,js]
--------------------------------------------------
PUT /_template/template_1
PUT /_template/template_1?include_type_name=false
{
"index_patterns" : ["*"],
"order" : 0,
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"_doc" : {
"_source" : { "enabled" : false }
}
"_source" : { "enabled" : false }
}
}

PUT /_template/template_2
PUT /_template/template_2?include_type_name=false
{
"index_patterns" : ["te*"],
"order" : 1,
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"_doc" : {
"_source" : { "enabled" : true }
}
"_source" : { "enabled" : true }
}
}
--------------------------------------------------
Expand All @@ -189,6 +188,11 @@ order templates, with lower order templates providing the basis.
NOTE: Multiple matching templates with the same order value will
result in a non-deterministic merging order.

NOTE: This mapping example uses the "typeless" format new to 7.0.
Previous versions of elasticsearch included document type names in the "mappings"section
but here we have set the `include_type_name=false` parameter in the URL to use the new format.
The old approach of including type names in mappings is still supported but is now deprecated.

[float]
[[versioning-templates]]
=== Template Versioning
Expand Down
Loading