Make sure to reject mappings with type _doc when include_type_name is false. (#38270)

`CreateIndexRequest#source(Map<String, Object>, ... )`, which is used when
deserializing index creation requests, accidentally accepts mappings that are
nested twice under the type key (as described in the bug report #38266).

This in turn causes us to be too lenient in parsing typeless mappings. In
particular, we accept the following index creation request, even though it
should not contain the type key `_doc`:

```
PUT index?include_type_name=false
{
  "mappings": {
    "_doc": {
      "properties": { ... }
    }
  }
}
```

There is a similar issue for both 'put templates' and 'put mappings' requests
as well.

This PR makes the minimal changes to detect and reject these typed mappings in
requests. It does not address #38266 generally, or attempt a larger refactor
around types in these server-side requests, as I think this should be done at a
later time.
This commit is contained in:
Julie Tibshirani 2019-02-05 10:52:32 -08:00 committed by GitHub
parent 8ebff0512b
commit 3ce7d2c9b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 644 additions and 618 deletions

View File

@ -1235,7 +1235,6 @@ public class CRUDDocumentationIT extends ESRestHighLevelClientTestCase {
createIndex.setJsonEntity(
"{\n" +
" \"mappings\" : {\n" +
" \"_doc\" : {\n" +
" \"properties\" : {\n" +
" \"message\" : {\n" +
" \"type\": \"text\",\n" +
@ -1243,7 +1242,6 @@ public class CRUDDocumentationIT extends ESRestHighLevelClientTestCase {
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}");
Response response = client().performRequest(createIndex);
assertEquals(200, response.getStatusLine().getStatusCode());
@ -1764,7 +1762,6 @@ public class CRUDDocumentationIT extends ESRestHighLevelClientTestCase {
createIndex.setJsonEntity(
"{\n" +
" \"mappings\" : {\n" +
" \"_doc\" : {\n" +
" \"properties\" : {\n" +
" \"foo\" : {\n" +
" \"type\": \"text\",\n" +
@ -1772,7 +1769,6 @@ public class CRUDDocumentationIT extends ESRestHighLevelClientTestCase {
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}");
Response response = client().performRequest(createIndex);
assertEquals(200, response.getStatusLine().getStatusCode());

View File

@ -313,15 +313,13 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
{
request = new CreateIndexRequest("twitter2");
//tag::create-index-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(jsonMap); // <1>
request.mapping(mapping); // <1>
//end::create-index-mappings-map
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
assertTrue(createIndexResponse.isAcknowledged());
@ -331,8 +329,6 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
//tag::create-index-mappings-xcontent
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("_doc");
{
builder.startObject("properties");
{
@ -345,8 +341,6 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
builder.endObject();
}
builder.endObject();
}
builder.endObject();
request.mapping(builder); // <1>
//end::create-index-mappings-xcontent
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
@ -381,11 +375,9 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
" \"number_of_replicas\" : 0\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"_doc\" : {\n" +
" \"properties\" : {\n" +
" \"message\" : { \"type\" : \"text\" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"aliases\" : {\n" +
" \"twitter_alias\" : {}\n" +

View File

@ -98,7 +98,6 @@ Closure setupTwitter = { String name, int count ->
number_of_shards: 1
number_of_replicas: 1
mappings:
_doc:
properties:
user:
type: keyword
@ -110,7 +109,6 @@ Closure setupTwitter = { String name, int count ->
- do:
bulk:
index: twitter
type: _doc
refresh: true
body: |'''
for (int i = 0; i < count; i++) {
@ -161,7 +159,6 @@ buildRestTests.setups['ledger'] = '''
number_of_shards: 2
number_of_replicas: 1
mappings:
_doc:
properties:
type:
type: keyword
@ -170,7 +167,6 @@ buildRestTests.setups['ledger'] = '''
- do:
bulk:
index: ledger
type: _doc
refresh: true
body: |
{"index":{}}
@ -194,14 +190,12 @@ buildRestTests.setups['sales'] = '''
number_of_shards: 2
number_of_replicas: 1
mappings:
_doc:
properties:
type:
type: keyword
- do:
bulk:
index: sales
type: _doc
refresh: true
body: |
{"index":{}}
@ -231,7 +225,6 @@ buildRestTests.setups['bank'] = '''
- do:
bulk:
index: bank
type: _doc
refresh: true
body: |
#bank_data#
@ -273,7 +266,6 @@ buildRestTests.setups['stackoverflow'] = '''
number_of_shards: 1
number_of_replicas: 1
mappings:
_doc:
properties:
author:
type: keyword
@ -282,7 +274,6 @@ buildRestTests.setups['stackoverflow'] = '''
- do:
bulk:
index: stackoverflow
type: _doc
refresh: true
body: |'''
@ -328,7 +319,6 @@ buildRestTests.setups['news'] = '''
number_of_shards: 1
number_of_replicas: 1
mappings:
_doc:
properties:
source:
type: keyword
@ -337,7 +327,6 @@ buildRestTests.setups['news'] = '''
- do:
bulk:
index: news
type: _doc
refresh: true
body: |'''
@ -381,14 +370,12 @@ buildRestTests.setups['exams'] = '''
number_of_shards: 1
number_of_replicas: 1
mappings:
_doc:
properties:
grade:
type: byte
- do:
bulk:
index: exams
type: _doc
refresh: true
body: |
{"index":{}}
@ -446,7 +433,6 @@ buildRestTests.setups['analyze_sample'] = '''
type: custom
filter: [lowercase]
mappings:
_doc:
properties:
obj1.field1:
type: text'''
@ -461,14 +447,12 @@ buildRestTests.setups['latency'] = '''
number_of_shards: 1
number_of_replicas: 1
mappings:
_doc:
properties:
load_time:
type: long
- do:
bulk:
index: latency
type: _doc
refresh: true
body: |'''
@ -493,14 +477,12 @@ buildRestTests.setups['iprange'] = '''
number_of_shards: 1
number_of_replicas: 1
mappings:
_doc:
properties:
ip:
type: ip
- do:
bulk:
index: ip_addresses
type: _doc
refresh: true
body: |'''
@ -613,7 +595,6 @@ buildRestTests.setups['sensor_rollup_job'] = '''
number_of_shards: 1
number_of_replicas: 0
mappings:
_doc:
properties:
timestamp:
type: date
@ -664,7 +645,6 @@ buildRestTests.setups['sensor_started_rollup_job'] = '''
number_of_shards: 1
number_of_replicas: 0
mappings:
_doc:
properties:
timestamp:
type: date
@ -678,7 +658,6 @@ buildRestTests.setups['sensor_started_rollup_job'] = '''
- do:
bulk:
index: sensor-1
type: _doc
refresh: true
body: |
{"index":{}}
@ -740,7 +719,6 @@ buildRestTests.setups['sensor_index'] = '''
number_of_shards: 1
number_of_replicas: 0
mappings:
_doc:
properties:
timestamp:
type: date
@ -771,7 +749,6 @@ buildRestTests.setups['sensor_prefab_data'] = '''
number_of_shards: 1
number_of_replicas: 0
mappings:
_doc:
properties:
timestamp:
type: date
@ -789,7 +766,6 @@ buildRestTests.setups['sensor_prefab_data'] = '''
number_of_shards: 1
number_of_replicas: 0
mappings:
_doc:
properties:
node.terms.value:
type: keyword
@ -846,7 +822,6 @@ buildRestTests.setups['sensor_prefab_data'] = '''
- do:
bulk:
index: sensor_rollup
type: _doc
refresh: true
body: |
{"index":{}}
@ -1093,7 +1068,6 @@ buildRestTests.setups['reviews'] = '''
number_of_shards: 1
number_of_replicas: 0
mappings:
_doc:
properties:
product:
type: keyword
@ -1102,7 +1076,6 @@ buildRestTests.setups['reviews'] = '''
- do:
bulk:
index: reviews
type: _doc
refresh: true
body: |
{"index": {"_id": "1"}}
@ -1139,7 +1112,6 @@ buildRestTests.setups['seats'] = '''
number_of_shards: 1
number_of_replicas: 0
mappings:
_doc:
properties:
theatre:
type: keyword
@ -1154,7 +1126,6 @@ buildRestTests.setups['seats'] = '''
- do:
bulk:
index: seats
type: _doc
refresh: true
body: |
{"index":{"_id": "1"}}

View File

@ -14,26 +14,22 @@ setup:
settings:
number_of_shards: 2
mappings:
_doc:
properties:
dval:
type: double
- do:
index:
index: test
type: _doc
id: d1
body: {"dval": 10}
- do:
index:
index: test
type: _doc
id: d2
body: {"dval": 100}
- do:
index:
index: test
type: _doc
id: d3
body: {"dval": 1000}
@ -225,7 +221,6 @@ setup:
settings:
number_of_shards: 1
mappings:
_doc:
properties:
date:
type: date
@ -309,7 +304,6 @@ setup:
settings:
number_of_shards: 1
mappings:
_doc:
properties:
ival:
type: integer

View File

@ -44,7 +44,6 @@
index: test
body:
mappings:
_doc:
_source:
enabled: false
- do:

View File

@ -135,3 +135,22 @@
properties:
"":
type: keyword
---
"Create index with explicit _doc type":
- skip:
version: " - 6.99.99"
reason: include_type_name defaults to true before 7.0
- do:
catch: bad_request
indices.create:
index: test_index
body:
mappings:
_doc:
properties:
field:
type: keyword
- match: { error.type: "illegal_argument_exception" }
- match: { error.reason: "The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true." }

View File

@ -68,3 +68,26 @@
properties:
"":
type: keyword
---
"Put mappings with explicit _doc type":
- skip:
version: " - 6.99.99"
reason: include_type_name defaults to true before 7.0
- do:
indices.create:
index: test_index
- do:
catch: bad_request
indices.put_mapping:
index: test_index
body:
_doc:
properties:
field:
type: keyword
- match: { error.type: "illegal_argument_exception" }
- match: { error.reason: "Types cannot be provided in put mapping requests, unless the include_type_name parameter is set to true." }

View File

@ -238,3 +238,24 @@
indices.put_template:
name: test
body: {}
---
"Put template with explicit _doc type":
- skip:
version: " - 6.99.99"
reason: include_type_name defaults to true before 7.0
- do:
catch: bad_request
indices.put_template:
name: test
body:
index_patterns: test-*
mappings:
_doc:
properties:
field:
type: keyword
- match: { error.type: "illegal_argument_exception" }
- match: { error.reason: "The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true." }

View File

@ -41,3 +41,32 @@
- match: { conditions: { "[max_docs: 2]": true } }
- match: { rolled_over: true }
---
"Mappings with explicit _doc type":
- skip:
version: " - 6.99.99"
reason: include_type_name defaults to true before 7.0
- do:
indices.create:
index: logs-1
body:
aliases:
logs_search: {}
- do:
catch: bad_request
indices.rollover:
alias: "logs_search"
body:
conditions:
max_docs: 2
mappings:
_doc:
properties:
field:
type: keyword
- match: { error.caused_by.type: "illegal_argument_exception" }
- match: { error.caused_by.reason: "The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true." }

View File

@ -70,13 +70,18 @@ public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implem
CONDITIONS, ObjectParser.ValueType.OBJECT);
PARSER.declareField((parser, request, context) -> request.createIndexRequest.settings(parser.map()),
CreateIndexRequest.SETTINGS, ObjectParser.ValueType.OBJECT);
PARSER.declareField((parser, request, isTypeIncluded) -> {
if (isTypeIncluded) {
PARSER.declareField((parser, request, includeTypeName) -> {
if (includeTypeName) {
for (Map.Entry<String, Object> mappingsEntry : parser.map().entrySet()) {
request.createIndexRequest.mapping(mappingsEntry.getKey(), (Map<String, Object>) mappingsEntry.getValue());
}
} else {
// a type is not included, add a dummy _doc type
Map<String, Object> mappings = parser.map();
if (MapperService.isMappingSourceTyped(MapperService.SINGLE_MAPPING_NAME, mappings)) {
throw new IllegalArgumentException("The mapping definition cannot be nested under a type " +
"[" + MapperService.SINGLE_MAPPING_NAME + "] unless include_type_name is set to true.");
}
request.createIndexRequest.mapping(MapperService.SINGLE_MAPPING_NAME, parser.map());
}
}, CreateIndexRequest.MAPPINGS, ObjectParser.ValueType.OBJECT);

View File

@ -22,13 +22,12 @@ package org.elasticsearch.cluster.metadata;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.elasticsearch.core.internal.io.IOUtils;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingClusterStateUpdateRequest;
import org.elasticsearch.cluster.AckedClusterStateTaskListener;
import org.elasticsearch.cluster.ClusterStateTaskExecutor;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateTaskConfig;
import org.elasticsearch.cluster.ClusterStateTaskExecutor;
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.service.ClusterService;
@ -37,8 +36,7 @@ import org.elasticsearch.common.Priority;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.core.internal.io.IOUtils;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.mapper.DocumentMapper;
@ -55,6 +53,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.elasticsearch.index.mapper.MapperService.isMappingSourceTyped;
import static org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason.NO_LONGER_ASSIGNED;
/**
@ -279,7 +278,7 @@ public class MetaDataMappingService {
if (mappingType == null) {
mappingType = newMapper.type();
} else if (mappingType.equals(newMapper.type()) == false
&& (isMappingSourceTyped(mapperService, mappingUpdateSource, request.type())
&& (isMappingSourceTyped(request.type(), mappingUpdateSource)
|| mapperService.resolveDocumentType(mappingType).equals(newMapper.type()) == false)) {
throw new InvalidTypeNameException("Type name provided does not match type name within mapping definition.");
}
@ -304,7 +303,7 @@ public class MetaDataMappingService {
// are handling a typeless call. In such a case, we override _doc with the actual type
// name in the mappings. This allows to use typeless APIs on typed indices.
String typeForUpdate = mappingType; // the type to use to apply the mapping update
if (isMappingSourceTyped(mapperService, mappingUpdateSource, request.type()) == false) {
if (isMappingSourceTyped(request.type(), mappingUpdateSource) == false) {
typeForUpdate = mapperService.resolveDocumentType(mappingType);
}
@ -371,15 +370,6 @@ public class MetaDataMappingService {
}
}
/**
* Returns {@code true} if the given {@code mappingSource} includes a type
* as a top-level object.
*/
private static boolean isMappingSourceTyped(MapperService mapperService, CompressedXContent mappingSource, String type) {
Map<String, Object> root = XContentHelper.convertToMap(mappingSource.compressedReference(), true, XContentType.JSON).v2();
return root.size() == 1 && root.keySet().iterator().next().equals(type);
}
public void putMapping(final PutMappingClusterStateUpdateRequest request, final ActionListener<ClusterStateUpdateResponse> listener) {
clusterService.submitStateUpdateTask("put-mapping",
request,

View File

@ -39,6 +39,7 @@ import org.elasticsearch.common.settings.Setting.Property;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.AbstractIndexComponent;
@ -665,6 +666,20 @@ public class MapperService extends AbstractIndexComponent implements Closeable {
return null;
}
/**
* Returns {@code true} if the given {@code mappingSource} includes a type
* as a top-level object.
*/
public static boolean isMappingSourceTyped(String type, Map<String, Object> mapping) {
return mapping.size() == 1 && mapping.keySet().iterator().next().equals(type);
}
public static boolean isMappingSourceTyped(String type, CompressedXContent mappingSource) {
Map<String, Object> root = XContentHelper.convertToMap(mappingSource.compressedReference(), true, XContentType.JSON).v2();
return isMappingSourceTyped(type, root);
}
/**
* Resolves a type from a mapping-related request into the type that should be used when
* merging and updating mappings.

View File

@ -64,18 +64,38 @@ public class RestCreateIndexAction extends BaseRestHandler {
}
CreateIndexRequest createIndexRequest = new CreateIndexRequest(request.param("index"));
if (request.hasContent()) {
Map<String, Object> sourceAsMap = XContentHelper.convertToMap(request.content(), false, request.getXContentType()).v2();
if (includeTypeName == false && sourceAsMap.containsKey("mappings")) {
Map<String, Object> newSourceAsMap = new HashMap<>(sourceAsMap);
newSourceAsMap.put("mappings", Collections.singletonMap(MapperService.SINGLE_MAPPING_NAME, sourceAsMap.get("mappings")));
sourceAsMap = newSourceAsMap;
}
Map<String, Object> sourceAsMap = XContentHelper.convertToMap(request.requiredContent(), false,
request.getXContentType()).v2();
sourceAsMap = prepareMappings(sourceAsMap, includeTypeName);
createIndexRequest.source(sourceAsMap, LoggingDeprecationHandler.INSTANCE);
}
createIndexRequest.timeout(request.paramAsTime("timeout", createIndexRequest.timeout()));
createIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", createIndexRequest.masterNodeTimeout()));
createIndexRequest.waitForActiveShards(ActiveShardCount.parseString(request.param("wait_for_active_shards")));
return channel -> client.admin().indices().create(createIndexRequest, new RestToXContentListener<>(channel));
}
static Map<String, Object> prepareMappings(Map<String, Object> source, boolean includeTypeName) {
if (includeTypeName
|| source.containsKey("mappings") == false
|| (source.get("mappings") instanceof Map) == false) {
return source;
}
Map<String, Object> newSource = new HashMap<>(source);
@SuppressWarnings("unchecked")
Map<String, Object> mappings = (Map<String, Object>) source.get("mappings");
if (MapperService.isMappingSourceTyped(MapperService.SINGLE_MAPPING_NAME, mappings)) {
throw new IllegalArgumentException("The mapping definition cannot be nested under a type " +
"[" + MapperService.SINGLE_MAPPING_NAME + "] unless include_type_name is set to true.");
}
newSource.put("mappings", Collections.singletonMap(MapperService.SINGLE_MAPPING_NAME, mappings));
return newSource;
}
}

View File

@ -26,7 +26,6 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
@ -35,7 +34,6 @@ import org.elasticsearch.rest.action.RestToXContentListener;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class RestPutIndexTemplateAction extends BaseRestHandler {
@ -59,6 +57,8 @@ public class RestPutIndexTemplateAction extends BaseRestHandler {
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
boolean includeTypeName = request.paramAsBoolean(INCLUDE_TYPE_NAME_PARAMETER, DEFAULT_INCLUDE_TYPE_NAME_POLICY);
PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest(request.param("name"));
if (request.hasParam(INCLUDE_TYPE_NAME_PARAMETER)) {
deprecationLogger.deprecatedAndMaybeLog("put_index_template_with_types", TYPES_DEPRECATION_MESSAGE);
@ -74,22 +74,11 @@ public class RestPutIndexTemplateAction extends BaseRestHandler {
putRequest.create(request.paramAsBoolean("create", false));
putRequest.cause(request.param("cause", ""));
boolean includeTypeName = request.paramAsBoolean(INCLUDE_TYPE_NAME_PARAMETER, DEFAULT_INCLUDE_TYPE_NAME_POLICY);
Map<String, Object> sourceAsMap = prepareRequestSource(request, includeTypeName);
Map<String, Object> sourceAsMap = XContentHelper.convertToMap(request.requiredContent(), false,
request.getXContentType()).v2();
sourceAsMap = RestCreateIndexAction.prepareMappings(sourceAsMap, includeTypeName);
putRequest.source(sourceAsMap);
return channel -> client.admin().indices().putTemplate(putRequest, new RestToXContentListener<>(channel));
}
Map<String, Object> prepareRequestSource(RestRequest request, boolean includeTypeName) {
Map<String, Object> sourceAsMap = XContentHelper.convertToMap(request.requiredContent(), false,
request.getXContentType()).v2();
if (includeTypeName == false && sourceAsMap.containsKey("mappings")) {
Map<String, Object> newSourceAsMap = new HashMap<>(sourceAsMap);
newSourceAsMap.put("mappings", Collections.singletonMap(MapperService.SINGLE_MAPPING_NAME, sourceAsMap.get("mappings")));
return newSourceAsMap;
} else {
return sourceAsMap;
}
}
}

View File

@ -26,6 +26,7 @@ import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestController;
@ -33,8 +34,10 @@ import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestToXContentListener;
import java.io.IOException;
import java.util.Map;
import static org.elasticsearch.client.Requests.putMappingRequest;
import static org.elasticsearch.index.mapper.MapperService.isMappingSourceTyped;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;
@ -81,15 +84,20 @@ public class RestPutMappingAction extends BaseRestHandler {
deprecationLogger.deprecatedAndMaybeLog("put_mapping_with_types", TYPES_DEPRECATION_MESSAGE);
}
PutMappingRequest putMappingRequest = putMappingRequest(Strings.splitStringByCommaToArray(request.param("index")));
final String type = request.param("type");
if (type != null && includeTypeName == false) {
putMappingRequest.type(includeTypeName ? type : MapperService.SINGLE_MAPPING_NAME);
Map<String, Object> sourceAsMap = XContentHelper.convertToMap(request.requiredContent(), false,
request.getXContentType()).v2();
if (includeTypeName == false &&
(type != null || isMappingSourceTyped(MapperService.SINGLE_MAPPING_NAME, sourceAsMap))) {
throw new IllegalArgumentException("Types cannot be provided in put mapping requests, unless " +
"the include_type_name parameter is set to true.");
}
PutMappingRequest putMappingRequest = putMappingRequest(Strings.splitStringByCommaToArray(request.param("index")));
putMappingRequest.type(includeTypeName ? type : MapperService.SINGLE_MAPPING_NAME);
putMappingRequest.source(request.requiredContent(), request.getXContentType());
putMappingRequest.source(sourceAsMap);
putMappingRequest.timeout(request.paramAsTime("timeout", putMappingRequest.timeout()));
putMappingRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putMappingRequest.masterNodeTimeout()));
putMappingRequest.indicesOptions(IndicesOptions.fromRequest(request, putMappingRequest.indicesOptions()));

View File

@ -20,7 +20,11 @@
package org.elasticsearch.rest.action.admin.indices;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.test.rest.FakeRestRequest;
import org.elasticsearch.test.rest.RestActionTestCase;
@ -59,4 +63,80 @@ public class RestCreateIndexActionTests extends RestActionTestCase {
.build();
action.prepareRequest(validRequest, mock(NodeClient.class));
}
public void testPrepareTypelessRequest() throws IOException {
XContentBuilder content = XContentFactory.jsonBuilder().startObject()
.startObject("mappings")
.startObject("properties")
.startObject("field1").field("type", "keyword").endObject()
.startObject("field2").field("type", "text").endObject()
.endObject()
.endObject()
.startObject("aliases")
.startObject("read_alias").endObject()
.endObject()
.endObject();
Map<String, Object> contentAsMap = XContentHelper.convertToMap(
BytesReference.bytes(content), true, content.contentType()).v2();
boolean includeTypeName = false;
Map<String, Object> source = RestCreateIndexAction.prepareMappings(contentAsMap, includeTypeName);
XContentBuilder expectedContent = XContentFactory.jsonBuilder().startObject()
.startObject("mappings")
.startObject("_doc")
.startObject("properties")
.startObject("field1").field("type", "keyword").endObject()
.startObject("field2").field("type", "text").endObject()
.endObject()
.endObject()
.endObject()
.startObject("aliases")
.startObject("read_alias").endObject()
.endObject()
.endObject();
Map<String, Object> expectedContentAsMap = XContentHelper.convertToMap(
BytesReference.bytes(expectedContent), true, expectedContent.contentType()).v2();
assertEquals(expectedContentAsMap, source);
}
public void testPrepareTypedRequest() throws IOException {
XContentBuilder content = XContentFactory.jsonBuilder().startObject()
.startObject("mappings")
.startObject("type")
.startObject("properties")
.startObject("field1").field("type", "keyword").endObject()
.startObject("field2").field("type", "text").endObject()
.endObject()
.endObject()
.endObject()
.startObject("aliases")
.startObject("read_alias").endObject()
.endObject()
.endObject();
Map<String, Object> contentAsMap = XContentHelper.convertToMap(
BytesReference.bytes(content), true, content.contentType()).v2();
boolean includeTypeName = true;
Map<String, Object> source = RestCreateIndexAction.prepareMappings(contentAsMap, includeTypeName);
assertEquals(contentAsMap, source);
}
public void testMalformedMappings() throws IOException {
XContentBuilder content = XContentFactory.jsonBuilder().startObject()
.field("mappings", "some string")
.startObject("aliases")
.startObject("read_alias").endObject()
.endObject()
.endObject();
Map<String, Object> contentAsMap = XContentHelper.convertToMap(
BytesReference.bytes(content), true, content.contentType()).v2();
boolean includeTypeName = false;
Map<String, Object> source = RestCreateIndexAction.prepareMappings(contentAsMap, includeTypeName);
assertEquals(contentAsMap, source);
}
}

View File

@ -24,7 +24,6 @@ import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.test.rest.FakeRestRequest;
@ -46,51 +45,6 @@ public class RestPutIndexTemplateActionTests extends RestActionTestCase {
action = new RestPutIndexTemplateAction(Settings.EMPTY, controller());
}
public void testPrepareTypelessRequest() throws IOException {
XContentBuilder content = XContentFactory.jsonBuilder().startObject()
.startObject("mappings")
.startObject("properties")
.startObject("field1").field("type", "keyword").endObject()
.startObject("field2").field("type", "text").endObject()
.endObject()
.endObject()
.startObject("aliases")
.startObject("read_alias").endObject()
.endObject()
.endObject();
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
.withMethod(RestRequest.Method.PUT)
.withPath("/_template/_some_template")
.withContent(BytesReference.bytes(content), XContentType.JSON)
.build();
action.prepareRequest(request, mock(NodeClient.class));
// Internally the above prepareRequest method calls prepareRequestSource to inject a
// default type into the mapping. Here we test that this does what is expected by
// explicitly calling that same helper function
boolean includeTypeName = false;
Map<String, Object> source = action.prepareRequestSource(request, includeTypeName);
XContentBuilder expectedContent = XContentFactory.jsonBuilder().startObject()
.startObject("mappings")
.startObject("_doc")
.startObject("properties")
.startObject("field1").field("type", "keyword").endObject()
.startObject("field2").field("type", "text").endObject()
.endObject()
.endObject()
.endObject()
.startObject("aliases")
.startObject("read_alias").endObject()
.endObject()
.endObject();
Map<String, Object> expectedContentAsMap = XContentHelper.convertToMap(
BytesReference.bytes(expectedContent), true, expectedContent.contentType()).v2();
assertEquals(expectedContentAsMap, source);
}
public void testIncludeTypeName() throws IOException {
XContentBuilder typedContent = XContentFactory.jsonBuilder().startObject()
.startObject("mappings")
@ -116,25 +70,5 @@ public class RestPutIndexTemplateActionTests extends RestActionTestCase {
.build();
action.prepareRequest(request, mock(NodeClient.class));
assertWarnings(RestPutIndexTemplateAction.TYPES_DEPRECATION_MESSAGE);
boolean includeTypeName = true;
Map<String, Object> source = action.prepareRequestSource(request, includeTypeName);
XContentBuilder expectedContent = XContentFactory.jsonBuilder().startObject()
.startObject("mappings")
.startObject("my_doc")
.startObject("properties")
.startObject("field1").field("type", "keyword").endObject()
.startObject("field2").field("type", "text").endObject()
.endObject()
.endObject()
.endObject()
.startObject("aliases")
.startObject("read_alias").endObject()
.endObject()
.endObject();
Map<String, Object> expectedContentAsMap = XContentHelper.convertToMap(
BytesReference.bytes(expectedContent), true, expectedContent.contentType()).v2();
assertEquals(expectedContentAsMap, source);
}
}

View File

@ -464,7 +464,6 @@ setups['sensor_rollup_job'] = '''
number_of_shards: 1
number_of_replicas: 0
mappings:
_doc:
properties:
timestamp:
type: date
@ -514,7 +513,6 @@ setups['sensor_started_rollup_job'] = '''
number_of_shards: 1
number_of_replicas: 0
mappings:
_doc:
properties:
timestamp:
type: date
@ -528,7 +526,6 @@ setups['sensor_started_rollup_job'] = '''
- do:
bulk:
index: sensor-1
type: _doc
refresh: true
body: |
{"index":{}}
@ -588,7 +585,6 @@ setups['sensor_index'] = '''
number_of_shards: 1
number_of_replicas: 0
mappings:
_doc:
properties:
timestamp:
type: date
@ -619,7 +615,6 @@ setups['sensor_prefab_data'] = '''
number_of_shards: 1
number_of_replicas: 0
mappings:
_doc:
properties:
timestamp:
type: date
@ -637,7 +632,6 @@ setups['sensor_prefab_data'] = '''
number_of_shards: 1
number_of_replicas: 0
mappings:
_doc:
properties:
node.terms.value:
type: keyword
@ -693,7 +687,6 @@ setups['sensor_prefab_data'] = '''
- do:
bulk:
index: sensor_rollup
type: _doc
refresh: true
body: |
{"index":{}}

View File

@ -210,7 +210,7 @@ public class CCRIndexLifecycleIT extends ESCCRRestTestCase {
// Create an index on the leader using the template set up above
Request createIndexRequest = new Request("PUT", "/" + indexName);
createIndexRequest.setJsonEntity("{" +
"\"mappings\": {\"_doc\": {\"properties\": {\"field\": {\"type\": \"keyword\"}}}}, " +
"\"mappings\": {\"properties\": {\"field\": {\"type\": \"keyword\"}}}, " +
"\"aliases\": {\"" + alias + "\": {\"is_write_index\": true}} }");
assertOK(leaderClient.performRequest(createIndexRequest));
// Check that the new index is created

View File

@ -82,8 +82,6 @@ public class DataLoader {
}
createIndex.endObject();
createIndex.startObject("mappings");
{
createIndex.startObject("_doc");
{
createIndex.startObject("properties");
{
@ -128,8 +126,6 @@ public class DataLoader {
}
createIndex.endObject();
}
createIndex.endObject();
}
createIndex.endObject().endObject();
request.setJsonEntity(Strings.toString(createIndex));
client.performRequest(request);
@ -210,8 +206,6 @@ public class DataLoader {
}
createIndex.endObject();
createIndex.startObject("mappings");
{
createIndex.startObject("_doc");
{
createIndex.startObject("properties");
{
@ -226,8 +220,6 @@ public class DataLoader {
}
createIndex.endObject();
}
createIndex.endObject();
}
createIndex.endObject().endObject();
request.setJsonEntity(Strings.toString(createIndex));
client.performRequest(request);
@ -262,8 +254,6 @@ public class DataLoader {
}
createIndex.endObject();
createIndex.startObject("mappings");
{
createIndex.startObject("_doc");
{
createIndex.startObject("properties");
{
@ -274,8 +264,6 @@ public class DataLoader {
}
createIndex.endObject();
}
createIndex.endObject();
}
createIndex.endObject().endObject();
request.setJsonEntity(Strings.toString(createIndex));
client.performRequest(request);

View File

@ -29,8 +29,6 @@ public class FetchSizeTestCase extends JdbcIntegrationTestCase {
Request request = new Request("PUT", "/test");
XContentBuilder createIndex = JsonXContent.contentBuilder().startObject();
createIndex.startObject("mappings");
{
createIndex.startObject("_doc");
{
createIndex.startObject("properties");
{
@ -42,8 +40,6 @@ public class FetchSizeTestCase extends JdbcIntegrationTestCase {
}
createIndex.endObject();
}
createIndex.endObject();
}
createIndex.endObject().endObject();
request.setJsonEntity(Strings.toString(createIndex));
client().performRequest(request);

View File

@ -6,7 +6,6 @@ setup:
index: foo
body:
mappings:
_doc:
properties:
the_field:
type: date

View File

@ -6,7 +6,6 @@ setup:
index: foo
body:
mappings:
_doc:
properties:
the_field:
type: date

View File

@ -6,7 +6,6 @@ setup:
index: foo
body:
mappings:
_doc:
properties:
the_field:
type: date

View File

@ -6,7 +6,6 @@ setup:
index: foo
body:
mappings:
_doc:
properties:
timestamp:
type: date
@ -51,7 +50,6 @@ setup:
body:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T05:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -67,7 +65,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T06:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -83,7 +80,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T07:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -99,7 +95,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -115,7 +110,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -319,7 +313,6 @@ setup:
body:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T05:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -335,7 +328,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T06:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -351,7 +343,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T07:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -367,7 +358,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -383,7 +373,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -467,7 +456,6 @@ setup:
body:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T05:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -483,7 +471,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T06:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -499,7 +486,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T07:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -515,7 +501,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -531,7 +516,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -617,7 +601,6 @@ setup:
body:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T05:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -633,7 +616,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T06:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -649,7 +631,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T07:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -665,7 +646,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -681,7 +661,6 @@ setup:
- index:
_index: "foo_rollup"
_type: "_doc"
- timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
timestamp.date_histogram.interval: "1h"
timestamp.date_histogram.time_zone: "UTC"
@ -760,7 +739,6 @@ setup:
index: bar
body:
mappings:
_doc:
properties:
timestamp:
type: date
@ -849,7 +827,6 @@ setup:
index: bar
body:
mappings:
_doc:
properties:
timestamp:
type: date

View File

@ -48,7 +48,6 @@ teardown:
index: foo
body:
mappings:
_doc:
properties:
timestamp:
type: date
@ -59,7 +58,6 @@ teardown:
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
index:
index: foo
type: _doc
body:
timestamp: 123
value_field: 1232
@ -69,7 +67,6 @@ teardown:
index: foobar
body:
mappings:
_doc:
properties:
timestamp:
type: date
@ -80,7 +77,6 @@ teardown:
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
index:
index: foobar
type: _doc
body:
timestamp: 123
value_field: 456
@ -220,7 +216,6 @@ teardown:
index: foo
body:
mappings:
_doc:
properties:
timestamp:
type: date
@ -233,7 +228,6 @@ teardown:
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
index:
index: foo
type: _doc
body:
timestamp: 123
value_field: 1232
@ -243,7 +237,6 @@ teardown:
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
index:
index: foobar
type: _doc
body:
timestamp: 123
value_field: 456

View File

@ -6,7 +6,6 @@ setup:
index: foo
body:
mappings:
_doc:
properties:
the_field:
type: date

View File

@ -6,7 +6,6 @@ setup:
index: foo
body:
mappings:
_doc:
properties:
the_field:
type: date

View File

@ -69,7 +69,7 @@ public class RollupIT extends ESRestTestCase {
try (XContentBuilder builder = jsonBuilder()) {
builder.startObject();
{
builder.startObject("mappings").startObject("_doc")
builder.startObject("mappings")
.startObject("properties")
.startObject("timestamp")
.field("type", "date")
@ -78,7 +78,6 @@ public class RollupIT extends ESRestTestCase {
.startObject("value")
.field("type", "integer")
.endObject()
.endObject()
.endObject().endObject();
}
builder.endObject();