-
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
Merged
jtibshirani
merged 11 commits into
elastic:master
from
jtibshirani:deprecate-types-in-put-mapping
Jan 18, 2019
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5446f57
Deprecate types in the put mapping API.
jtibshirani 79a17d6
Remove a REST test that is already covered by a unit test.
jtibshirani 49964a0
Add a typeless put mapping method to the HLRC.
jtibshirani 4b7fb35
Remove PutMappingRequest#source(Object... source), as it is not as us…
jtibshirani 9aad5dd
Make sure to use the right xContent type in PutMappingRequest#toXCont…
jtibshirani 634d1e0
Merge remote-tracking branch 'upstream/master' into deprecate-types-i…
jtibshirani b96cb3a
Merge remote-tracking branch 'upstream/master' into deprecate-types-i…
jtibshirani 6335ca5
Make sure RestHighLevelClientTests can handle two methods with the sa…
jtibshirani 314fb3b
Fix static analysis violations.
jtibshirani 393877a
Correct a deprecation comment in IndicesClient.
jtibshirani 8ba19ac
Fix IndicesClientDocumentationIT#testAnalyze.
jtibshirani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
224 changes: 224 additions & 0 deletions
224
client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutMappingRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| /* | ||
| * 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.Strings; | ||
| 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; | ||
| } | ||
|
|
||
| /** | ||
| * A specialized simplified mapping source method, takes the form of simple properties definition: | ||
| * ("field1", "type=string,store=true"). | ||
| * | ||
| * Also supports metadata mapping fields such as `_all` and `_parent` as property definition, these metadata | ||
| * mapping fields will automatically be put on the top level mapping object. | ||
| */ | ||
| public PutMappingRequest source(Object... source) { | ||
cbuescher marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return source(buildFromSimplifiedDef(source)); | ||
| } | ||
|
|
||
| /** | ||
| * @param source A list of objects representing field andproperties pairs (e.g. "field", "type=keyword"). | ||
| * | ||
| * @throws IllegalArgumentException if the number of the source arguments is not divisible by two. | ||
| * @return the mappings definition | ||
| */ | ||
| private static XContentBuilder buildFromSimplifiedDef(Object... source) { | ||
jtibshirani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (source.length % 2 != 0) { | ||
| throw new IllegalArgumentException("mapping source must be pairs of field names and properties definitions."); | ||
| } | ||
| try { | ||
| XContentBuilder builder = XContentFactory.jsonBuilder(); | ||
| builder.startObject(); | ||
|
|
||
| for (int i = 0; i < source.length; i++) { | ||
| String fieldName = source[i++].toString(); | ||
| if (RESERVED_FIELDS.contains(fieldName)) { | ||
| builder.startObject(fieldName); | ||
| String[] s1 = Strings.splitStringByCommaToArray(source[i].toString()); | ||
| for (String s : s1) { | ||
| String[] s2 = Strings.split(s, "="); | ||
| if (s2.length != 2) { | ||
| throw new IllegalArgumentException("malformed " + s); | ||
| } | ||
| builder.field(s2[0], s2[1]); | ||
| } | ||
| builder.endObject(); | ||
| } | ||
| } | ||
|
|
||
| builder.startObject("properties"); | ||
| for (int i = 0; i < source.length; i++) { | ||
| String fieldName = source[i++].toString(); | ||
| if (RESERVED_FIELDS.contains(fieldName)) { | ||
| continue; | ||
| } | ||
|
|
||
| builder.startObject(fieldName); | ||
| String[] s1 = Strings.splitStringByCommaToArray(source[i].toString()); | ||
| for (String s : s1) { | ||
| String[] s2 = Strings.split(s, "="); | ||
| if (s2.length != 2) { | ||
| throw new IllegalArgumentException("malformed " + s); | ||
| } | ||
| builder.field(s2[0], s2[1]); | ||
| } | ||
| builder.endObject(); | ||
| } | ||
| builder.endObject(); | ||
| builder.endObject(); | ||
| return builder; | ||
| } catch (Exception e) { | ||
| throw new IllegalArgumentException("failed to generate simplified mapping definition", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 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.JSON); | ||
jtibshirani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } else { | ||
| builder.startObject().endObject(); | ||
| } | ||
| return builder; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.