-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Deprecate types in the put mapping API. #37280
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
Changes from all commits
5446f57
79a17d6
49964a0
4b7fb35
9aad5dd
634d1e0
b96cb3a
6335ca5
314fb3b
393877a
8ba19ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.client.indices; | ||
|
|
||
| import com.carrotsearch.hppc.ObjectHashSet; | ||
| import org.elasticsearch.ElasticsearchGenerationException; | ||
| import org.elasticsearch.action.IndicesRequest; | ||
| import org.elasticsearch.action.support.IndicesOptions; | ||
| import org.elasticsearch.client.TimedRequest; | ||
| import org.elasticsearch.common.bytes.BytesArray; | ||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.xcontent.ToXContent; | ||
| import org.elasticsearch.common.xcontent.ToXContentObject; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.common.xcontent.XContentFactory; | ||
| import org.elasticsearch.common.xcontent.XContentType; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Put a mapping definition into one or more indices. If an index already contains mappings, | ||
| * the new mappings will be merged with the existing one. If there are elements that cannot | ||
| * be merged, the request will be rejected. | ||
| */ | ||
| public class PutMappingRequest extends TimedRequest implements IndicesRequest, ToXContentObject { | ||
|
|
||
| private static ObjectHashSet<String> RESERVED_FIELDS = ObjectHashSet.from( | ||
| "_uid", "_id", "_type", "_source", "_all", "_analyzer", "_parent", "_routing", "_index", | ||
| "_size", "_timestamp", "_ttl", "_field_names" | ||
| ); | ||
|
|
||
| private final String[] indices; | ||
| private IndicesOptions indicesOptions = IndicesOptions.fromOptions(false, false, true, true); | ||
|
|
||
| private BytesReference source; | ||
| private XContentType xContentType; | ||
|
|
||
| /** | ||
| * Constructs a new put mapping request against one or more indices. If no indices | ||
| * are provided then it will be executed against all indices. | ||
| */ | ||
| public PutMappingRequest(String... indices) { | ||
| this.indices = indices; | ||
| } | ||
|
|
||
| /** | ||
| * The indices into which the mappings will be put. | ||
| */ | ||
| @Override | ||
| public String[] indices() { | ||
| return indices; | ||
| } | ||
|
|
||
| @Override | ||
| public IndicesOptions indicesOptions() { | ||
| return indicesOptions; | ||
| } | ||
|
|
||
| public PutMappingRequest indicesOptions(IndicesOptions indicesOptions) { | ||
| this.indicesOptions = indicesOptions; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * The mapping source definition. | ||
| */ | ||
| public BytesReference source() { | ||
| return source; | ||
| } | ||
|
|
||
| /** | ||
| * The {@link XContentType} of the mapping source. | ||
| */ | ||
| public XContentType xContentType() { | ||
| return xContentType; | ||
| } | ||
|
|
||
| /** | ||
| * The mapping source definition. | ||
| * | ||
| * Note that the definition should *not* be nested under a type name. | ||
| */ | ||
| public PutMappingRequest source(Map<String, ?> mappingSource) { | ||
| try { | ||
| XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); | ||
| builder.map(mappingSource); | ||
| return source(builder); | ||
| } catch (IOException e) { | ||
| throw new ElasticsearchGenerationException("Failed to generate [" + mappingSource + "]", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The mapping source definition. | ||
| * | ||
| * Note that the definition should *not* be nested under a type name. | ||
| */ | ||
| public PutMappingRequest source(String mappingSource, XContentType xContentType) { | ||
| this.source = new BytesArray(mappingSource); | ||
| this.xContentType = xContentType; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * The mapping source definition. | ||
| * | ||
| * Note that the definition should *not* be nested under a type name. | ||
| */ | ||
| public PutMappingRequest source(XContentBuilder builder) { | ||
| this.source = BytesReference.bytes(builder); | ||
| this.xContentType = builder.contentType(); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * The mapping source definition. | ||
| * | ||
| * Note that the definition should *not* be nested under a type name. | ||
| */ | ||
| public PutMappingRequest source(BytesReference source, XContentType xContentType) { | ||
| this.source = source; | ||
| this.xContentType = xContentType; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
| if (source != null) { | ||
| try (InputStream stream = source.streamInput()) { | ||
| builder.rawValue(stream, xContentType); | ||
| } | ||
| } else { | ||
| builder.startObject().endObject(); | ||
| } | ||
| return builder; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,7 +47,6 @@ | |
| import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse; | ||
| import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest; | ||
| import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse; | ||
| import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; | ||
| import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; | ||
| import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; | ||
| import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; | ||
|
|
@@ -74,6 +73,7 @@ | |
| import org.elasticsearch.client.indices.FreezeIndexRequest; | ||
| import org.elasticsearch.client.indices.GetIndexTemplatesRequest; | ||
| import org.elasticsearch.client.indices.IndexTemplatesExistRequest; | ||
| import org.elasticsearch.client.indices.PutMappingRequest; | ||
| import org.elasticsearch.client.indices.UnfreezeIndexRequest; | ||
| import org.elasticsearch.cluster.metadata.AliasMetaData; | ||
| import org.elasticsearch.cluster.metadata.IndexMetaData; | ||
|
|
@@ -93,6 +93,7 @@ | |
| import org.elasticsearch.index.query.QueryBuilder; | ||
| import org.elasticsearch.index.query.QueryBuilders; | ||
| import org.elasticsearch.rest.RestStatus; | ||
| import org.elasticsearch.rest.action.admin.indices.RestPutMappingAction; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
|
|
@@ -404,32 +405,56 @@ public void testGetIndexNonExistentIndex() throws IOException { | |
| } | ||
|
|
||
| public void testPutMapping() throws IOException { | ||
| // Add mappings to index | ||
| String indexName = "mapping_index"; | ||
| createIndex(indexName, Settings.EMPTY); | ||
|
|
||
| PutMappingRequest putMappingRequest = new PutMappingRequest(indexName); | ||
| putMappingRequest.type("_doc"); | ||
| XContentBuilder mappingBuilder = JsonXContent.contentBuilder(); | ||
| mappingBuilder.startObject().startObject("properties").startObject("field"); | ||
| mappingBuilder.field("type", "text"); | ||
| mappingBuilder.endObject().endObject().endObject(); | ||
| putMappingRequest.source(mappingBuilder); | ||
|
|
||
| AcknowledgedResponse putMappingResponse = | ||
| execute(putMappingRequest, highLevelClient().indices()::putMapping, highLevelClient().indices()::putMappingAsync); | ||
| AcknowledgedResponse putMappingResponse = execute(putMappingRequest, | ||
| highLevelClient().indices()::putMapping, | ||
| highLevelClient().indices()::putMappingAsync); | ||
| assertTrue(putMappingResponse.isAcknowledged()); | ||
|
|
||
| Map<String, Object> getIndexResponse = getAsMap(indexName); | ||
| assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", getIndexResponse)); | ||
| assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", | ||
| getIndexResponse)); | ||
| } | ||
|
|
||
| public void testPutMappingWithTypes() throws IOException { | ||
| String indexName = "mapping_index"; | ||
| createIndex(indexName, Settings.EMPTY); | ||
|
|
||
| org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest putMappingRequest = | ||
| new org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest(indexName); | ||
| putMappingRequest.type("some_type"); | ||
|
|
||
| XContentBuilder mappingBuilder = JsonXContent.contentBuilder(); | ||
| mappingBuilder.startObject().startObject("properties").startObject("field"); | ||
| mappingBuilder.field("type", "text"); | ||
| mappingBuilder.endObject().endObject().endObject(); | ||
| putMappingRequest.source(mappingBuilder); | ||
|
|
||
| AcknowledgedResponse putMappingResponse = execute(putMappingRequest, | ||
| highLevelClient().indices()::putMapping, | ||
| highLevelClient().indices()::putMappingAsync, | ||
| expectWarnings(RestPutMappingAction.TYPES_DEPRECATION_MESSAGE)); | ||
| assertTrue(putMappingResponse.isAcknowledged()); | ||
|
|
||
| Map<String, Object> getIndexResponse = getAsMap(indexName); | ||
| assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", | ||
| getIndexResponse)); | ||
| } | ||
|
|
||
| public void testGetMapping() throws IOException { | ||
| String indexName = "test"; | ||
| createIndex(indexName, Settings.EMPTY); | ||
|
|
||
| PutMappingRequest putMappingRequest = new PutMappingRequest(indexName); | ||
| putMappingRequest.type("_doc"); | ||
| XContentBuilder mappingBuilder = JsonXContent.contentBuilder(); | ||
| mappingBuilder.startObject().startObject("properties").startObject("field"); | ||
| mappingBuilder.field("type", "text"); | ||
|
|
@@ -465,7 +490,6 @@ public void testGetFieldMapping() throws IOException { | |
| createIndex(indexName, Settings.EMPTY); | ||
|
|
||
| PutMappingRequest putMappingRequest = new PutMappingRequest(indexName); | ||
| putMappingRequest.type("_doc"); | ||
| XContentBuilder mappingBuilder = JsonXContent.contentBuilder(); | ||
| mappingBuilder.startObject().startObject("properties").startObject("field"); | ||
| mappingBuilder.field("type", "text"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a little later in this test method we still have a type ref: @jpountz said in a recent email thread "we should feel free to use better objects"
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR is just focused on put mappings (and not get mappings), so I think we should plan to address that in a subsequent PR. |
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.