From 1bebc05801aa4276c66b0a7db94687fce173149e Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Thu, 24 Jan 2019 13:17:47 -0800 Subject: [PATCH 1/7] Deprecate types in create index requests. (#37134) From #29453 and #37285, the include_type_name parameter was already present and defaulted to false. This PR makes the following updates: * Add deprecation warnings to RestCreateIndexAction, plus tests in RestCreateIndexActionTests. * Add a typeless 'create index' method to the Java HLRC, and deprecate the old typed version. To do this cleanly, I created new CreateIndexRequest and CreateIndexResponse objects that differ from the existing server ones. --- .../elasticsearch/client/IndicesClient.java | 75 +++- .../client/IndicesRequestConverters.java | 17 +- .../client/indices/CreateIndexRequest.java | 364 ++++++++++++++++++ .../client/indices/CreateIndexResponse.java | 74 ++++ .../java/org/elasticsearch/client/CCRIT.java | 4 +- .../client/ESRestHighLevelClientTestCase.java | 2 +- .../elasticsearch/client/IndicesClientIT.java | 100 ++++- .../client/IndicesRequestConvertersTests.java | 38 +- .../client/MachineLearningIT.java | 59 ++- .../documentation/CCRDocumentationIT.java | 4 +- .../documentation/CRUDDocumentationIT.java | 22 +- .../ClusterClientDocumentationIT.java | 2 +- .../documentation/ILMDocumentationIT.java | 18 +- .../IndicesClientDocumentationIT.java | 51 ++- .../MlClientDocumentationIT.java | 36 +- .../documentation/SearchDocumentationIT.java | 32 +- .../SnapshotClientDocumentationIT.java | 2 +- .../indices/CreateIndexRequestTests.java | 93 +++++ .../indices/PutMappingRequestTests.java | 1 - .../indices/RandomCreateIndexGenerator.java | 61 +++ .../high-level/indices/create_index.asciidoc | 7 - .../admin/indices/RestCreateIndexAction.java | 16 +- .../mapping/put/PutMappingRequestTests.java | 2 +- .../indices/RestCreateIndexActionTests.java | 62 +++ .../index/RandomCreateIndexGenerator.java | 20 +- .../test/AbstractXContentTestCase.java | 2 +- 26 files changed, 1035 insertions(+), 129 deletions(-) create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexResponse.java create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CreateIndexRequestTests.java create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/indices/RandomCreateIndexGenerator.java create mode 100644 server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java index 961e8f1699a54..10a65b2f9da8d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java @@ -28,8 +28,6 @@ import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest; import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse; import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; -import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; -import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.flush.FlushRequest; import org.elasticsearch.action.admin.indices.flush.FlushResponse; @@ -60,6 +58,8 @@ import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.core.ShardsAcknowledgedResponse; +import org.elasticsearch.client.indices.CreateIndexRequest; +import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.FreezeIndexRequest; import org.elasticsearch.client.indices.GetIndexTemplatesRequest; import org.elasticsearch.client.indices.IndexTemplatesExistRequest; @@ -147,9 +147,10 @@ public void deleteAsync(DeleteIndexRequest deleteIndexRequest, ActionListener * See * Create Index API on elastic.co - * @deprecated Prefer {@link #create(CreateIndexRequest, RequestOptions)} + * + * @deprecated This method uses an old request object which still refers to types, a deprecated feature. The + * method {@link #create(CreateIndexRequest, RequestOptions)} should be used instead, which accepts a new + * request object and also uses request options instead of headers. */ @Deprecated - public CreateIndexResponse create(CreateIndexRequest createIndexRequest, Header... headers) throws IOException { + public CreateIndexResponse create( + org.elasticsearch.action.admin.indices.create.CreateIndexRequest createIndexRequest, + Header... headers) throws IOException { return restHighLevelClient.performRequestAndParseEntity(createIndexRequest, IndicesRequestConverters::createIndex, CreateIndexResponse::fromXContent, emptySet(), headers); } + /** + * Creates an index using the Create Index API. + * See + * Create Index API on elastic.co + * @param createIndexRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @return the response + * @throws IOException in case there is a problem sending the request or parsing back the response + * + * @deprecated This method uses an old request object which still refers to types, a deprecated feature. The + * method {@link #create(CreateIndexRequest, RequestOptions)} should be used instead, which accepts a new + * request object. + */ + @Deprecated + public org.elasticsearch.action.admin.indices.create.CreateIndexResponse create( + org.elasticsearch.action.admin.indices.create.CreateIndexRequest createIndexRequest, + RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(createIndexRequest, + IndicesRequestConverters::createIndex, options, + org.elasticsearch.action.admin.indices.create.CreateIndexResponse::fromXContent, emptySet()); + } + /** * Asynchronously creates an index using the Create Index API. * See @@ -173,9 +201,32 @@ public CreateIndexResponse create(CreateIndexRequest createIndexRequest, Header. * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @param listener the listener to be notified upon request completion */ - public void createAsync(CreateIndexRequest createIndexRequest, RequestOptions options, ActionListener listener) { + public void createAsync(CreateIndexRequest createIndexRequest, + RequestOptions options, + ActionListener listener) { restHighLevelClient.performRequestAsyncAndParseEntity(createIndexRequest, IndicesRequestConverters::createIndex, options, - CreateIndexResponse::fromXContent, listener, emptySet()); + CreateIndexResponse::fromXContent, listener, emptySet()); + } + + /** + * Asynchronously creates an index using the Create Index API. + * See + * Create Index API on elastic.co + * @param createIndexRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion + * + * @deprecated This method uses an old request object which still refers to types, a deprecated feature. The + * method {@link #createAsync(CreateIndexRequest, RequestOptions, ActionListener)} should be used instead, + * which accepts a new request object. + */ + @Deprecated + public void createAsync(org.elasticsearch.action.admin.indices.create.CreateIndexRequest createIndexRequest, + RequestOptions options, + ActionListener listener) { + restHighLevelClient.performRequestAsyncAndParseEntity(createIndexRequest, + IndicesRequestConverters::createIndex, options, + org.elasticsearch.action.admin.indices.create.CreateIndexResponse::fromXContent, listener, emptySet()); } /** @@ -183,10 +234,14 @@ public void createAsync(CreateIndexRequest createIndexRequest, RequestOptions op *

* See * Create Index API on elastic.co - * @deprecated Prefer {@link #createAsync(CreateIndexRequest, RequestOptions, ActionListener)} + * + * @deprecated This method uses an old request object which still refers to types, a deprecated feature. The + * method {@link #createAsync(CreateIndexRequest, RequestOptions, ActionListener)} should be used instead, + * which accepts a new request object and also uses request objects instead of headers. */ @Deprecated - public void createAsync(CreateIndexRequest createIndexRequest, ActionListener listener, Header... headers) { + public void createAsync(org.elasticsearch.action.admin.indices.create.CreateIndexRequest createIndexRequest, + ActionListener listener, Header... headers) { restHighLevelClient.performRequestAsyncAndParseEntity(createIndexRequest, IndicesRequestConverters::createIndex, CreateIndexResponse::fromXContent, listener, emptySet(), headers); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java index 798b1d9582155..a6be15d2f0b0d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java @@ -29,7 +29,6 @@ import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest; import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest; import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; -import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.flush.FlushRequest; import org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest; @@ -48,6 +47,7 @@ import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; import org.elasticsearch.action.support.ActiveShardCount; +import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.FreezeIndexRequest; import org.elasticsearch.client.indices.GetIndexTemplatesRequest; import org.elasticsearch.client.indices.IndexTemplatesExistRequest; @@ -99,6 +99,21 @@ static Request closeIndex(CloseIndexRequest closeIndexRequest) { } static Request createIndex(CreateIndexRequest createIndexRequest) throws IOException { + String endpoint = new RequestConverters.EndpointBuilder() + .addPathPart(createIndexRequest.index()).build(); + Request request = new Request(HttpPut.METHOD_NAME, endpoint); + + RequestConverters.Params parameters = new RequestConverters.Params(request); + parameters.withTimeout(createIndexRequest.timeout()); + parameters.withMasterTimeout(createIndexRequest.masterNodeTimeout()); + parameters.withWaitForActiveShards(createIndexRequest.waitForActiveShards(), ActiveShardCount.DEFAULT); + + request.setEntity(RequestConverters.createEntity(createIndexRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); + return request; + } + + static Request createIndex(org.elasticsearch.action.admin.indices.create.CreateIndexRequest createIndexRequest) + throws IOException { String endpoint = RequestConverters.endpoint(createIndexRequest.indices()); Request request = new Request(HttpPut.METHOD_NAME, endpoint); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java new file mode 100644 index 0000000000000..f0bff6e6f4307 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java @@ -0,0 +1,364 @@ +/* + * 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 org.elasticsearch.ElasticsearchGenerationException; +import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.action.admin.indices.alias.Alias; +import org.elasticsearch.action.support.ActiveShardCount; +import org.elasticsearch.client.TimedRequest; +import org.elasticsearch.client.Validatable; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.bytes.BytesArray; +import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.DeprecationHandler; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; +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 java.io.IOException; +import java.io.InputStream; +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS; + +/** + * A request to create an index. + */ +public class CreateIndexRequest extends TimedRequest implements Validatable, ToXContentObject { + static final ParseField MAPPINGS = new ParseField("mappings"); + static final ParseField SETTINGS = new ParseField("settings"); + static final ParseField ALIASES = new ParseField("aliases"); + + private final String index; + private Settings settings = EMPTY_SETTINGS; + + private BytesReference mappings; + private XContentType mappingsXContentType; + + private final Set aliases = new HashSet<>(); + + private ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT; + + /** + * Constructs a new request to create an index with the specified name. + */ + public CreateIndexRequest(String index) { + if (index == null) { + throw new IllegalArgumentException("The index name cannot be null."); + } + this.index = index; + } + + /** + * The name of the index to create. + */ + public String index() { + return index; + } + + /** + * The settings to create the index with. + */ + public Settings settings() { + return settings; + } + + /** + * The settings to create the index with. + */ + public CreateIndexRequest settings(Settings.Builder settings) { + this.settings = settings.build(); + return this; + } + + /** + * The settings to create the index with. + */ + public CreateIndexRequest settings(Settings settings) { + this.settings = settings; + return this; + } + + /** + * The settings to create the index with (either json or yaml format) + */ + public CreateIndexRequest settings(String source, XContentType xContentType) { + this.settings = Settings.builder().loadFromSource(source, xContentType).build(); + return this; + } + + /** + * Allows to set the settings using a json builder. + */ + public CreateIndexRequest settings(XContentBuilder builder) { + settings(Strings.toString(builder), builder.contentType()); + return this; + } + + /** + * The settings to create the index with (either json/yaml/properties format) + */ + public CreateIndexRequest settings(Map source) { + try { + XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); + builder.map(source); + settings(Strings.toString(builder), XContentType.JSON); + } catch (IOException e) { + throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); + } + return this; + } + + public BytesReference mappings() { + return mappings; + } + + public XContentType mappingsXContentType() { + return mappingsXContentType; + } + + /** + * Adds mapping that will be added when the index gets created. + * + * Note that the definition should *not* be nested under a type name. + * + * @param source The mapping source + * @param xContentType The content type of the source + */ + public CreateIndexRequest mapping(String source, XContentType xContentType) { + return mapping(new BytesArray(source), xContentType); + } + + /** + * Adds mapping that will be added when the index gets created. + * + * Note that the definition should *not* be nested under a type name. + * + * @param source The mapping source + */ + public CreateIndexRequest mapping(XContentBuilder source) { + return mapping(BytesReference.bytes(source), source.contentType()); + } + + /** + * Adds mapping that will be added when the index gets created. + * + * Note that the definition should *not* be nested under a type name. + * + * @param source The mapping source + */ + public CreateIndexRequest mapping(Map source) { + try { + XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); + builder.map(source); + return mapping(BytesReference.bytes(builder), builder.contentType()); + } catch (IOException e) { + throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); + } + } + + /** + * Adds mapping that will be added when the index gets created. + * + * Note that the definition should *not* be nested under a type name. + * + * @param source The mapping source + * @param xContentType the content type of the mapping source + */ + public CreateIndexRequest mapping(BytesReference source, XContentType xContentType) { + Objects.requireNonNull(xContentType); + mappings = source; + mappingsXContentType = xContentType; + return this; + } + + public Set aliases() { + return this.aliases; + } + + /** + * Sets the aliases that will be associated with the index when it gets created + */ + public CreateIndexRequest aliases(Map source) { + try { + XContentBuilder builder = XContentFactory.jsonBuilder(); + builder.map(source); + return aliases(BytesReference.bytes(builder), builder.contentType()); + } catch (IOException e) { + throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); + } + } + + /** + * Sets the aliases that will be associated with the index when it gets created + */ + public CreateIndexRequest aliases(XContentBuilder source) { + return aliases(BytesReference.bytes(source), source.contentType()); + } + + /** + * Sets the aliases that will be associated with the index when it gets created + */ + public CreateIndexRequest aliases(String source, XContentType contentType) { + return aliases(new BytesArray(source), contentType); + } + + /** + * Sets the aliases that will be associated with the index when it gets created + */ + public CreateIndexRequest aliases(BytesReference source, XContentType contentType) { + // EMPTY is safe here because we never call namedObject + try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, source, contentType)) { + //move to the first alias + parser.nextToken(); + while ((parser.nextToken()) != XContentParser.Token.END_OBJECT) { + alias(Alias.fromXContent(parser)); + } + return this; + } catch(IOException e) { + throw new ElasticsearchParseException("Failed to parse aliases", e); + } + } + + /** + * Adds an alias that will be associated with the index when it gets created + */ + public CreateIndexRequest alias(Alias alias) { + this.aliases.add(alias); + return this; + } + + /** + * Adds aliases that will be associated with the index when it gets created + */ + public CreateIndexRequest aliases(Collection aliases) { + this.aliases.addAll(aliases); + return this; + } + + /** + * Sets the settings and mappings as a single source. + * + * Note that the mapping definition should *not* be nested under a type name. + */ + public CreateIndexRequest source(String source, XContentType xContentType) { + return source(new BytesArray(source), xContentType); + } + + /** + * Sets the settings and mappings as a single source. + * + * Note that the mapping definition should *not* be nested under a type name. + */ + public CreateIndexRequest source(XContentBuilder source) { + return source(BytesReference.bytes(source), source.contentType()); + } + + /** + * Sets the settings and mappings as a single source. + * + * Note that the mapping definition should *not* be nested under a type name. + */ + public CreateIndexRequest source(BytesReference source, XContentType xContentType) { + Objects.requireNonNull(xContentType); + source(XContentHelper.convertToMap(source, false, xContentType).v2()); + return this; + } + + /** + * Sets the settings and mappings as a single source. + * + * Note that the mapping definition should *not* be nested under a type name. + */ + @SuppressWarnings("unchecked") + public CreateIndexRequest source(Map source) { + DeprecationHandler deprecationHandler = DeprecationHandler.THROW_UNSUPPORTED_OPERATION; + for (Map.Entry entry : source.entrySet()) { + String name = entry.getKey(); + if (SETTINGS.match(name, deprecationHandler)) { + settings((Map) entry.getValue()); + } else if (MAPPINGS.match(name, deprecationHandler)) { + mapping((Map) entry.getValue()); + } else if (ALIASES.match(name, deprecationHandler)) { + aliases((Map) entry.getValue()); + } + } + return this; + } + + public ActiveShardCount waitForActiveShards() { + return waitForActiveShards; + } + + /** + * Sets the number of shard copies that should be active for index creation to return. + * Defaults to {@link ActiveShardCount#DEFAULT}, which will wait for one shard copy + * (the primary) to become active. Set this value to {@link ActiveShardCount#ALL} to + * wait for all shards (primary and all replicas) to be active before returning. + * Otherwise, use {@link ActiveShardCount#from(int)} to set this value to any + * non-negative integer, up to the number of copies per shard (number of replicas + 1), + * to wait for the desired amount of shard copies to become active before returning. + * Index creation will only wait up until the timeout value for the number of shard copies + * to be active before returning. Check {@link CreateIndexResponse#isShardsAcknowledged()} to + * determine if the requisite shard copies were all started before returning or timing out. + * + * @param waitForActiveShards number of active shard copies to wait on + */ + public CreateIndexRequest waitForActiveShards(ActiveShardCount waitForActiveShards) { + this.waitForActiveShards = waitForActiveShards; + return this; + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + + builder.startObject(SETTINGS.getPreferredName()); + settings.toXContent(builder, params); + builder.endObject(); + + if (mappings != null) { + try (InputStream stream = mappings.streamInput()) { + builder.rawField(MAPPINGS.getPreferredName(), stream, mappingsXContentType); + } + } + + builder.startObject(ALIASES.getPreferredName()); + for (Alias alias : aliases) { + alias.toXContent(builder, params); + } + builder.endObject(); + + builder.endObject(); + return builder; + } +} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexResponse.java new file mode 100644 index 0000000000000..9755edd680f53 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexResponse.java @@ -0,0 +1,74 @@ +/* + * 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 org.elasticsearch.action.support.master.ShardsAcknowledgedResponse; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.util.Objects; + +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; + +/** + * A response for a create index action. + */ +public class CreateIndexResponse extends ShardsAcknowledgedResponse { + + private static final ParseField INDEX = new ParseField("index"); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("create_index", + true, args -> new CreateIndexResponse((boolean) args[0], (boolean) args[1], (String) args[2])); + + static { + declareAcknowledgedAndShardsAcknowledgedFields(PARSER); + PARSER.declareField(constructorArg(), (parser, context) -> parser.textOrNull(), INDEX, ObjectParser.ValueType.STRING_OR_NULL); + } + + private String index; + + public CreateIndexResponse(boolean acknowledged, boolean shardsAcknowledged, String index) { + super(acknowledged, shardsAcknowledged); + this.index = index; + } + + public String index() { + return index; + } + + public static CreateIndexResponse fromXContent(XContentParser parser) { + return PARSER.apply(parser, null); + } + + @Override + public boolean equals(Object o) { + if (super.equals(o)) { + CreateIndexResponse that = (CreateIndexResponse) o; + return Objects.equals(index, that.index); + } + return false; + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), index); + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/CCRIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/CCRIT.java index 76f1997c713f1..9ab65448e45a0 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/CCRIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/CCRIT.java @@ -23,8 +23,6 @@ import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse; import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; -import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; -import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; @@ -44,6 +42,8 @@ import org.elasticsearch.client.ccr.ResumeFollowRequest; import org.elasticsearch.client.ccr.UnfollowRequest; import org.elasticsearch.client.core.AcknowledgedResponse; +import org.elasticsearch.client.indices.CreateIndexRequest; +import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentType; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ESRestHighLevelClientTestCase.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ESRestHighLevelClientTestCase.java index c44ce71747b22..af083dd700821 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ESRestHighLevelClientTestCase.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ESRestHighLevelClientTestCase.java @@ -22,11 +22,11 @@ import org.apache.http.Header; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; -import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.ingest.PutPipelineRequest; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.PlainActionFuture; +import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index 0e7c8be2c3407..4a1ec8e2c3633 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -33,8 +33,6 @@ import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest; import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse; import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; -import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; -import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.flush.FlushRequest; import org.elasticsearch.action.admin.indices.flush.FlushResponse; @@ -71,6 +69,8 @@ import org.elasticsearch.action.support.broadcast.BroadcastResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.core.ShardsAcknowledgedResponse; +import org.elasticsearch.client.indices.CreateIndexRequest; +import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.FreezeIndexRequest; import org.elasticsearch.client.indices.GetIndexTemplatesRequest; import org.elasticsearch.client.indices.IndexTemplatesExistRequest; @@ -94,6 +94,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.RestCreateIndexAction; import org.elasticsearch.rest.action.admin.indices.RestPutMappingAction; import java.io.IOException; @@ -192,9 +193,9 @@ public void testCreateIndex() throws IOException { CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName); - CreateIndexResponse createIndexResponse = - execute(createIndexRequest, highLevelClient().indices()::create, highLevelClient().indices()::createAsync, - highLevelClient().indices()::create, highLevelClient().indices()::createAsync); + CreateIndexResponse createIndexResponse = execute(createIndexRequest, + highLevelClient().indices()::create, + highLevelClient().indices()::createAsync); assertTrue(createIndexResponse.isAcknowledged()); assertTrue(indexExists(indexName)); @@ -219,11 +220,11 @@ public void testCreateIndex() throws IOException { mappingBuilder.startObject().startObject("properties").startObject("field"); mappingBuilder.field("type", "text"); mappingBuilder.endObject().endObject().endObject(); - createIndexRequest.mapping(MapperService.SINGLE_MAPPING_NAME, mappingBuilder); + createIndexRequest.mapping(mappingBuilder); - CreateIndexResponse createIndexResponse = - execute(createIndexRequest, highLevelClient().indices()::create, highLevelClient().indices()::createAsync, - highLevelClient().indices()::create, highLevelClient().indices()::createAsync); + CreateIndexResponse createIndexResponse = execute(createIndexRequest, + highLevelClient().indices()::create, + highLevelClient().indices()::createAsync); assertTrue(createIndexResponse.isAcknowledged()); Map getIndexResponse = getAsMap(indexName); @@ -241,6 +242,70 @@ public void testCreateIndex() throws IOException { } } + @SuppressWarnings({"unchecked", "rawtypes"}) + public void testCreateIndexWithTypes() throws IOException { + { + // Create index + String indexName = "plain_index"; + assertFalse(indexExists(indexName)); + + org.elasticsearch.action.admin.indices.create.CreateIndexRequest createIndexRequest = + new org.elasticsearch.action.admin.indices.create.CreateIndexRequest(indexName); + + org.elasticsearch.action.admin.indices.create.CreateIndexResponse createIndexResponse = execute( + createIndexRequest, + highLevelClient().indices()::create, + highLevelClient().indices()::createAsync, + expectWarnings(RestCreateIndexAction.TYPES_DEPRECATION_MESSAGE)); + assertTrue(createIndexResponse.isAcknowledged()); + + assertTrue(indexExists(indexName)); + } + { + // Create index with mappings, aliases and settings + String indexName = "rich_index"; + assertFalse(indexExists(indexName)); + + org.elasticsearch.action.admin.indices.create.CreateIndexRequest createIndexRequest = + new org.elasticsearch.action.admin.indices.create.CreateIndexRequest(indexName); + + Alias alias = new Alias("alias_name"); + alias.filter("{\"term\":{\"year\":2016}}"); + alias.routing("1"); + createIndexRequest.alias(alias); + + Settings.Builder settings = Settings.builder(); + settings.put(SETTING_NUMBER_OF_REPLICAS, 2); + createIndexRequest.settings(settings); + + XContentBuilder mappingBuilder = JsonXContent.contentBuilder(); + mappingBuilder.startObject().startObject("properties").startObject("field"); + mappingBuilder.field("type", "text"); + mappingBuilder.endObject().endObject().endObject(); + createIndexRequest.mapping(MapperService.SINGLE_MAPPING_NAME, mappingBuilder); + + org.elasticsearch.action.admin.indices.create.CreateIndexResponse createIndexResponse = execute( + createIndexRequest, + highLevelClient().indices()::create, + highLevelClient().indices()::createAsync, + expectWarnings(RestCreateIndexAction.TYPES_DEPRECATION_MESSAGE)); + assertTrue(createIndexResponse.isAcknowledged()); + + Map getIndexResponse = getAsMap(indexName); + assertEquals("2", XContentMapValues.extractValue(indexName + ".settings.index.number_of_replicas", getIndexResponse)); + + Map aliasData = + (Map)XContentMapValues.extractValue(indexName + ".aliases.alias_name", getIndexResponse); + assertNotNull(aliasData); + assertEquals("1", aliasData.get("index_routing")); + Map filter = (Map) aliasData.get("filter"); + Map term = (Map) filter.get("term"); + assertEquals(2016, term.get("year")); + + assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", getIndexResponse)); + } + } + public void testGetSettings() throws IOException { String indexName = "get_settings_index"; Settings basicSettings = Settings.builder() @@ -908,8 +973,14 @@ public void testShrink() throws IOException { ResizeRequest resizeRequest = new ResizeRequest("target", "source"); resizeRequest.setResizeType(ResizeType.SHRINK); - Settings targetSettings = Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0).build(); - resizeRequest.setTargetIndex(new CreateIndexRequest("target").settings(targetSettings).alias(new Alias("alias"))); + Settings targetSettings = + Settings.builder() + .put("index.number_of_shards", 2) + .put("index.number_of_replicas", 0) + .build(); + resizeRequest.setTargetIndex(new org.elasticsearch.action.admin.indices.create.CreateIndexRequest("target") + .settings(targetSettings) + .alias(new Alias("alias"))); ResizeResponse resizeResponse = execute(resizeRequest, highLevelClient().indices()::shrink, highLevelClient().indices()::shrinkAsync, highLevelClient().indices()::shrink, highLevelClient().indices()::shrinkAsync); assertTrue(resizeResponse.isAcknowledged()); @@ -932,9 +1003,10 @@ public void testSplit() throws IOException { ResizeRequest resizeRequest = new ResizeRequest("target", "source"); resizeRequest.setResizeType(ResizeType.SPLIT); Settings targetSettings = Settings.builder().put("index.number_of_shards", 4).put("index.number_of_replicas", 0).build(); - resizeRequest.setTargetIndex(new CreateIndexRequest("target").settings(targetSettings).alias(new Alias("alias"))); - ResizeResponse resizeResponse = execute(resizeRequest, highLevelClient().indices()::split, highLevelClient().indices()::splitAsync, - highLevelClient().indices()::split, highLevelClient().indices()::splitAsync); + resizeRequest.setTargetIndex(new org.elasticsearch.action.admin.indices.create.CreateIndexRequest("target") + .settings(targetSettings) + .alias(new Alias("alias"))); + ResizeResponse resizeResponse = execute(resizeRequest, highLevelClient().indices()::split, highLevelClient().indices()::splitAsync); assertTrue(resizeResponse.isAcknowledged()); assertTrue(resizeResponse.isShardsAcknowledged()); Map getIndexResponse = getAsMap("target"); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java index 16690aa2fc8e7..f2e8dd2d6d224 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java @@ -32,7 +32,6 @@ import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest; import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest; import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; -import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.flush.FlushRequest; import org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest; @@ -52,6 +51,8 @@ import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.action.support.master.AcknowledgedRequest; +import org.elasticsearch.client.indices.CreateIndexRequest; +import org.elasticsearch.client.indices.RandomCreateIndexGenerator; import org.elasticsearch.client.indices.GetIndexTemplatesRequest; import org.elasticsearch.client.indices.IndexTemplatesExistRequest; import org.elasticsearch.client.indices.PutMappingRequest; @@ -60,7 +61,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.index.RandomCreateIndexGenerator; import org.elasticsearch.test.ESTestCase; import org.junit.Assert; @@ -76,7 +76,6 @@ import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; import static org.elasticsearch.index.RandomCreateIndexGenerator.randomAliases; -import static org.elasticsearch.index.RandomCreateIndexGenerator.randomCreateIndexRequest; import static org.elasticsearch.index.RandomCreateIndexGenerator.randomIndexSettings; import static org.elasticsearch.index.alias.RandomAliasActionsGenerator.randomAliasAction; import static org.elasticsearch.rest.BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER; @@ -128,7 +127,23 @@ public void testIndicesExistEmptyIndices() { } public void testCreateIndex() throws IOException { - CreateIndexRequest createIndexRequest = randomCreateIndexRequest(); + CreateIndexRequest createIndexRequest = RandomCreateIndexGenerator.randomCreateIndexRequest(); + + Map expectedParams = new HashMap<>(); + RequestConvertersTests.setRandomTimeout(createIndexRequest, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); + RequestConvertersTests.setRandomMasterTimeout(createIndexRequest, expectedParams); + RequestConvertersTests.setRandomWaitForActiveShards(createIndexRequest::waitForActiveShards, expectedParams); + + Request request = IndicesRequestConverters.createIndex(createIndexRequest); + Assert.assertEquals("/" + createIndexRequest.index(), request.getEndpoint()); + Assert.assertEquals(expectedParams, request.getParameters()); + Assert.assertEquals(HttpPut.METHOD_NAME, request.getMethod()); + RequestConvertersTests.assertToXContentBody(createIndexRequest, request.getEntity()); + } + + public void testCreateIndexWithTypes() throws IOException { + org.elasticsearch.action.admin.indices.create.CreateIndexRequest createIndexRequest = + org.elasticsearch.index.RandomCreateIndexGenerator.randomCreateIndexRequest(); Map expectedParams = new HashMap<>(); RequestConvertersTests.setRandomTimeout(createIndexRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); @@ -143,8 +158,8 @@ public void testCreateIndex() throws IOException { } public void testCreateIndexNullIndex() { - ActionRequestValidationException validationException = new CreateIndexRequest(null).validate(); - Assert.assertNotNull(validationException); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new CreateIndexRequest(null)); + assertEquals(e.getMessage(), "The index name cannot be null."); } public void testUpdateAliases() throws IOException { @@ -712,7 +727,8 @@ private void resizeTest(ResizeType resizeType, CheckedFunction + request.mapping(// <1> "{\n" + - " \"_doc\": {\n" + - " \"properties\": {\n" + - " \"message\": {\n" + - " \"type\": \"text\"\n" + - " }\n" + + " \"properties\": {\n" + + " \"message\": {\n" + + " \"type\": \"text\"\n" + " }\n" + " }\n" + "}", // <2> @@ -326,7 +325,7 @@ public void testCreateIndex() throws IOException { Map mapping = new HashMap<>(); mapping.put("properties", properties); jsonMap.put("_doc", mapping); - request.mapping("_doc", jsonMap); // <1> + request.mapping(jsonMap); // <1> //end::create-index-mappings-map CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); @@ -352,19 +351,11 @@ public void testCreateIndex() throws IOException { builder.endObject(); } builder.endObject(); - request.mapping("_doc", builder); // <1> + request.mapping(builder); // <1> //end::create-index-mappings-xcontent CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); } - { - request = new CreateIndexRequest("twitter4"); - //tag::create-index-mappings-shortcut - request.mapping("_doc", "message", "type=text"); // <1> - //end::create-index-mappings-shortcut - CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); - assertTrue(createIndexResponse.isAcknowledged()); - } request = new CreateIndexRequest("twitter5"); // tag::create-index-request-aliases @@ -372,15 +363,13 @@ public void testCreateIndex() throws IOException { // end::create-index-request-aliases // tag::create-index-request-timeout - request.timeout(TimeValue.timeValueMinutes(2)); // <1> - request.timeout("2m"); // <2> + request.setTimeout(TimeValue.timeValueMinutes(2)); // <1> // end::create-index-request-timeout // tag::create-index-request-masterTimeout - request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1> - request.masterNodeTimeout("1m"); // <2> + request.setMasterTimeout(TimeValue.timeValueMinutes(1)); // <1> // end::create-index-request-masterTimeout // tag::create-index-request-waitForActiveShards - request.waitForActiveShards(2); // <1> + request.waitForActiveShards(ActiveShardCount.from(2)); // <1> request.waitForActiveShards(ActiveShardCount.DEFAULT); // <2> // end::create-index-request-waitForActiveShards { @@ -1118,7 +1107,7 @@ public void testGetSettings() throws Exception { { Settings settings = Settings.builder().put("number_of_shards", 3).build(); CreateIndexResponse createIndexResponse = client.indices().create( - new CreateIndexRequest("index", settings), RequestOptions.DEFAULT); + new CreateIndexRequest("index").settings(settings), RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); } @@ -1182,7 +1171,7 @@ public void testGetSettingsWithDefaults() throws Exception { { Settings settings = Settings.builder().put("number_of_shards", 3).build(); CreateIndexResponse createIndexResponse = client.indices().create( - new CreateIndexRequest("index", settings), RequestOptions.DEFAULT); + new CreateIndexRequest("index").settings(settings), RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); } @@ -1233,9 +1222,11 @@ public void testGetIndex() throws Exception { { Settings settings = Settings.builder().put("number_of_shards", 3).build(); String mappings = "{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}"; + CreateIndexRequest createIndexRequest = new CreateIndexRequest("index") + .settings(settings) + .mapping(mappings, XContentType.JSON); CreateIndexResponse createIndexResponse = client.indices().create( - new CreateIndexRequest("index", settings).mapping("_doc", mappings, XContentType.JSON), - RequestOptions.DEFAULT); + createIndexRequest, RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); } @@ -1912,7 +1903,9 @@ public void testGetAlias() throws Exception { RestHighLevelClient client = highLevelClient(); { - CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index").alias(new Alias("alias"))); + CreateIndexResponse createIndexResponse = client.indices().create( + new CreateIndexRequest("index").alias(new Alias("alias")), + RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java index 0ef6c8d682806..0048023a53d9e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java @@ -20,7 +20,6 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.LatchedActionListener; -import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; @@ -33,6 +32,7 @@ import org.elasticsearch.client.MlTestStateCleaner; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.ml.CloseJobRequest; import org.elasticsearch.client.ml.CloseJobResponse; import org.elasticsearch.client.ml.DeleteCalendarEventRequest; @@ -139,6 +139,7 @@ import org.elasticsearch.client.ml.job.util.PageParams; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregatorFactories; @@ -869,7 +870,16 @@ public void testPreviewDatafeed() throws Exception { String datafeedId = job.getId() + "-feed"; String indexName = "preview_data_2"; CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName); - createIndexRequest.mapping("doc", "timestamp", "type=date", "total", "type=long"); + createIndexRequest.mapping(XContentFactory.jsonBuilder().startObject() + .startObject("properties") + .startObject("timestamp") + .field("type", "date") + .endObject() + .startObject("total") + .field("type", "long") + .endObject() + .endObject() + .endObject()); highLevelClient().indices().create(createIndexRequest, RequestOptions.DEFAULT); DatafeedConfig datafeed = DatafeedConfig.builder(datafeedId, job.getId()) .setTypes(Arrays.asList("doc")) @@ -929,7 +939,16 @@ public void testStartDatafeed() throws Exception { String datafeedId = job.getId() + "-feed"; String indexName = "start_data_2"; CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName); - createIndexRequest.mapping("doc", "timestamp", "type=date", "total", "type=long"); + createIndexRequest.mapping(XContentFactory.jsonBuilder().startObject() + .startObject("properties") + .startObject("timestamp") + .field("type", "date") + .endObject() + .startObject("total") + .field("type", "long") + .endObject() + .endObject() + .endObject()); highLevelClient().indices().create(createIndexRequest, RequestOptions.DEFAULT); DatafeedConfig datafeed = DatafeedConfig.builder(datafeedId, job.getId()) .setTypes(Arrays.asList("doc")) @@ -1050,7 +1069,16 @@ public void testGetDatafeedStats() throws Exception { String datafeedId1 = job.getId() + "-feed"; String indexName = "datafeed_stats_data_2"; CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName); - createIndexRequest.mapping("doc", "timestamp", "type=date", "total", "type=long"); + createIndexRequest.mapping(XContentFactory.jsonBuilder().startObject() + .startObject("properties") + .startObject("timestamp") + .field("type", "date") + .endObject() + .startObject("total") + .field("type", "long") + .endObject() + .endObject() + .endObject()); highLevelClient().indices().create(createIndexRequest, RequestOptions.DEFAULT); DatafeedConfig datafeed = DatafeedConfig.builder(datafeedId1, job.getId()) .setTypes(Arrays.asList("doc")) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java index b6fbf759848ac..7fa5153e90e56 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java @@ -22,8 +22,6 @@ import org.apache.lucene.search.Explanation; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.LatchedActionListener; -import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; -import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.explain.ExplainRequest; @@ -51,11 +49,14 @@ import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.core.CountRequest; import org.elasticsearch.client.core.CountResponse; +import org.elasticsearch.client.indices.CreateIndexRequest; +import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.document.DocumentField; import org.elasticsearch.common.text.Text; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.query.MatchQueryBuilder; @@ -1255,12 +1256,26 @@ public void onFailure(Exception e) { private void indexSearchTestData() throws IOException { CreateIndexRequest authorsRequest = new CreateIndexRequest("authors") - .mapping("doc", "user", "type=keyword,doc_values=false"); + .mapping(XContentFactory.jsonBuilder().startObject() + .startObject("properties") + .startObject("user") + .field("type", "keyword") + .field("doc_values", "false") + .endObject() + .endObject() + .endObject()); CreateIndexResponse authorsResponse = highLevelClient().indices().create(authorsRequest, RequestOptions.DEFAULT); assertTrue(authorsResponse.isAcknowledged()); CreateIndexRequest reviewersRequest = new CreateIndexRequest("contributors") - .mapping("doc", "user", "type=keyword,store=true"); + .mapping(XContentFactory.jsonBuilder().startObject() + .startObject("properties") + .startObject("user") + .field("type", "keyword") + .field("store", "true") + .endObject() + .endObject() + .endObject()); CreateIndexResponse reviewersResponse = highLevelClient().indices().create(reviewersRequest, RequestOptions.DEFAULT); assertTrue(reviewersResponse.isAcknowledged()); @@ -1381,7 +1396,14 @@ public void onFailure(Exception e) { private static void indexCountTestData() throws IOException { CreateIndexRequest authorsRequest = new CreateIndexRequest("author") - .mapping("doc", "user", "type=keyword,doc_values=false"); + .mapping(XContentFactory.jsonBuilder().startObject() + .startObject("properties") + .startObject("user") + .field("type", "keyword") + .field("doc_values", "false") + .endObject() + .endObject() + .endObject()); CreateIndexResponse authorsResponse = highLevelClient().indices().create(authorsRequest, RequestOptions.DEFAULT); assertTrue(authorsResponse.isAcknowledged()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java index d1aed55f44e5b..301748fac90d1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java @@ -38,7 +38,6 @@ import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotStatus; import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotsStatusRequest; import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse; -import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.ESRestHighLevelClientTestCase; @@ -46,6 +45,7 @@ import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.Response; import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.cluster.SnapshotsInProgress; import org.elasticsearch.cluster.metadata.RepositoryMetaData; import org.elasticsearch.common.settings.Settings; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CreateIndexRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CreateIndexRequestTests.java new file mode 100644 index 0000000000000..374f024401155 --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CreateIndexRequestTests.java @@ -0,0 +1,93 @@ +/* + * 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 org.elasticsearch.action.admin.indices.alias.Alias; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractXContentTestCase; + +import java.io.IOException; +import java.util.Set; +import java.util.function.Predicate; + +import static org.elasticsearch.client.indices.CreateIndexRequest.ALIASES; +import static org.elasticsearch.client.indices.CreateIndexRequest.MAPPINGS; +import static org.elasticsearch.client.indices.CreateIndexRequest.SETTINGS; + +public class CreateIndexRequestTests extends AbstractXContentTestCase { + + @Override + protected CreateIndexRequest createTestInstance() { + return RandomCreateIndexGenerator.randomCreateIndexRequest(); + } + + @Override + protected CreateIndexRequest doParseInstance(XContentParser parser) throws IOException { + return new CreateIndexRequest("index").source(parser.map()); + } + + @Override + protected void assertEqualInstances(CreateIndexRequest expected, CreateIndexRequest actual) { + assertEquals(expected.settings(), actual.settings()); + assertAliasesEqual(expected.aliases(), actual.aliases()); + assertMappingsEqual(expected, actual); + } + + private void assertMappingsEqual(CreateIndexRequest expected, CreateIndexRequest actual) { + if (expected.mappings() == null) { + assertNull(actual.mappings()); + } else { + assertNotNull(actual.mappings()); + try (XContentParser expectedJson = createParser(expected.mappingsXContentType().xContent(), expected.mappings()); + XContentParser actualJson = createParser(actual.mappingsXContentType().xContent(), actual.mappings())) { + assertEquals(expectedJson.map(), actualJson.map()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + private void assertAliasesEqual(Set expected, Set actual) { + assertEquals(expected, actual); + + for (Alias expectedAlias : expected) { + for (Alias actualAlias : actual) { + if (expectedAlias.equals(actualAlias)) { + // As Alias#equals only looks at name, we check the equality of the other Alias parameters here. + assertEquals(expectedAlias.filter(), actualAlias.filter()); + assertEquals(expectedAlias.indexRouting(), actualAlias.indexRouting()); + assertEquals(expectedAlias.searchRouting(), actualAlias.searchRouting()); + } + } + } + } + + @Override + protected Predicate getRandomFieldsExcludeFilter() { + return field -> field.startsWith(MAPPINGS.getPreferredName()) + || field.startsWith(SETTINGS.getPreferredName()) + || field.startsWith(ALIASES.getPreferredName()); + } + + @Override + protected boolean supportsUnknownFields() { + return true; + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutMappingRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutMappingRequestTests.java index 50224aa1b9ad7..e4fd0708b540c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutMappingRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutMappingRequestTests.java @@ -21,7 +21,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.RandomCreateIndexGenerator; import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/RandomCreateIndexGenerator.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/RandomCreateIndexGenerator.java new file mode 100644 index 0000000000000..179b7e728b620 --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/RandomCreateIndexGenerator.java @@ -0,0 +1,61 @@ +/* + * 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 org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; + +import java.io.IOException; + +public class RandomCreateIndexGenerator { + + /** + * Returns a random {@link CreateIndexRequest}. + * + * Randomizes the index name, the aliases, mappings and settings associated with the + * index. When present, the mappings make no mention of types. + */ + public static CreateIndexRequest randomCreateIndexRequest() { + try { + // Create a random server request, and copy its contents into the HLRC request. + // Because client requests only accept typeless mappings, we must swap out the + // mapping definition for one that does not contain types. + org.elasticsearch.action.admin.indices.create.CreateIndexRequest serverRequest = + org.elasticsearch.index.RandomCreateIndexGenerator.randomCreateIndexRequest(); + return new CreateIndexRequest(serverRequest.index()) + .settings(serverRequest.settings()) + .aliases(serverRequest.aliases()) + .mapping(randomMapping()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** + * Creates a random mapping, with no mention of types. + */ + public static XContentBuilder randomMapping() throws IOException { + XContentBuilder builder = XContentFactory.jsonBuilder(); + builder.startObject(); + org.elasticsearch.index.RandomCreateIndexGenerator.randomMappingFields(builder, true); + builder.endObject(); + return builder; + } +} diff --git a/docs/java-rest/high-level/indices/create_index.asciidoc b/docs/java-rest/high-level/indices/create_index.asciidoc index 997b860b2786f..e6352d481ef86 100644 --- a/docs/java-rest/high-level/indices/create_index.asciidoc +++ b/docs/java-rest/high-level/indices/create_index.asciidoc @@ -55,13 +55,6 @@ include-tagged::{doc-tests-file}[{api}-mappings-xcontent] <1> Mapping source provided as an `XContentBuilder` object, the Elasticsearch built-in helpers to generate JSON content -["source","java",subs="attributes,callouts,macros"] --------------------------------------------------- -include-tagged::{doc-tests-file}[{api}-mappings-shortcut] --------------------------------------------------- -<1> Mapping source provided as `Object` key-pairs, which gets converted to -JSON format - ==== Index aliases Aliases can be set at index creation time diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java index eb36b71ec2bd3..c054fcc4c18d5 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java @@ -39,8 +39,10 @@ import java.util.Map; public class RestCreateIndexAction extends BaseRestHandler { - - private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger(LogManager.getLogger(RestCreateIndexAction.class)); + private static final DeprecationLogger deprecationLogger = new DeprecationLogger( + LogManager.getLogger(RestPutMappingAction.class)); + public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Using include_type_name in create " + + "index requests is deprecated. The parameter will be removed in the next major version."; public RestCreateIndexAction(Settings settings, RestController controller) { super(settings); @@ -54,7 +56,13 @@ public String getName() { @Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { - final boolean includeTypeName = request.paramAsBoolean(INCLUDE_TYPE_NAME_PARAMETER, DEFAULT_INCLUDE_TYPE_NAME_POLICY); + final boolean includeTypeName = request.paramAsBoolean(INCLUDE_TYPE_NAME_PARAMETER, + DEFAULT_INCLUDE_TYPE_NAME_POLICY); + + if (request.hasParam(INCLUDE_TYPE_NAME_PARAMETER)) { + deprecationLogger.deprecatedAndMaybeLog("create_index_with_types", TYPES_DEPRECATION_MESSAGE); + } + CreateIndexRequest createIndexRequest = new CreateIndexRequest(request.param("index")); if (request.hasContent()) { Map sourceAsMap = XContentHelper.convertToMap(request.content(), false, request.getXContentType()).v2(); @@ -66,7 +74,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC createIndexRequest.source(sourceAsMap, LoggingDeprecationHandler.INSTANCE); } if (request.hasParam("update_all_types")) { - DEPRECATION_LOGGER.deprecated("[update_all_types] is deprecated since indices may not have more than one type anymore"); + deprecationLogger.deprecated("[update_all_types] is deprecated since indices may not have more than one type anymore"); } createIndexRequest.updateAllTypes(request.paramAsBoolean("update_all_types", false)); createIndexRequest.timeout(request.paramAsTime("timeout", createIndexRequest.timeout())); diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingRequestTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingRequestTests.java index 1e8b8f5535b01..b41d17daad34f 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingRequestTests.java @@ -160,7 +160,7 @@ private static PutMappingRequest createTestItem() throws IOException { String type = randomAlphaOfLength(5); request.type(type); - request.source(RandomCreateIndexGenerator.randomMapping()); + request.source(RandomCreateIndexGenerator.randomMapping(type)); return request; } diff --git a/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionTests.java b/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionTests.java new file mode 100644 index 0000000000000..1ec0a0f949965 --- /dev/null +++ b/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionTests.java @@ -0,0 +1,62 @@ +/* + * 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.rest.action.admin.indices; + +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.test.rest.FakeRestRequest; +import org.elasticsearch.test.rest.RestActionTestCase; +import org.junit.Before; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import static org.elasticsearch.rest.BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER; +import static org.mockito.Mockito.mock; + +public class RestCreateIndexActionTests extends RestActionTestCase { + private RestCreateIndexAction action; + + @Before + public void setupAction() { + action = new RestCreateIndexAction(Settings.EMPTY, controller()); + } + + public void testIncludeTypeName() throws IOException { + Map params = new HashMap<>(); + params.put(INCLUDE_TYPE_NAME_PARAMETER, randomFrom("true", "false")); + RestRequest deprecatedRequest = new FakeRestRequest.Builder(xContentRegistry()) + .withMethod(RestRequest.Method.PUT) + .withPath("/some_index") + .withParams(params) + .build(); + + action.prepareRequest(deprecatedRequest, mock(NodeClient.class)); + assertWarnings(RestCreateIndexAction.TYPES_DEPRECATION_MESSAGE); + + RestRequest validRequest = new FakeRestRequest.Builder(xContentRegistry()) + .withMethod(RestRequest.Method.PUT) + .withPath("/some_index") + .build(); + action.prepareRequest(validRequest, mock(NodeClient.class)); + } +} diff --git a/test/framework/src/main/java/org/elasticsearch/index/RandomCreateIndexGenerator.java b/test/framework/src/main/java/org/elasticsearch/index/RandomCreateIndexGenerator.java index e4836150c6e86..345ef1f58bcac 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/RandomCreateIndexGenerator.java +++ b/test/framework/src/main/java/org/elasticsearch/index/RandomCreateIndexGenerator.java @@ -40,8 +40,10 @@ public final class RandomCreateIndexGenerator { private RandomCreateIndexGenerator() {} /** - * Returns a random {@link CreateIndexRequest}. Randomizes the index name, the aliases, - * mappings and settings associated with the index. + * Returns a random {@link CreateIndexRequest}. + * + * Randomizes the index name, the aliases, mappings and settings associated with the + * index. If present, the mapping definition will be nested under a type name. */ public static CreateIndexRequest randomCreateIndexRequest() throws IOException { String index = randomAlphaOfLength(5); @@ -78,20 +80,6 @@ public static Settings randomIndexSettings() { return builder.build(); } - - /** - * Creates a random mapping, with no mention of types. - */ - public static XContentBuilder randomMapping() throws IOException { - XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values())); - builder.startObject(); - - randomMappingFields(builder, true); - - builder.endObject(); - return builder; - } - /** * Creates a random mapping, with the mapping definition nested * under the given type name. diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java index 9507c5e12f8c2..136f51bed3c8c 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java @@ -19,8 +19,8 @@ package org.elasticsearch.test; -import org.elasticsearch.common.CheckedBiFunction; import org.elasticsearch.common.CheckedBiConsumer; +import org.elasticsearch.common.CheckedBiFunction; import org.elasticsearch.common.CheckedFunction; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; From 437d08dcf713bfb6bebba964ec1f33ab48e0227e Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Thu, 24 Jan 2019 14:08:58 -0800 Subject: [PATCH 2/7] Remove outdated callouts from the 'create index' HLRC docs --- docs/java-rest/high-level/indices/create_index.asciidoc | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/java-rest/high-level/indices/create_index.asciidoc b/docs/java-rest/high-level/indices/create_index.asciidoc index e6352d481ef86..004279ba2a892 100644 --- a/docs/java-rest/high-level/indices/create_index.asciidoc +++ b/docs/java-rest/high-level/indices/create_index.asciidoc @@ -84,14 +84,12 @@ The following arguments can optionally be provided: include-tagged::{doc-tests-file}[{api}-request-timeout] -------------------------------------------------- <1> Timeout to wait for the all the nodes to acknowledge the index creation as a `TimeValue` -<2> Timeout to wait for the all the nodes to acknowledge the index creation as a `String` ["source","java",subs="attributes,callouts,macros"] -------------------------------------------------- include-tagged::{doc-tests-file}[{api}-request-masterTimeout] -------------------------------------------------- <1> Timeout to connect to the master node as a `TimeValue` -<2> Timeout to connect to the master node as a `String` ["source","java",subs="attributes,callouts,macros"] -------------------------------------------------- From 7438dfe7f6a54cd363873aaa8ff994373ddc1aef Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Thu, 24 Jan 2019 14:51:18 -0800 Subject: [PATCH 3/7] Updates to account for different default for include_type_name --- .../client/IndicesRequestConverters.java | 3 +++ .../elasticsearch/client/IndicesClientIT.java | 10 +++++---- .../client/IndicesRequestConvertersTests.java | 3 ++- .../admin/indices/RestCreateIndexAction.java | 22 ++++++++++--------- .../indices/RestCreateIndexActionTests.java | 18 ++++++++++++--- 5 files changed, 38 insertions(+), 18 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java index a6be15d2f0b0d..63eb60557decc 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java @@ -107,6 +107,7 @@ static Request createIndex(CreateIndexRequest createIndexRequest) throws IOExcep parameters.withTimeout(createIndexRequest.timeout()); parameters.withMasterTimeout(createIndexRequest.masterNodeTimeout()); parameters.withWaitForActiveShards(createIndexRequest.waitForActiveShards(), ActiveShardCount.DEFAULT); + parameters.putParam(INCLUDE_TYPE_NAME_PARAMETER, "false"); request.setEntity(RequestConverters.createEntity(createIndexRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); return request; @@ -121,6 +122,7 @@ static Request createIndex(org.elasticsearch.action.admin.indices.create.CreateI parameters.withTimeout(createIndexRequest.timeout()); parameters.withMasterTimeout(createIndexRequest.masterNodeTimeout()); parameters.withWaitForActiveShards(createIndexRequest.waitForActiveShards(), ActiveShardCount.DEFAULT); + parameters.putParam(INCLUDE_TYPE_NAME_PARAMETER, "true"); request.setEntity(RequestConverters.createEntity(createIndexRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); return request; @@ -161,6 +163,7 @@ static Request putMapping(org.elasticsearch.action.admin.indices.mapping.put.Put RequestConverters.Params parameters = new RequestConverters.Params(request); parameters.withTimeout(putMappingRequest.timeout()); parameters.withMasterTimeout(putMappingRequest.masterNodeTimeout()); + parameters.putParam(INCLUDE_TYPE_NAME_PARAMETER, "true"); request.setEntity(RequestConverters.createEntity(putMappingRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); return request; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index 4a1ec8e2c3633..d0b96e7627103 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -255,8 +255,7 @@ public void testCreateIndexWithTypes() throws IOException { org.elasticsearch.action.admin.indices.create.CreateIndexResponse createIndexResponse = execute( createIndexRequest, highLevelClient().indices()::create, - highLevelClient().indices()::createAsync, - expectWarnings(RestCreateIndexAction.TYPES_DEPRECATION_MESSAGE)); + highLevelClient().indices()::createAsync); assertTrue(createIndexResponse.isAcknowledged()); assertTrue(indexExists(indexName)); @@ -284,11 +283,14 @@ public void testCreateIndexWithTypes() throws IOException { mappingBuilder.endObject().endObject().endObject(); createIndexRequest.mapping(MapperService.SINGLE_MAPPING_NAME, mappingBuilder); + String shardsWarning = "the default number of shards will change from [5] to [1] in 7.0.0; " + + "if you wish to continue using the default of [5] shards, " + + "you must manage this on the create index request or with an index template"; org.elasticsearch.action.admin.indices.create.CreateIndexResponse createIndexResponse = execute( createIndexRequest, highLevelClient().indices()::create, highLevelClient().indices()::createAsync, - expectWarnings(RestCreateIndexAction.TYPES_DEPRECATION_MESSAGE)); + expectWarnings(shardsWarning, RestCreateIndexAction.TYPES_DEPRECATION_MESSAGE)); assertTrue(createIndexResponse.isAcknowledged()); Map getIndexResponse = getAsMap(indexName); @@ -302,7 +304,7 @@ public void testCreateIndexWithTypes() throws IOException { Map term = (Map) filter.get("term"); assertEquals(2016, term.get("year")); - assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", getIndexResponse)); + assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings._doc.properties.field.type", getIndexResponse)); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java index f2e8dd2d6d224..95598d4610e05 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java @@ -133,6 +133,7 @@ public void testCreateIndex() throws IOException { RequestConvertersTests.setRandomTimeout(createIndexRequest, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); RequestConvertersTests.setRandomMasterTimeout(createIndexRequest, expectedParams); RequestConvertersTests.setRandomWaitForActiveShards(createIndexRequest::waitForActiveShards, expectedParams); + expectedParams.put(INCLUDE_TYPE_NAME_PARAMETER, "false"); Request request = IndicesRequestConverters.createIndex(createIndexRequest); Assert.assertEquals("/" + createIndexRequest.index(), request.getEndpoint()); @@ -212,9 +213,9 @@ public void testPutMappingWithTypes() throws IOException { putMappingRequest.type(type); Map expectedParams = new HashMap<>(); - RequestConvertersTests.setRandomTimeout(putMappingRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); RequestConvertersTests.setRandomMasterTimeout(putMappingRequest, expectedParams); + expectedParams.put(INCLUDE_TYPE_NAME_PARAMETER, "true"); Request request = IndicesRequestConverters.putMapping(putMappingRequest); StringJoiner endpoint = new StringJoiner("/", "/", ""); diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java index c054fcc4c18d5..b8ba15eeb0812 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java @@ -41,8 +41,9 @@ public class RestCreateIndexAction extends BaseRestHandler { private static final DeprecationLogger deprecationLogger = new DeprecationLogger( LogManager.getLogger(RestPutMappingAction.class)); - public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Using include_type_name in create " + - "index requests is deprecated. The parameter will be removed in the next major version."; + public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Specifying types in create index " + + "requests is deprecated. The parameter include_type_name should be provided and set to false to be " + + "compatible with the next major version."; public RestCreateIndexAction(Settings settings, RestController controller) { super(settings); @@ -59,20 +60,21 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC final boolean includeTypeName = request.paramAsBoolean(INCLUDE_TYPE_NAME_PARAMETER, DEFAULT_INCLUDE_TYPE_NAME_POLICY); - if (request.hasParam(INCLUDE_TYPE_NAME_PARAMETER)) { - deprecationLogger.deprecatedAndMaybeLog("create_index_with_types", TYPES_DEPRECATION_MESSAGE); - } - CreateIndexRequest createIndexRequest = new CreateIndexRequest(request.param("index")); if (request.hasContent()) { Map sourceAsMap = XContentHelper.convertToMap(request.content(), false, request.getXContentType()).v2(); - if (includeTypeName == false && sourceAsMap.containsKey("mappings")) { - Map newSourceAsMap = new HashMap<>(sourceAsMap); - newSourceAsMap.put("mappings", Collections.singletonMap(MapperService.SINGLE_MAPPING_NAME, sourceAsMap.get("mappings"))); - sourceAsMap = newSourceAsMap; + if (sourceAsMap.containsKey("mappings")) { + if (includeTypeName == false) { + Map newSourceAsMap = new HashMap<>(sourceAsMap); + newSourceAsMap.put("mappings", Collections.singletonMap(MapperService.SINGLE_MAPPING_NAME, sourceAsMap.get("mappings"))); + sourceAsMap = newSourceAsMap; + } else { + deprecationLogger.deprecatedAndMaybeLog("create_index_with_types", TYPES_DEPRECATION_MESSAGE); + } } createIndexRequest.source(sourceAsMap, LoggingDeprecationHandler.INSTANCE); } + if (request.hasParam("update_all_types")) { deprecationLogger.deprecated("[update_all_types] is deprecated since indices may not have more than one type anymore"); } diff --git a/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionTests.java b/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionTests.java index 1ec0a0f949965..28c8ee7b99c8a 100644 --- a/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionTests.java +++ b/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionTests.java @@ -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.XContentType; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.test.rest.RestActionTestCase; @@ -42,21 +46,29 @@ public void setupAction() { } public void testIncludeTypeName() throws IOException { - Map params = new HashMap<>(); - params.put(INCLUDE_TYPE_NAME_PARAMETER, randomFrom("true", "false")); + XContentBuilder content = XContentFactory.jsonBuilder().startObject() + .startObject("mappings") + .startObject("some_type").endObject() + .endObject() + .endObject(); + RestRequest deprecatedRequest = new FakeRestRequest.Builder(xContentRegistry()) .withMethod(RestRequest.Method.PUT) .withPath("/some_index") - .withParams(params) + .withContent(BytesReference.bytes(content), XContentType.JSON) .build(); action.prepareRequest(deprecatedRequest, mock(NodeClient.class)); assertWarnings(RestCreateIndexAction.TYPES_DEPRECATION_MESSAGE); + Map params = new HashMap<>(); + params.put(INCLUDE_TYPE_NAME_PARAMETER, "false"); RestRequest validRequest = new FakeRestRequest.Builder(xContentRegistry()) .withMethod(RestRequest.Method.PUT) .withPath("/some_index") + .withParams(params) .build(); + action.prepareRequest(validRequest, mock(NodeClient.class)); } } From ae71a64181d090f05da90df074d96597f9726814 Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Thu, 24 Jan 2019 15:59:02 -0800 Subject: [PATCH 4/7] Fix static analysis violations. --- .../rest/action/admin/indices/RestCreateIndexAction.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java index b8ba15eeb0812..24046dbbed6c6 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java @@ -66,7 +66,8 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC if (sourceAsMap.containsKey("mappings")) { if (includeTypeName == false) { Map newSourceAsMap = new HashMap<>(sourceAsMap); - newSourceAsMap.put("mappings", Collections.singletonMap(MapperService.SINGLE_MAPPING_NAME, sourceAsMap.get("mappings"))); + newSourceAsMap.put("mappings", Collections.singletonMap( + MapperService.SINGLE_MAPPING_NAME, sourceAsMap.get("mappings"))); sourceAsMap = newSourceAsMap; } else { deprecationLogger.deprecatedAndMaybeLog("create_index_with_types", TYPES_DEPRECATION_MESSAGE); From d00c6ffda50a413c8eab7124878f25849336bab6 Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Fri, 25 Jan 2019 22:53:27 -0800 Subject: [PATCH 5/7] Fix IndicesRequestConvertersTests. --- .../org/elasticsearch/client/IndicesRequestConvertersTests.java | 1 + 1 file changed, 1 insertion(+) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java index 18138a7918f88..cf32a52afc4f6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java @@ -150,6 +150,7 @@ public void testCreateIndexWithTypes() throws IOException { RequestConvertersTests.setRandomTimeout(createIndexRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); RequestConvertersTests.setRandomMasterTimeout(createIndexRequest, expectedParams); RequestConvertersTests.setRandomWaitForActiveShards(createIndexRequest::waitForActiveShards, expectedParams); + expectedParams.put(INCLUDE_TYPE_NAME_PARAMETER, "true"); Request request = IndicesRequestConverters.createIndex(createIndexRequest); Assert.assertEquals("/" + createIndexRequest.index(), request.getEndpoint()); From 28542ad402c24e5903e5f684e71f338811b14e54 Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Fri, 25 Jan 2019 22:55:24 -0800 Subject: [PATCH 6/7] Make sure the deprecated 'get field mappings' call specifies include_type_name. --- .../java/org/elasticsearch/client/IndicesRequestConverters.java | 2 ++ .../org/elasticsearch/client/IndicesRequestConvertersTests.java | 1 + 2 files changed, 3 insertions(+) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java index c598515d5e46c..47ab480d37973 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java @@ -220,6 +220,8 @@ static Request getFieldMapping(org.elasticsearch.action.admin.indices.mapping.ge parameters.withIndicesOptions(getFieldMappingsRequest.indicesOptions()); parameters.withIncludeDefaults(getFieldMappingsRequest.includeDefaults()); parameters.withLocal(getFieldMappingsRequest.local()); + parameters.putParam(INCLUDE_TYPE_NAME_PARAMETER, "true"); + return request; } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java index cf32a52afc4f6..e3f92a6bddf2f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java @@ -354,6 +354,7 @@ public void testGetFieldMappingWithTypes() { RequestConvertersTests.setRandomIndicesOptions(getFieldMappingsRequest::indicesOptions, getFieldMappingsRequest::indicesOptions, expectedParams); RequestConvertersTests.setRandomLocal(getFieldMappingsRequest::local, expectedParams); + expectedParams.put(INCLUDE_TYPE_NAME_PARAMETER, "true"); Request request = IndicesRequestConverters.getFieldMapping(getFieldMappingsRequest); StringJoiner endpoint = new StringJoiner("/", "/", ""); From eae5856af6a63e90aab79f3e61bd7698a7f92c80 Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Sat, 26 Jan 2019 12:20:12 -0800 Subject: [PATCH 7/7] Fix some HLRC tests. --- .../client/MachineLearningIT.java | 12 ++--- .../documentation/SearchDocumentationIT.java | 54 +++++++++---------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java index b2bc96adb8eba..fbea74d1cade9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java @@ -548,7 +548,7 @@ public void testStartDatafeed() throws Exception { while(pastCopy < now) { IndexRequest doc = new IndexRequest(); doc.index(indexName); - doc.type("doc"); + doc.type("_doc"); doc.id("id" + i); doc.source("{\"total\":" +randomInt(1000) + ",\"timestamp\":"+ pastCopy +"}", XContentType.JSON); bulk.add(doc); @@ -568,7 +568,7 @@ public void testStartDatafeed() throws Exception { DatafeedConfig datafeed = DatafeedConfig.builder(datafeedId, jobId) .setIndices(indexName) .setQueryDelay(TimeValue.timeValueSeconds(1)) - .setTypes(Arrays.asList("doc")) + .setTypes(Arrays.asList("_doc")) .setFrequency(TimeValue.timeValueSeconds(1)).build(); machineLearningClient.putDatafeed(new PutDatafeedRequest(datafeed), RequestOptions.DEFAULT); @@ -785,7 +785,7 @@ public void testPreviewDatafeed() throws Exception { Integer total = randomInt(1000); IndexRequest doc = new IndexRequest(); doc.index(indexName); - doc.type("doc"); + doc.type("_doc"); doc.id("id" + i); doc.source("{\"total\":" + total + ",\"timestamp\":"+ thePast +"}", XContentType.JSON); bulk.add(doc); @@ -805,7 +805,7 @@ public void testPreviewDatafeed() throws Exception { DatafeedConfig datafeed = DatafeedConfig.builder(datafeedId, jobId) .setIndices(indexName) .setQueryDelay(TimeValue.timeValueSeconds(1)) - .setTypes(Collections.singletonList("doc")) + .setTypes(Collections.singletonList("_doc")) .setFrequency(TimeValue.timeValueSeconds(1)).build(); machineLearningClient.putDatafeed(new PutDatafeedRequest(datafeed), RequestOptions.DEFAULT); @@ -856,7 +856,7 @@ private String createExpiredData(String jobId) throws Exception { long timestamp = nowMillis - TimeValue.timeValueHours(totalBuckets - bucket).getMillis(); int bucketRate = bucket == anomalousBucket ? anomalousRate : normalRate; for (int point = 0; point < bucketRate; point++) { - IndexRequest indexRequest = new IndexRequest(indexId, "doc"); + IndexRequest indexRequest = new IndexRequest(indexId, "_doc"); indexRequest.source(XContentType.JSON, "timestamp", timestamp, "total", randomInt(1000)); bulk.add(indexRequest); } @@ -1450,7 +1450,7 @@ private String createAndPutDatafeed(String jobId, String indexName) throws IOExc DatafeedConfig datafeed = DatafeedConfig.builder(datafeedId, jobId) .setIndices(indexName) .setQueryDelay(TimeValue.timeValueSeconds(1)) - .setTypes(Arrays.asList("doc")) + .setTypes(Arrays.asList("_doc")) .setFrequency(TimeValue.timeValueSeconds(1)).build(); highLevelClient().machineLearning().putDatafeed(new PutDatafeedRequest(datafeed), RequestOptions.DEFAULT); return datafeedId; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java index 7fa5153e90e56..43216b90564d1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java @@ -301,11 +301,11 @@ public void testSearchRequestAggregations() throws IOException { RestHighLevelClient client = highLevelClient(); { BulkRequest request = new BulkRequest(); - request.add(new IndexRequest("posts", "doc", "1") + request.add(new IndexRequest("posts", "_doc", "1") .source(XContentType.JSON, "company", "Elastic", "age", 20)); - request.add(new IndexRequest("posts", "doc", "2") + request.add(new IndexRequest("posts", "_doc", "2") .source(XContentType.JSON, "company", "Elastic", "age", 30)); - request.add(new IndexRequest("posts", "doc", "3") + request.add(new IndexRequest("posts", "_doc", "3") .source(XContentType.JSON, "company", "Elastic", "age", 40)); request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT); @@ -377,10 +377,10 @@ public void testSearchRequestSuggestions() throws IOException { RestHighLevelClient client = highLevelClient(); { BulkRequest request = new BulkRequest(); - request.add(new IndexRequest("posts", "doc", "1").source(XContentType.JSON, "user", "kimchy")); - request.add(new IndexRequest("posts", "doc", "2").source(XContentType.JSON, "user", "javanna")); - request.add(new IndexRequest("posts", "doc", "3").source(XContentType.JSON, "user", "tlrx")); - request.add(new IndexRequest("posts", "doc", "4").source(XContentType.JSON, "user", "cbuescher")); + request.add(new IndexRequest("posts", "_doc", "1").source(XContentType.JSON, "user", "kimchy")); + request.add(new IndexRequest("posts", "_doc", "2").source(XContentType.JSON, "user", "javanna")); + request.add(new IndexRequest("posts", "_doc", "3").source(XContentType.JSON, "user", "tlrx")); + request.add(new IndexRequest("posts", "_doc", "4").source(XContentType.JSON, "user", "cbuescher")); request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT); assertSame(RestStatus.OK, bulkResponse.status()); @@ -419,13 +419,13 @@ public void testSearchRequestHighlighting() throws IOException { RestHighLevelClient client = highLevelClient(); { BulkRequest request = new BulkRequest(); - request.add(new IndexRequest("posts", "doc", "1") + request.add(new IndexRequest("posts", "_doc", "1") .source(XContentType.JSON, "title", "In which order are my Elasticsearch queries executed?", "user", Arrays.asList("kimchy", "luca"), "innerObject", Collections.singletonMap("key", "value"))); - request.add(new IndexRequest("posts", "doc", "2") + request.add(new IndexRequest("posts", "_doc", "2") .source(XContentType.JSON, "title", "Current status and upcoming changes in Elasticsearch", "user", Arrays.asList("kimchy", "christoph"), "innerObject", Collections.singletonMap("key", "value"))); - request.add(new IndexRequest("posts", "doc", "3") + request.add(new IndexRequest("posts", "_doc", "3") .source(XContentType.JSON, "title", "The Future of Federated Search in Elasticsearch", "user", Arrays.asList("kimchy", "tanguy"), "innerObject", Collections.singletonMap("key", "value"))); request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); @@ -482,7 +482,7 @@ public void testSearchRequestHighlighting() throws IOException { public void testSearchRequestProfiling() throws IOException { RestHighLevelClient client = highLevelClient(); { - IndexRequest request = new IndexRequest("posts", "doc", "1") + IndexRequest request = new IndexRequest("posts", "_doc", "1") .source(XContentType.JSON, "tags", "elasticsearch", "comments", 123); request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT); @@ -554,11 +554,11 @@ public void testScroll() throws Exception { RestHighLevelClient client = highLevelClient(); { BulkRequest request = new BulkRequest(); - request.add(new IndexRequest("posts", "doc", "1") + request.add(new IndexRequest("posts", "_doc", "1") .source(XContentType.JSON, "title", "In which order are my Elasticsearch queries executed?")); - request.add(new IndexRequest("posts", "doc", "2") + request.add(new IndexRequest("posts", "_doc", "2") .source(XContentType.JSON, "title", "Current status and upcoming changes in Elasticsearch")); - request.add(new IndexRequest("posts", "doc", "3") + request.add(new IndexRequest("posts", "_doc", "3") .source(XContentType.JSON, "title", "The Future of Federated Search in Elasticsearch")); request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT); @@ -976,7 +976,7 @@ public void testExplain() throws Exception { RestHighLevelClient client = highLevelClient(); // tag::explain-request - ExplainRequest request = new ExplainRequest("contributors", "doc", "1"); + ExplainRequest request = new ExplainRequest("contributors", "_doc", "1"); request.query(QueryBuilders.termQuery("user", "tanguy")); // end::explain-request @@ -1011,7 +1011,7 @@ public void testExplain() throws Exception { GetResult getResult = response.getGetResult(); // <8> // end::explain-response assertThat(index, equalTo("contributors")); - assertThat(type, equalTo("doc")); + assertThat(type, equalTo("_doc")); assertThat(id, equalTo("1")); assertTrue(exists); assertTrue(match); @@ -1244,7 +1244,7 @@ public void onFailure(Exception e) { // tag::multi-search-request-index MultiSearchRequest request = new MultiSearchRequest(); request.add(new SearchRequest("posts") // <1> - .types("doc")); // <2> + .types("_doc")); // <2> // end::multi-search-request-index MultiSearchResponse response = client.msearch(request, RequestOptions.DEFAULT); MultiSearchResponse.Item firstResponse = response.getResponses()[0]; @@ -1280,19 +1280,19 @@ private void indexSearchTestData() throws IOException { assertTrue(reviewersResponse.isAcknowledged()); BulkRequest bulkRequest = new BulkRequest(); - bulkRequest.add(new IndexRequest("posts", "doc", "1") + bulkRequest.add(new IndexRequest("posts", "_doc", "1") .source(XContentType.JSON, "title", "In which order are my Elasticsearch queries executed?", "user", Arrays.asList("kimchy", "luca"), "innerObject", Collections.singletonMap("key", "value"))); - bulkRequest.add(new IndexRequest("posts", "doc", "2") + bulkRequest.add(new IndexRequest("posts", "_doc", "2") .source(XContentType.JSON, "title", "Current status and upcoming changes in Elasticsearch", "user", Arrays.asList("kimchy", "christoph"), "innerObject", Collections.singletonMap("key", "value"))); - bulkRequest.add(new IndexRequest("posts", "doc", "3") + bulkRequest.add(new IndexRequest("posts", "_doc", "3") .source(XContentType.JSON, "title", "The Future of Federated Search in Elasticsearch", "user", Arrays.asList("kimchy", "tanguy"), "innerObject", Collections.singletonMap("key", "value"))); - bulkRequest.add(new IndexRequest("authors", "doc", "1") + bulkRequest.add(new IndexRequest("authors", "_doc", "1") .source(XContentType.JSON, "user", "kimchy")); - bulkRequest.add(new IndexRequest("contributors", "doc", "1") + bulkRequest.add(new IndexRequest("contributors", "_doc", "1") .source(XContentType.JSON, "user", "tanguy")); @@ -1318,7 +1318,7 @@ public void testCount() throws Exception { { // tag::count-request-indices-types CountRequest countRequest = new CountRequest("blog"); // <1> - countRequest.types("doc"); // <2> + countRequest.types("_doc"); // <2> // end::count-request-indices-types // tag::count-request-routing countRequest.routing("routing"); // <1> @@ -1408,17 +1408,17 @@ private static void indexCountTestData() throws IOException { assertTrue(authorsResponse.isAcknowledged()); BulkRequest bulkRequest = new BulkRequest(); - bulkRequest.add(new IndexRequest("blog", "doc", "1") + bulkRequest.add(new IndexRequest("blog", "_doc", "1") .source(XContentType.JSON, "title", "Doubling Down on Open?", "user", Collections.singletonList("kimchy"), "innerObject", Collections.singletonMap("key", "value"))); - bulkRequest.add(new IndexRequest("blog", "doc", "2") + bulkRequest.add(new IndexRequest("blog", "_doc", "2") .source(XContentType.JSON, "title", "Swiftype Joins Forces with Elastic", "user", Arrays.asList("kimchy", "matt"), "innerObject", Collections.singletonMap("key", "value"))); - bulkRequest.add(new IndexRequest("blog", "doc", "3") + bulkRequest.add(new IndexRequest("blog", "_doc", "3") .source(XContentType.JSON, "title", "On Net Neutrality", "user", Arrays.asList("tyler", "kimchy"), "innerObject", Collections.singletonMap("key", "value"))); - bulkRequest.add(new IndexRequest("author", "doc", "1") + bulkRequest.add(new IndexRequest("author", "_doc", "1") .source(XContentType.JSON, "user", "kimchy"));