diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java index fcea9044856c5..1e7566e89a42d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java @@ -1223,7 +1223,7 @@ public void testMultiSearch() throws IOException { }; MultiSearchRequest.readMultiLineFormat(new BytesArray(EntityUtils.toByteArray(request.getEntity())), REQUEST_BODY_CONTENT_TYPE.xContent(), consumer, null, multiSearchRequest.indicesOptions(), null, null, null, - xContentRegistry(), true); + xContentRegistry(), true, key -> false); assertEquals(requests, multiSearchRequest.requests()); } diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateAction.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateAction.java index 52052d5ad67ac..1896cd0eec143 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateAction.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateAction.java @@ -32,6 +32,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.function.Function; import static java.util.Arrays.asList; import static org.elasticsearch.rest.RestRequest.Method.GET; @@ -49,7 +50,7 @@ public class RestMultiSearchTemplateAction extends BaseRestHandler { } - private final boolean allowExplicitIndex; + protected final boolean allowExplicitIndex; public RestMultiSearchTemplateAction(Settings settings) { this.allowExplicitIndex = MULTI_ALLOW_EXPLICIT_INDEX.get(settings); @@ -79,6 +80,19 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client * Parses a {@link RestRequest} body and returns a {@link MultiSearchTemplateRequest} */ public static MultiSearchTemplateRequest parseRequest(RestRequest restRequest, boolean allowExplicitIndex) throws IOException { + return parseRequest(restRequest,allowExplicitIndex, k->false); + } + + /** + * Parses a {@link RestRequest} body and returns a {@link MultiSearchTemplateRequest} + * @param typeConsumer - A function used to validate if a provided xContent key is allowed. + * This is useful for xContent compatibility to determine + * if a key is allowed to be present in version agnostic manner. + * The provided function should return false if the key is not allowed. + */ + public static MultiSearchTemplateRequest parseRequest(RestRequest restRequest, + boolean allowExplicitIndex, + Function typeConsumer) throws IOException { MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest(); if (restRequest.hasParam("max_concurrent_searches")) { multiRequest.maxConcurrentSearchRequests(restRequest.paramAsInt("max_concurrent_searches", 0)); @@ -94,7 +108,7 @@ public static MultiSearchTemplateRequest parseRequest(RestRequest restRequest, b throw new IllegalArgumentException("Malformed search template"); } RestSearchAction.checkRestTotalHits(restRequest, searchRequest); - }); + }, typeConsumer); return multiRequest; } diff --git a/modules/rest-compatibility/build.gradle b/modules/rest-compatibility/build.gradle index 96e81f07d823a..a4b9b20d046a5 100644 --- a/modules/rest-compatibility/build.gradle +++ b/modules/rest-compatibility/build.gradle @@ -19,7 +19,12 @@ esplugin { description 'Adds a compatiblity layer for the prior major versions REST API' - classname 'org.elasticsearch.rest.compat.RestCompatPlugin' + classname 'org.elasticsearch.compat.RestCompatPlugin' +} + +dependencies { + implementation project(':modules:lang-mustache') + implementation project(':modules:reindex') } integTest.enabled = false diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/compat/RestCompatPlugin.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/compat/RestCompatPlugin.java similarity index 63% rename from modules/rest-compatibility/src/main/java/org/elasticsearch/rest/compat/RestCompatPlugin.java rename to modules/rest-compatibility/src/main/java/org/elasticsearch/compat/RestCompatPlugin.java index 3fe0c356a5a34..f82ee1590c395 100644 --- a/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/compat/RestCompatPlugin.java +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/compat/RestCompatPlugin.java @@ -7,7 +7,7 @@ * 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 + * 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 @@ -17,7 +17,7 @@ * under the License. */ -package org.elasticsearch.rest.compat; +package org.elasticsearch.compat; import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; @@ -26,13 +26,21 @@ import org.elasticsearch.common.settings.IndexScopedSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; +import org.elasticsearch.index.reindex.RestDeleteByQueryActionV7; +import org.elasticsearch.index.reindex.RestUpdateByQueryActionV7; import org.elasticsearch.plugins.ActionPlugin; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestHandler; -import org.elasticsearch.rest.compat.version7.RestCreateIndexActionV7; -import org.elasticsearch.rest.compat.version7.RestGetActionV7; -import org.elasticsearch.rest.compat.version7.RestIndexActionV7; +import org.elasticsearch.rest.action.admin.indices.RestCreateIndexActionV7; +import org.elasticsearch.rest.action.document.RestGetActionV7; +import org.elasticsearch.rest.action.document.RestIndexActionV7; +import org.elasticsearch.rest.action.document.RestMultiTermVectorsActionV7; +import org.elasticsearch.rest.action.document.RestTermVectorsActionV7; +import org.elasticsearch.rest.action.search.RestMultiSearchActionV7; +import org.elasticsearch.rest.action.search.RestSearchActionV7; +import org.elasticsearch.script.mustache.RestMultiSearchTemplateActionV7; +import org.elasticsearch.script.mustache.RestSearchTemplateActionV7; import java.util.Collections; import java.util.List; @@ -52,11 +60,19 @@ public List getRestHandlers( ) { if (Version.CURRENT.major == 8) { return List.of( + new RestDeleteByQueryActionV7(), + new RestUpdateByQueryActionV7(), + new RestCreateIndexActionV7(), new RestGetActionV7(), new RestIndexActionV7.CompatibleRestIndexAction(), new RestIndexActionV7.CompatibleCreateHandler(), new RestIndexActionV7.CompatibleAutoIdHandler(nodesInCluster), - new RestCreateIndexActionV7() + new RestTermVectorsActionV7(), + new RestMultiTermVectorsActionV7(), + new RestSearchActionV7(), + new RestMultiSearchActionV7(settings), + new RestSearchTemplateActionV7(), + new RestMultiSearchTemplateActionV7(settings) ); } return Collections.emptyList(); diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/compat/TypeConsumer.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/compat/TypeConsumer.java new file mode 100644 index 0000000000000..f7c0e9b9e9283 --- /dev/null +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/compat/TypeConsumer.java @@ -0,0 +1,53 @@ +/* + * 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.compat; + +import org.elasticsearch.common.Strings; +import org.elasticsearch.rest.RestRequest; + +import java.util.Set; +import java.util.function.Function; + +public class TypeConsumer implements Function { + + private final RestRequest request; + private final Set fieldNames; + private boolean foundTypeInBody = false; + + public TypeConsumer(RestRequest request, String... fieldNames) { + this.request = request; + this.fieldNames = Set.of(fieldNames); + } + + @Override + public Boolean apply(String fieldName) { + if (fieldNames.contains(fieldName)) { + foundTypeInBody = true; + return true; + } + return false; + } + + public boolean hasTypes() { + // TODO can params be types too? or _types? + String[] types = Strings.splitStringByCommaToArray(request.param("type")); + return types.length > 0 || foundTypeInBody; + } +} diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/index/reindex/RestDeleteByQueryActionV7.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/index/reindex/RestDeleteByQueryActionV7.java new file mode 100644 index 0000000000000..9fdba5d9edbc0 --- /dev/null +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/index/reindex/RestDeleteByQueryActionV7.java @@ -0,0 +1,59 @@ +/* + * 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.index.reindex; + +import org.elasticsearch.Version; +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.logging.DeprecationLogger; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.action.search.RestSearchActionV7; + +import java.io.IOException; +import java.util.List; + +import static org.elasticsearch.rest.RestRequest.Method.POST; + +public class RestDeleteByQueryActionV7 extends RestDeleteByQueryAction { + private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestDeleteByQueryActionV7.class); + + @Override + public List routes() { + return List.of(new Route(POST, "/{index}/{type}/_delete_by_query")); + } + + @Override + public String getName() { + return super.getName() + "_v7"; + } + + @Override + public Version compatibleWithVersion() { + return Version.V_7_0_0; + } + + @Override + public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { + if (request.hasParam("type")) { + deprecationLogger.deprecate("search_with_types", RestSearchActionV7.TYPES_DEPRECATION_MESSAGE); + request.param("type"); + } + return super.prepareRequest(request, client); + } +} diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/index/reindex/RestUpdateByQueryActionV7.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/index/reindex/RestUpdateByQueryActionV7.java new file mode 100644 index 0000000000000..29486286597c5 --- /dev/null +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/index/reindex/RestUpdateByQueryActionV7.java @@ -0,0 +1,60 @@ +/* + * 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.index.reindex; + +import org.elasticsearch.Version; +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.logging.DeprecationLogger; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.action.search.RestSearchActionV7; + +import java.io.IOException; +import java.util.List; + +import static org.elasticsearch.rest.RestRequest.Method.POST; + +public class RestUpdateByQueryActionV7 extends RestUpdateByQueryAction { + + private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestUpdateByQueryActionV7.class); + + @Override + public List routes() { + return List.of(new Route(POST, "/{index}/{type}/_update_by_query")); + } + + @Override + public String getName() { + return super.getName() + "_v7"; + } + + @Override + public Version compatibleWithVersion() { + return Version.V_7_0_0; + } + + @Override + public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { + if (request.hasParam("type")) { + deprecationLogger.deprecate("search_with_types", RestSearchActionV7.TYPES_DEPRECATION_MESSAGE); + request.param("type"); + } + return super.prepareRequest(request, client); + } +} diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/compat/version7/RestCreateIndexActionV7.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionV7.java similarity index 90% rename from modules/rest-compatibility/src/main/java/org/elasticsearch/rest/compat/version7/RestCreateIndexActionV7.java rename to modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionV7.java index 6f5d3d1fe272f..463866af9d894 100644 --- a/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/compat/version7/RestCreateIndexActionV7.java +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionV7.java @@ -7,7 +7,7 @@ * 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 + * 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 @@ -17,7 +17,7 @@ * under the License. */ -package org.elasticsearch.rest.compat.version7; +package org.elasticsearch.rest.action.admin.indices; import org.elasticsearch.Version; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; @@ -28,7 +28,6 @@ import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; -import org.elasticsearch.rest.action.admin.indices.RestCreateIndexAction; import java.io.IOException; import java.util.Collections; @@ -46,7 +45,7 @@ public class RestCreateIndexActionV7 extends RestCreateIndexAction { @Override public String getName() { - return "create_index_action_v7"; + return super.getName() + "_v7"; } @Override @@ -56,19 +55,19 @@ public Version compatibleWithVersion() { @Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { - CreateIndexRequest createIndexRequest = prepareRequest(request); + CreateIndexRequest createIndexRequest = prepareV7Request(request); return channel -> client.admin().indices().create(createIndexRequest, new RestToXContentListener<>(channel)); } // default scope for testing - CreateIndexRequest prepareRequest(RestRequest request) { + CreateIndexRequest prepareV7Request(RestRequest request) { CreateIndexRequest createIndexRequest = new CreateIndexRequest(request.param("index")); if (request.hasContent()) { Map sourceAsMap = XContentHelper.convertToMap(request.requiredContent(), false, request.getXContentType()).v2(); request.param(INCLUDE_TYPE_NAME_PARAMETER);// just consume, it is always replaced with _doc - sourceAsMap = prepareMappings(sourceAsMap, request); + sourceAsMap = prepareMappingsV7(sourceAsMap, request); createIndexRequest.source(sourceAsMap, LoggingDeprecationHandler.INSTANCE); } @@ -79,7 +78,7 @@ CreateIndexRequest prepareRequest(RestRequest request) { return createIndexRequest; } - static Map prepareMappings(Map source, RestRequest request) { + static Map prepareMappingsV7(Map source, RestRequest request) { final boolean includeTypeName = request.paramAsBoolean(INCLUDE_TYPE_NAME_PARAMETER, false); @SuppressWarnings("unchecked") diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/compat/version7/RestGetActionV7.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestGetActionV7.java similarity index 87% rename from modules/rest-compatibility/src/main/java/org/elasticsearch/rest/compat/version7/RestGetActionV7.java rename to modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestGetActionV7.java index 68efa7088bbe7..a75fc46d2428e 100644 --- a/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/compat/version7/RestGetActionV7.java +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestGetActionV7.java @@ -7,7 +7,7 @@ * 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 + * 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 @@ -17,15 +17,12 @@ * under the License. */ -package org.elasticsearch.rest.compat.version7; +package org.elasticsearch.rest.action.document; -import org.apache.logging.log4j.LogManager; import org.elasticsearch.Version; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.document.RestGetAction; -import org.elasticsearch.rest.action.document.RestIndexAction; import java.io.IOException; import java.util.List; @@ -41,7 +38,7 @@ public class RestGetActionV7 extends RestGetAction { @Override public String getName() { - return "document_get_action_v7"; + return super.getName() + "_v7"; } @Override diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/compat/version7/RestIndexActionV7.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestIndexActionV7.java similarity index 94% rename from modules/rest-compatibility/src/main/java/org/elasticsearch/rest/compat/version7/RestIndexActionV7.java rename to modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestIndexActionV7.java index 1368509a5e72a..134deea9ff9db 100644 --- a/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/compat/version7/RestIndexActionV7.java +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestIndexActionV7.java @@ -7,7 +7,7 @@ * 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 + * 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 @@ -17,15 +17,13 @@ * under the License. */ -package org.elasticsearch.rest.compat.version7; +package org.elasticsearch.rest.action.document; -import org.apache.logging.log4j.LogManager; import org.elasticsearch.Version; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.document.RestIndexAction; import java.io.IOException; import java.util.List; @@ -48,7 +46,7 @@ private static void logDeprecationMessage() { public static class CompatibleRestIndexAction extends RestIndexAction { @Override public String getName() { - return "document_index_action_v7"; + return super.getName() + "_v7"; } @Override diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestMultiTermVectorsActionV7.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestMultiTermVectorsActionV7.java new file mode 100644 index 0000000000000..d4df9c2a9baab --- /dev/null +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestMultiTermVectorsActionV7.java @@ -0,0 +1,81 @@ +/* + * 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.document; + +import org.elasticsearch.Version; +import org.elasticsearch.action.termvectors.MultiTermVectorsRequest; +import org.elasticsearch.action.termvectors.TermVectorsRequest; +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.logging.DeprecationLogger; +import org.elasticsearch.compat.TypeConsumer; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.action.RestToXContentListener; + +import java.io.IOException; +import java.util.List; + +import static org.elasticsearch.rest.RestRequest.Method.GET; +import static org.elasticsearch.rest.RestRequest.Method.POST; + +public class RestMultiTermVectorsActionV7 extends RestMultiTermVectorsAction { + private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestMultiTermVectorsActionV7.class); + static final String TYPES_DEPRECATION_MESSAGE = "[types removal] " + "Specifying types in multi term vector requests is deprecated."; + + @Override + public List routes() { + return List.of( + new Route(GET, "/_mtermvectors"), + new Route(POST, "/_mtermvectors"), + new Route(GET, "/{index}/_mtermvectors"), + new Route(POST, "/{index}/_mtermvectors"), + new Route(GET, "/{index}/{type}/_mtermvectors"), + new Route(POST, "/{index}/{type}/_mtermvectors") + ); + } + + @Override + public String getName() { + return super.getName() + "_v7"; + } + + @Override + public Version compatibleWithVersion() { + return Version.V_7_0_0; + } + + @Override + public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { + TypeConsumer typeConsumer = new TypeConsumer(request, "_type"); + + MultiTermVectorsRequest multiTermVectorsRequest = new MultiTermVectorsRequest(); + TermVectorsRequest template = new TermVectorsRequest().index(request.param("index")); + + RestTermVectorsAction.readURIParameters(template, request); + multiTermVectorsRequest.ids(Strings.commaDelimitedListToStringArray(request.param("ids"))); + request.withContentOrSourceParamParserOrNull(p -> multiTermVectorsRequest.add(template, p, typeConsumer)); + + if (typeConsumer.hasTypes()) { + request.param("type"); + deprecationLogger.deprecate("termvectors_with_types", TYPES_DEPRECATION_MESSAGE); + } + return channel -> client.multiTermVectors(multiTermVectorsRequest, new RestToXContentListener<>(channel)); + } +} diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestTermVectorsActionV7.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestTermVectorsActionV7.java new file mode 100644 index 0000000000000..3d083e066432a --- /dev/null +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestTermVectorsActionV7.java @@ -0,0 +1,87 @@ +/* + * 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.document; + +import org.elasticsearch.Version; +import org.elasticsearch.action.termvectors.TermVectorsRequest; +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.logging.DeprecationLogger; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.compat.TypeConsumer; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.action.RestToXContentListener; + +import java.io.IOException; +import java.util.List; + +import static org.elasticsearch.rest.RestRequest.Method.GET; +import static org.elasticsearch.rest.RestRequest.Method.POST; + +public class RestTermVectorsActionV7 extends RestTermVectorsAction { + private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestTermVectorsActionV7.class); + public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] " + "Specifying types in term vector requests is deprecated."; + + @Override + public List routes() { + return List.of( + new Route(GET, "/{index}/_termvectors"), + new Route(POST, "/{index}/_termvectors"), + new Route(GET, "/{index}/_termvectors/{id}"), + new Route(POST, "/{index}/_termvectors/{id}"), + // Deprecated typed endpoints. + new Route(GET, "/{index}/{type}/_termvectors"), + new Route(POST, "/{index}/{type}/_termvectors"), + new Route(GET, "/{index}/{type}/{id}/_termvectors"), + new Route(POST, "/{index}/{type}/{id}/_termvectors") + ); + } + + @Override + public String getName() { + return super.getName() + "_v7"; + } + + @Override + public Version compatibleWithVersion() { + return Version.V_7_0_0; + } + + @Override + public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { + TypeConsumer typeConsumer = new TypeConsumer(request, "_type"); + + TermVectorsRequest termVectorsRequest = new TermVectorsRequest(request.param("index"), request.param("id")); + + if (request.hasContentOrSourceParam()) { + try (XContentParser parser = request.contentOrSourceParamParser()) { + TermVectorsRequest.parseRequest(termVectorsRequest, parser, typeConsumer); + } + } + readURIParameters(termVectorsRequest, request); + + if (typeConsumer.hasTypes()) { + request.param("type"); + deprecationLogger.deprecate("termvectors_with_types", TYPES_DEPRECATION_MESSAGE); + } + + return channel -> client.termVectors(termVectorsRequest, new RestToXContentListener<>(channel)); + } + +} diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchActionV7.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchActionV7.java new file mode 100644 index 0000000000000..e440925dc2250 --- /dev/null +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchActionV7.java @@ -0,0 +1,80 @@ +/* + * 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.search; + +import org.elasticsearch.Version; +import org.elasticsearch.action.search.MultiSearchRequest; +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.logging.DeprecationLogger; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.compat.TypeConsumer; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.action.RestToXContentListener; + +import java.io.IOException; +import java.util.List; + +import static org.elasticsearch.rest.RestRequest.Method.GET; +import static org.elasticsearch.rest.RestRequest.Method.POST; + +public class RestMultiSearchActionV7 extends RestMultiSearchAction { + private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestMultiSearchActionV7.class); + static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" + " Specifying types in multi search requests is deprecated."; + + public RestMultiSearchActionV7(Settings settings) { + super(settings); + } + + @Override + public List routes() { + return List.of( + new Route(GET, "/_msearch"), + new Route(POST, "/_msearch"), + new Route(GET, "/{index}/_msearch"), + new Route(POST, "/{index}/_msearch"), + // Deprecated typed endpoints. + new Route(GET, "/{index}/{type}/_msearch"), + new Route(POST, "/{index}/{type}/_msearch") + ); + } + + @Override + public String getName() { + return super.getName() + "_v7"; + } + + @Override + public Version compatibleWithVersion() { + return Version.V_7_0_0; + } + + @Override + public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { + request.param("type"); + TypeConsumer typeConsumer = new TypeConsumer(request, "type", "types"); + + MultiSearchRequest multiSearchRequest = parseRequest(request, allowExplicitIndex, typeConsumer); + if (typeConsumer.hasTypes()) { + deprecationLogger.deprecate("msearch_with_types", TYPES_DEPRECATION_MESSAGE); + } + return channel -> client.multiSearch(multiSearchRequest, new RestToXContentListener<>(channel)); + } + +} diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/search/RestSearchActionV7.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/search/RestSearchActionV7.java new file mode 100644 index 0000000000000..7d64c854d9e36 --- /dev/null +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/search/RestSearchActionV7.java @@ -0,0 +1,71 @@ +/* + * 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.search; + +import org.elasticsearch.Version; +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.logging.DeprecationLogger; +import org.elasticsearch.rest.RestRequest; + +import java.io.IOException; +import java.util.List; + +import static org.elasticsearch.rest.RestRequest.Method.GET; +import static org.elasticsearch.rest.RestRequest.Method.POST; + +public class RestSearchActionV7 extends RestSearchAction { + public static final String INCLUDE_TYPE_NAME_PARAMETER = "include_type_name"; + public static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" + " Specifying types in search requests is deprecated."; + + private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestSearchActionV7.class); + + @Override + public List routes() { + return List.of( + new Route(GET, "/_search"), + new Route(POST, "/_search"), + new Route(GET, "/{index}/_search"), + new Route(POST, "/{index}/_search"), + new Route(GET, "/{index}/{type}/_search"), + new Route(POST, "/{index}/{type}/_search") + ); + } + + @Override + public String getName() { + return super.getName() + "_v7"; + } + + @Override + public Version compatibleWithVersion() { + return Version.V_7_0_0; + } + + @Override + public RestChannelConsumer prepareRequest(RestRequest request, final NodeClient client) throws IOException { + request.param(INCLUDE_TYPE_NAME_PARAMETER); + + if (request.hasParam("type")) { + deprecationLogger.deprecate("search_with_types", TYPES_DEPRECATION_MESSAGE); + } + + return super.prepareRequest(request, client); + } +} diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateActionV7.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateActionV7.java new file mode 100644 index 0000000000000..87c65cd2d4e59 --- /dev/null +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateActionV7.java @@ -0,0 +1,111 @@ +/* + * 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.script.mustache; + +import org.elasticsearch.compat.TypeConsumer; +import org.elasticsearch.Version; +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.logging.DeprecationLogger; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.rest.action.search.RestMultiSearchAction; +import org.elasticsearch.rest.action.search.RestSearchAction; + +import java.io.IOException; +import java.util.List; + +import static org.elasticsearch.rest.RestRequest.Method.GET; +import static org.elasticsearch.rest.RestRequest.Method.POST; + +public class RestMultiSearchTemplateActionV7 extends RestMultiSearchTemplateAction { + private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestMultiSearchTemplateActionV7.class); + + static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" + + " Specifying types in multi search template requests is deprecated."; + + public RestMultiSearchTemplateActionV7(Settings settings) { + super(settings); + } + + @Override + public List routes() { + return List.of( + new Route(GET, "/_msearch/template"), + new Route(POST, "/_msearch/template"), + new Route(GET, "/{index}/_msearch/template"), + new Route(POST, "/{index}/_msearch/template"), + + // Deprecated typed endpoints. + new Route(GET, "/{index}/{type}/_msearch/template"), + new Route(POST, "/{index}/{type}/_msearch/template") + ); + } + + @Override + public String getName() { + return super.getName() + "_v7"; + } + + @Override + public Version compatibleWithVersion() { + return Version.V_7_0_0; + } + + @Override + public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { + request.param("type"); + + TypeConsumer typeConsumer = new TypeConsumer(request, "type", "types"); + MultiSearchTemplateRequest multiRequest = parseRequest(request, allowExplicitIndex, typeConsumer); + if (typeConsumer.hasTypes()) { + deprecationLogger.deprecate("msearch_with_types", TYPES_DEPRECATION_MESSAGE); + } + return channel -> client.execute(MultiSearchTemplateAction.INSTANCE, multiRequest, new RestToXContentListener<>(channel)); + } + + /** + * Parses a {@link RestRequest} body and returns a {@link MultiSearchTemplateRequest} + */ + public static MultiSearchTemplateRequest parseRequest(RestRequest restRequest, boolean allowExplicitIndex) throws IOException { + MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest(); + if (restRequest.hasParam("max_concurrent_searches")) { + multiRequest.maxConcurrentSearchRequests(restRequest.paramAsInt("max_concurrent_searches", 0)); + } + + RestMultiSearchAction.parseMultiLineRequest( + restRequest, + multiRequest.indicesOptions(), + allowExplicitIndex, + (searchRequest, bytes) -> { + SearchTemplateRequest searchTemplateRequest = SearchTemplateRequest.fromXContent(bytes); + if (searchTemplateRequest.getScript() != null) { + searchTemplateRequest.setRequest(searchRequest); + multiRequest.add(searchTemplateRequest); + } else { + throw new IllegalArgumentException("Malformed search template"); + } + RestSearchAction.checkRestTotalHits(restRequest, searchRequest); + }, + k -> false + ); + return multiRequest; + } +} diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/script/mustache/RestSearchTemplateActionV7.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/script/mustache/RestSearchTemplateActionV7.java new file mode 100644 index 0000000000000..8eefa2708d447 --- /dev/null +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/script/mustache/RestSearchTemplateActionV7.java @@ -0,0 +1,68 @@ +/* + * 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.script.mustache; + +import org.elasticsearch.Version; +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.logging.DeprecationLogger; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.action.search.RestSearchActionV7; + +import java.io.IOException; +import java.util.List; + +import static org.elasticsearch.rest.RestRequest.Method.GET; +import static org.elasticsearch.rest.RestRequest.Method.POST; + +public class RestSearchTemplateActionV7 extends RestSearchTemplateAction { + private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestSearchTemplateActionV7.class); + + @Override + public List routes() { + return List.of( + new Route(GET, "/_search/template"), + new Route(POST, "/_search/template"), + new Route(GET, "/{index}/_search/template"), + new Route(POST, "/{index}/_search/template"), + // Deprecated typed endpoints. + new Route(GET, "/{index}/{type}/_search/template"), + new Route(POST, "/{index}/{type}/_search/template") + ); + } + + @Override + public String getName() { + return super.getName() + "_v7"; + } + + @Override + public Version compatibleWithVersion() { + return Version.V_7_0_0; + } + + @Override + public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { + if (request.hasParam("type")) { + deprecationLogger.deprecate("search_with_types", RestSearchActionV7.TYPES_DEPRECATION_MESSAGE); + request.param("type"); + } + return super.prepareRequest(request, client); + } +} diff --git a/modules/rest-compatibility/src/test/java/org/elasticsearch/compat/FakeCompatRestRequestBuilder.java b/modules/rest-compatibility/src/test/java/org/elasticsearch/compat/FakeCompatRestRequestBuilder.java new file mode 100644 index 0000000000000..9c2d98ad2f4f0 --- /dev/null +++ b/modules/rest-compatibility/src/test/java/org/elasticsearch/compat/FakeCompatRestRequestBuilder.java @@ -0,0 +1,44 @@ +/* + * 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.compat; + +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.test.rest.FakeRestRequest; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import static org.elasticsearch.test.ESTestCase.randomFrom; + +public class FakeCompatRestRequestBuilder extends FakeRestRequest.Builder { + final String mimeType = randomFrom("application/vnd.elasticsearch+json;compatible-with=7"); + final List contentTypeHeader = Collections.singletonList(mimeType); + + public FakeCompatRestRequestBuilder(NamedXContentRegistry xContentRegistry) { + super(xContentRegistry); + } + + @Override + public FakeRestRequest build() { + addHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader)); + return super.build(); + } +} diff --git a/modules/rest-compatibility/src/test/java/org/elasticsearch/index/reindex/RestDeleteByQueryActionV7Tests.java b/modules/rest-compatibility/src/test/java/org/elasticsearch/index/reindex/RestDeleteByQueryActionV7Tests.java new file mode 100644 index 0000000000000..7722a1a9dcc97 --- /dev/null +++ b/modules/rest-compatibility/src/test/java/org/elasticsearch/index/reindex/RestDeleteByQueryActionV7Tests.java @@ -0,0 +1,65 @@ +/* + * 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.index.reindex; + +import org.elasticsearch.compat.FakeCompatRestRequestBuilder; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.action.search.RestSearchActionV7; +import org.elasticsearch.test.rest.RestActionTestCase; +import org.junit.Before; + +import java.io.IOException; + +import static java.util.Collections.emptyList; + +public class RestDeleteByQueryActionV7Tests extends RestActionTestCase { + private RestDeleteByQueryActionV7 action; + + @Before + public void setUpAction() { + action = new RestDeleteByQueryActionV7(); + controller().registerHandler(action); + } + + public void testTypeInPath() throws IOException { + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.POST) + .withPath("/some_index/some_type/_delete_by_query") + .build(); + dispatchRequest(request); + + // checks the type in the URL is propagated correctly to the request object + // only works after the request is dispatched, so its params are filled from url. + DeleteByQueryRequest dbqRequest = action.buildRequest(request); + // assertArrayEquals(new String[]{"some_type"}, dbqRequest.getDocTypes()); + + // RestDeleteByQueryAction itself doesn't check for a deprecated type usage + // checking here for a deprecation from its internal search request + assertWarnings(RestSearchActionV7.TYPES_DEPRECATION_MESSAGE); + } + + public void testParseEmpty() throws IOException { + DeleteByQueryRequest request = action.buildRequest( + new FakeCompatRestRequestBuilder(new NamedXContentRegistry(emptyList())).build() + ); + // assertEquals(AbstractBulkByScrollRequest.SIZE_ALL_MATCHES, request.getSize()); + assertEquals(AbstractBulkByScrollRequest.DEFAULT_SCROLL_SIZE, request.getSearchRequest().source().size()); + } +} diff --git a/modules/rest-compatibility/src/test/java/org/elasticsearch/index/reindex/RestUpdateByQueryActionTests.java b/modules/rest-compatibility/src/test/java/org/elasticsearch/index/reindex/RestUpdateByQueryActionTests.java new file mode 100644 index 0000000000000..4f97bbdaa9585 --- /dev/null +++ b/modules/rest-compatibility/src/test/java/org/elasticsearch/index/reindex/RestUpdateByQueryActionTests.java @@ -0,0 +1,66 @@ +/* + * 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.index.reindex; + +import org.elasticsearch.compat.FakeCompatRestRequestBuilder; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.action.search.RestSearchActionV7; +import org.elasticsearch.test.rest.RestActionTestCase; +import org.junit.Before; + +import java.io.IOException; + +import static java.util.Collections.emptyList; + +public class RestUpdateByQueryActionTests extends RestActionTestCase { + + private RestUpdateByQueryAction action; + + @Before + public void setUpAction() { + action = new RestUpdateByQueryActionV7(); + controller().registerHandler(action); + } + + public void testTypeInPath() throws IOException { + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.POST) + .withPath("/some_index/some_type/_update_by_query") + .build(); + dispatchRequest(request); + + // checks the type in the URL is propagated correctly to the request object + // only works after the request is dispatched, so its params are filled from url. + UpdateByQueryRequest ubqRequest = action.buildRequest(request); + // assertArrayEquals(new String[]{"some_type"}, ubqRequest.getDocTypes()); + + // RestUpdateByQueryAction itself doesn't check for a deprecated type usage + // checking here for a deprecation from its internal search request + assertWarnings(RestSearchActionV7.TYPES_DEPRECATION_MESSAGE); + } + + public void testParseEmpty() throws IOException { + UpdateByQueryRequest request = action.buildRequest( + new FakeCompatRestRequestBuilder(new NamedXContentRegistry(emptyList())).build() + ); + // assertEquals(AbstractBulkByScrollRequest.SIZE_ALL_MATCHES, request.getSize()); + assertEquals(AbstractBulkByScrollRequest.DEFAULT_SCROLL_SIZE, request.getSearchRequest().source().size()); + } +} diff --git a/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/compat/version7/RestCreateIndexActionV7Tests.java b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionV7Tests.java similarity index 79% rename from modules/rest-compatibility/src/test/java/org/elasticsearch/rest/compat/version7/RestCreateIndexActionV7Tests.java rename to modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionV7Tests.java index 781ed2e12e3cc..35140a22b1479 100644 --- a/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/compat/version7/RestCreateIndexActionV7Tests.java +++ b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionV7Tests.java @@ -7,7 +7,7 @@ * 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 + * 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 @@ -17,28 +17,23 @@ * under the License. */ -package org.elasticsearch.rest.compat.version7; +package org.elasticsearch.rest.action.admin.indices; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.common.bytes.BytesArray; +import org.elasticsearch.compat.FakeCompatRestRequestBuilder; 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.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; import static org.hamcrest.Matchers.equalTo; public class RestCreateIndexActionV7Tests extends RestActionTestCase { - String mimeType = "application/vnd.elasticsearch+json;compatible-with=7"; - List contentTypeHeader = Collections.singletonList(mimeType); - RestCreateIndexActionV7 restHandler = new RestCreateIndexActionV7(); @Before @@ -61,14 +56,13 @@ public void testTypeInMapping() throws IOException { Map params = new HashMap<>(); params.put(RestCreateIndexActionV7.INCLUDE_TYPE_NAME_PARAMETER, "true"); - RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.PUT) - .withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader)) + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.PUT) .withPath("/some_index") .withParams(params) .withContent(new BytesArray(content), null) .build(); - CreateIndexRequest createIndexRequest = restHandler.prepareRequest(request); + CreateIndexRequest createIndexRequest = restHandler.prepareV7Request(request); // some_type is replaced with _doc assertThat(createIndexRequest.mappings(), equalTo("{\"_doc\":{\"properties\":{\"field1\":{\"type\":\"text\"}}}}")); } diff --git a/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/compat/version7/RestGetActionV7Tests.java b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestGetActionV7Tests.java similarity index 58% rename from modules/rest-compatibility/src/test/java/org/elasticsearch/rest/compat/version7/RestGetActionV7Tests.java rename to modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestGetActionV7Tests.java index 124138ea2a142..725e414fae95a 100644 --- a/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/compat/version7/RestGetActionV7Tests.java +++ b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestGetActionV7Tests.java @@ -17,20 +17,15 @@ * under the License. */ -package org.elasticsearch.rest.compat.version7; +package org.elasticsearch.rest.action.document; +import org.elasticsearch.compat.FakeCompatRestRequestBuilder; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.test.rest.RestActionTestCase; import org.junit.Before; -import java.util.Collections; -import java.util.List; -import java.util.Map; - public class RestGetActionV7Tests extends RestActionTestCase { - final String mimeType = "application/vnd.elasticsearch+json;compatible-with=7"; - final List contentTypeHeader = Collections.singletonList(mimeType); @Before public void setUpAction() { @@ -38,18 +33,18 @@ public void setUpAction() { } public void testTypeInPathWithGet() { - FakeRestRequest.Builder deprecatedRequest = new FakeRestRequest.Builder(xContentRegistry()).withHeaders( - Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader) - ).withPath("/some_index/some_type/some_id"); - dispatchRequest(deprecatedRequest.withMethod(RestRequest.Method.GET).build()); + FakeRestRequest deprecatedRequest = new FakeCompatRestRequestBuilder(xContentRegistry()).withPath("/some_index/some_type/some_id") + .withMethod(RestRequest.Method.GET) + .build(); + dispatchRequest(deprecatedRequest); assertWarnings(RestGetActionV7.TYPES_DEPRECATION_MESSAGE); } public void testTypeInPathWithHead() { - FakeRestRequest.Builder deprecatedRequest = new FakeRestRequest.Builder(xContentRegistry()).withHeaders( - Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader) - ).withPath("/some_index/some_type/some_id"); - dispatchRequest(deprecatedRequest.withMethod(RestRequest.Method.HEAD).build()); + FakeRestRequest deprecatedRequest = new FakeCompatRestRequestBuilder(xContentRegistry()).withPath("/some_index/some_type/some_id") + .withMethod(RestRequest.Method.HEAD) + .build(); + dispatchRequest(deprecatedRequest); assertWarnings(RestGetActionV7.TYPES_DEPRECATION_MESSAGE); } diff --git a/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/compat/version7/RestIndexActionV7Tests.java b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestIndexActionV7Tests.java similarity index 76% rename from modules/rest-compatibility/src/test/java/org/elasticsearch/rest/compat/version7/RestIndexActionV7Tests.java rename to modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestIndexActionV7Tests.java index 6fb7b2dfac2f2..69812d67333a0 100644 --- a/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/compat/version7/RestIndexActionV7Tests.java +++ b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestIndexActionV7Tests.java @@ -17,17 +17,16 @@ * under the License. */ -package org.elasticsearch.rest.compat.version7; +package org.elasticsearch.rest.action.document; +import org.elasticsearch.compat.FakeCompatRestRequestBuilder; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.test.rest.RestActionTestCase; import org.junit.Before; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.concurrent.atomic.AtomicReference; public class RestIndexActionV7Tests extends RestActionTestCase { @@ -46,8 +45,7 @@ public void setUpAction() { public void testTypeInPath() { // using CompatibleRestIndexAction - RestRequest deprecatedRequest = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.PUT) - .withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader)) + RestRequest deprecatedRequest = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.PUT) .withPath("/some_index/some_type/some_id") .build(); dispatchRequest(deprecatedRequest); @@ -56,8 +54,7 @@ public void testTypeInPath() { public void testCreateWithTypeInPath() { // using CompatibleCreateHandler - RestRequest deprecatedRequest = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.PUT) - .withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader)) + RestRequest deprecatedRequest = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.PUT) .withPath("/some_index/some_type/some_id/_create") .build(); dispatchRequest(deprecatedRequest); @@ -66,8 +63,7 @@ public void testCreateWithTypeInPath() { public void testAutoIdWithType() { // using CompatibleAutoIdHandler - RestRequest deprecatedRequest = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.POST) - .withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader)) + RestRequest deprecatedRequest = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.POST) .withPath("/some_index/some_type/") .build(); dispatchRequest(deprecatedRequest); diff --git a/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestMultiTermVectorsActionV7Tests.java b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestMultiTermVectorsActionV7Tests.java new file mode 100644 index 0000000000000..67623ebff8e0e --- /dev/null +++ b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestMultiTermVectorsActionV7Tests.java @@ -0,0 +1,86 @@ +/* + * 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.document; + +import org.elasticsearch.compat.FakeCompatRestRequestBuilder; +import org.elasticsearch.common.bytes.BytesReference; +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.rest.RestRequest.Method; +import org.elasticsearch.test.rest.RestActionTestCase; +import org.junit.Before; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class RestMultiTermVectorsActionV7Tests extends RestActionTestCase { + + @Before + public void setUpAction() { + controller().registerHandler(new RestMultiTermVectorsActionV7()); + } + + public void testTypeInPath() { + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(Method.POST) + .withPath("/some_index/some_type/_mtermvectors") + .build(); + + dispatchRequest(request); + assertWarnings(RestMultiTermVectorsActionV7.TYPES_DEPRECATION_MESSAGE); + } + + public void testTypeParameter() { + Map params = new HashMap<>(); + params.put("type", "some_type"); + + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(Method.GET) + .withPath("/some_index/_mtermvectors") + .withParams(params) + .build(); + + dispatchRequest(request); + assertWarnings(RestMultiTermVectorsActionV7.TYPES_DEPRECATION_MESSAGE); + } + + public void testTypeInBody() throws IOException { + XContentBuilder content = XContentFactory.jsonBuilder() + .startObject() + .startArray("docs") + .startObject() + .field("_type", "some_type") + .field("_id", 1) + .endObject() + .endArray() + .endObject(); + + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(Method.GET) + .withPath("/some_index/_mtermvectors") + .withContent(BytesReference.bytes(content), XContentType.JSON) + .build(); + + dispatchRequest(request); + // TODO change - now the deprecation warning is from MultiTermVectors.. + // assertWarnings(RestTermVectorsActionV7.TYPES_DEPRECATION_MESSAGE); + assertWarnings(RestMultiTermVectorsActionV7.TYPES_DEPRECATION_MESSAGE); + } +} diff --git a/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestTermVectorsActionV7Tests.java b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestTermVectorsActionV7Tests.java new file mode 100644 index 0000000000000..3aa7503e74813 --- /dev/null +++ b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestTermVectorsActionV7Tests.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.rest.action.document; + +import org.elasticsearch.compat.FakeCompatRestRequestBuilder; +import org.elasticsearch.common.bytes.BytesReference; +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.rest.RestRequest.Method; +import org.elasticsearch.test.rest.RestActionTestCase; +import org.junit.Before; + +import java.io.IOException; + +public class RestTermVectorsActionV7Tests extends RestActionTestCase { + + @Before + public void setUpAction() { + controller().registerHandler(new RestTermVectorsActionV7()); + } + + public void testTypeInPath() { + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(Method.POST) + .withPath("/some_index/some_type/some_id/_termvectors") + .build(); + + dispatchRequest(request); + assertWarnings(RestTermVectorsActionV7.TYPES_DEPRECATION_MESSAGE); + } + + public void testTypeInBody() throws IOException { + XContentBuilder content = XContentFactory.jsonBuilder().startObject().field("_type", "some_type").field("_id", 1).endObject(); + + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(Method.GET) + .withPath("/some_index/_termvectors/some_id") + .withContent(BytesReference.bytes(content), XContentType.JSON) + .build(); + + dispatchRequest(request); + assertWarnings(RestTermVectorsActionV7.TYPES_DEPRECATION_MESSAGE); + } +} diff --git a/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/search/RestMultiSearchActionV7Tests.java b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/search/RestMultiSearchActionV7Tests.java new file mode 100644 index 0000000000000..b221e9ebf6295 --- /dev/null +++ b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/search/RestMultiSearchActionV7Tests.java @@ -0,0 +1,64 @@ +/* + * 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.search; + +import org.elasticsearch.compat.FakeCompatRestRequestBuilder; +import org.elasticsearch.common.bytes.BytesArray; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.test.rest.RestActionTestCase; +import org.junit.Before; + +import java.nio.charset.StandardCharsets; + +public class RestMultiSearchActionV7Tests extends RestActionTestCase { + + @Before + public void setUpAction() { + controller().registerHandler(new RestMultiSearchActionV7(Settings.EMPTY)); + } + + public void testTypeInPath() { + String content = "{ \"index\": \"some_index\" } \n {} \n"; + BytesArray bytesContent = new BytesArray(content.getBytes(StandardCharsets.UTF_8)); + + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.GET) + .withPath("/some_index/some_type/_msearch") + .withContent(bytesContent, XContentType.JSON) + .build(); + + dispatchRequest(request); + assertWarnings(RestMultiSearchActionV7.TYPES_DEPRECATION_MESSAGE); + } + + public void testTypeInBody() { + String content = "{ \"index\": \"some_index\", \"type\": \"some_type\" } \n {} \n"; + BytesArray bytesContent = new BytesArray(content.getBytes(StandardCharsets.UTF_8)); + + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.POST) + .withPath("/some_index/_msearch") + .withContent(bytesContent, XContentType.JSON) + .build(); + + dispatchRequest(request); + assertWarnings(RestMultiSearchActionV7.TYPES_DEPRECATION_MESSAGE); + } +} diff --git a/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/search/RestSearchActionV7Tests.java b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/search/RestSearchActionV7Tests.java new file mode 100644 index 0000000000000..66396725ec63c --- /dev/null +++ b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/search/RestSearchActionV7Tests.java @@ -0,0 +1,58 @@ +/* + * 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.search; + +import org.elasticsearch.compat.FakeCompatRestRequestBuilder; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.test.rest.RestActionTestCase; +import org.junit.Before; + +import java.util.HashMap; +import java.util.Map; + +public class RestSearchActionV7Tests extends RestActionTestCase { + + @Before + public void setUpAction() { + controller().registerHandler(new RestSearchActionV7()); + } + + public void testTypeInPath() { + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.GET) + .withPath("/some_index/some_type/_search") + .build(); + + dispatchRequest(request); + assertWarnings(RestSearchActionV7.TYPES_DEPRECATION_MESSAGE); + } + + public void testTypeParameter() { + Map params = new HashMap<>(); + params.put("type", "some_type"); + + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.GET) + .withPath("/some_index/_search") + .withParams(params) + .build(); + + dispatchRequest(request); + assertWarnings(RestSearchActionV7.TYPES_DEPRECATION_MESSAGE); + } +} diff --git a/modules/rest-compatibility/src/test/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateActionV7Tests.java b/modules/rest-compatibility/src/test/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateActionV7Tests.java new file mode 100644 index 0000000000000..4b3439b2514b6 --- /dev/null +++ b/modules/rest-compatibility/src/test/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateActionV7Tests.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.script.mustache; + +import org.elasticsearch.compat.FakeCompatRestRequestBuilder; +import org.elasticsearch.common.bytes.BytesArray; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.test.rest.RestActionTestCase; +import org.junit.Before; + +import java.nio.charset.StandardCharsets; + +public class RestMultiSearchTemplateActionV7Tests extends RestActionTestCase { + + @Before + public void setUpAction() { + controller().registerHandler(new RestMultiSearchTemplateActionV7(Settings.EMPTY)); + } + + public void testTypeInPath() { + String content = "{ \"index\": \"some_index\" } \n" + "{\"source\": {\"query\" : {\"match_all\" :{}}}} \n"; + BytesArray bytesContent = new BytesArray(content.getBytes(StandardCharsets.UTF_8)); + + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.GET) + .withPath("/some_index/some_type/_msearch/template") + .withContent(bytesContent, XContentType.JSON) + .build(); + + dispatchRequest(request); + assertWarnings(RestMultiSearchTemplateActionV7.TYPES_DEPRECATION_MESSAGE); + } + + public void testTypeInBody() { + String content = "{ \"index\": \"some_index\", \"type\": \"some_type\" } \n" + "{\"source\": {\"query\" : {\"match_all\" :{}}}} \n"; + BytesArray bytesContent = new BytesArray(content.getBytes(StandardCharsets.UTF_8)); + + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withPath("/some_index/_msearch/template") + .withContent(bytesContent, XContentType.JSON) + .build(); + + dispatchRequest(request); + assertWarnings(RestMultiSearchTemplateActionV7.TYPES_DEPRECATION_MESSAGE); + } +} diff --git a/modules/rest-compatibility/src/test/java/org/elasticsearch/script/mustache/RestSearchTemplateActionV7Tests.java b/modules/rest-compatibility/src/test/java/org/elasticsearch/script/mustache/RestSearchTemplateActionV7Tests.java new file mode 100644 index 0000000000000..f22e159380be5 --- /dev/null +++ b/modules/rest-compatibility/src/test/java/org/elasticsearch/script/mustache/RestSearchTemplateActionV7Tests.java @@ -0,0 +1,58 @@ +/* + * 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.script.mustache; + +import org.elasticsearch.compat.FakeCompatRestRequestBuilder; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.action.search.RestSearchActionV7; +import org.elasticsearch.test.rest.RestActionTestCase; +import org.junit.Before; + +import java.util.HashMap; +import java.util.Map; + +public class RestSearchTemplateActionV7Tests extends RestActionTestCase { + + @Before + public void setUpAction() { + controller().registerHandler(new RestSearchTemplateActionV7()); + } + + public void testTypeInPath() { + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.GET) + .withPath("/some_index/some_type/_search/template") + .build(); + + dispatchRequest(request); + assertWarnings(RestSearchActionV7.TYPES_DEPRECATION_MESSAGE); + } + + public void testTypeParameter() { + Map params = new HashMap<>(); + params.put("type", "some_type"); + + RestRequest request = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.GET) + .withPath("/some_index/_search/template") + .withParams(params) + .build(); + + dispatchRequest(request); + assertWarnings(RestSearchActionV7.TYPES_DEPRECATION_MESSAGE); + } +} diff --git a/server/src/main/java/org/elasticsearch/action/search/MultiSearchRequest.java b/server/src/main/java/org/elasticsearch/action/search/MultiSearchRequest.java index 2fba92f5bab26..ce570731dc10f 100644 --- a/server/src/main/java/org/elasticsearch/action/search/MultiSearchRequest.java +++ b/server/src/main/java/org/elasticsearch/action/search/MultiSearchRequest.java @@ -43,6 +43,7 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; +import java.util.function.Function; import static org.elasticsearch.action.ValidateActions.addValidationError; import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue; @@ -177,7 +178,8 @@ public static void readMultiLineFormat(BytesReference data, String searchType, Boolean ccsMinimizeRoundtrips, NamedXContentRegistry registry, - boolean allowExplicitIndex) throws IOException { + boolean allowExplicitIndex, + Function typeConsumer) throws IOException { int from = 0; byte marker = xContent.streamSeparator(); while (true) { @@ -239,7 +241,9 @@ public static void readMultiLineFormat(BytesReference data, } else if ("ignore_throttled".equals(entry.getKey()) || "ignoreThrottled".equals(entry.getKey())) { ignoreThrottled = value; } else { - throw new IllegalArgumentException("key [" + entry.getKey() + "] is not supported in the metadata section"); + if(typeConsumer.apply(entry.getKey()) == false){ + throw new IllegalArgumentException("key [" + entry.getKey() + "] is not supported in the metadata section"); + } } } defaultOptions = IndicesOptions.fromParameters(expandWildcards, ignoreUnavailable, allowNoIndices, ignoreThrottled, diff --git a/server/src/main/java/org/elasticsearch/action/termvectors/MultiTermVectorsRequest.java b/server/src/main/java/org/elasticsearch/action/termvectors/MultiTermVectorsRequest.java index 056947484a0f5..16e558ec8d9aa 100644 --- a/server/src/main/java/org/elasticsearch/action/termvectors/MultiTermVectorsRequest.java +++ b/server/src/main/java/org/elasticsearch/action/termvectors/MultiTermVectorsRequest.java @@ -37,6 +37,7 @@ import java.util.Iterator; import java.util.List; import java.util.Set; +import java.util.function.Function; public class MultiTermVectorsRequest extends ActionRequest implements Iterable, CompositeIndicesRequest, RealtimeRequest { @@ -98,8 +99,11 @@ public boolean isEmpty() { public List getRequests() { return requests; } - public void add(TermVectorsRequest template, @Nullable XContentParser parser) throws IOException { + add(template, parser, k->false); + } + public void add(TermVectorsRequest template, @Nullable XContentParser parser, Function typeConsumer) + throws IOException { XContentParser.Token token; String currentFieldName = null; if (parser != null) { @@ -113,7 +117,7 @@ public void add(TermVectorsRequest template, @Nullable XContentParser parser) th throw new IllegalArgumentException("docs array element should include an object"); } TermVectorsRequest termVectorsRequest = new TermVectorsRequest(template); - TermVectorsRequest.parseRequest(termVectorsRequest, parser); + TermVectorsRequest.parseRequest(termVectorsRequest, parser, typeConsumer); add(termVectorsRequest); } } else if ("ids".equals(currentFieldName)) { diff --git a/server/src/main/java/org/elasticsearch/action/termvectors/TermVectorsRequest.java b/server/src/main/java/org/elasticsearch/action/termvectors/TermVectorsRequest.java index 09f7a15dff795..0bc7259206b91 100644 --- a/server/src/main/java/org/elasticsearch/action/termvectors/TermVectorsRequest.java +++ b/server/src/main/java/org/elasticsearch/action/termvectors/TermVectorsRequest.java @@ -48,6 +48,7 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Function; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @@ -528,11 +529,16 @@ public enum Flag { // the ordinal for encoding! Only append to the end! Positions, Offsets, Payloads, FieldStatistics, TermStatistics } + public static void parseRequest(TermVectorsRequest termVectorsRequest, XContentParser parser) throws IOException { + parseRequest(termVectorsRequest, parser, k -> false); + } /** * populates a request object (pre-populated with defaults) based on a parser. */ - public static void parseRequest(TermVectorsRequest termVectorsRequest, XContentParser parser) throws IOException { + public static void parseRequest(TermVectorsRequest termVectorsRequest, + XContentParser parser, + Function typeConsumer) throws IOException { XContentParser.Token token; String currentFieldName = null; List fields = new ArrayList<>(); @@ -585,7 +591,7 @@ public static void parseRequest(TermVectorsRequest termVectorsRequest, XContentP termVectorsRequest.version = parser.longValue(); } else if (VERSION_TYPE.match(currentFieldName, parser.getDeprecationHandler())) { termVectorsRequest.versionType = VersionType.fromString(parser.text()); - } else { + } else if(typeConsumer.apply(currentFieldName) == false) { throw new ElasticsearchParseException("failed to parse term vectors request. unknown field [{}]", currentFieldName); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java b/server/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java index ee6cba31d26f1..19a32213f213c 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java @@ -42,6 +42,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.function.Function; import static org.elasticsearch.rest.RestRequest.Method.GET; import static org.elasticsearch.rest.RestRequest.Method.POST; @@ -57,7 +58,7 @@ public class RestMultiSearchAction extends BaseRestHandler { RESPONSE_PARAMS = Collections.unmodifiableSet(responseParams); } - private final boolean allowExplicitIndex; + protected final boolean allowExplicitIndex; public RestMultiSearchAction(Settings settings) { this.allowExplicitIndex = MULTI_ALLOW_EXPLICIT_INDEX.get(settings); @@ -83,10 +84,21 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC return channel -> client.multiSearch(multiSearchRequest, new RestToXContentListener<>(channel)); } + public static MultiSearchRequest parseRequest(RestRequest restRequest, boolean allowExplicitIndex) throws IOException { + return parseRequest(restRequest,allowExplicitIndex, key->false); + } + /** * Parses a {@link RestRequest} body and returns a {@link MultiSearchRequest} + * + * @param typeConsumer - is a function used when parsing a request body. if it contains a types field it will consume it, + * allowing the same parsing logic to work in v7 and v8. + * It takes a string - field name, returns a boolean - if the field was "type or types" */ - public static MultiSearchRequest parseRequest(RestRequest restRequest, boolean allowExplicitIndex) throws IOException { + public static MultiSearchRequest parseRequest(RestRequest restRequest, + boolean allowExplicitIndex, + Function typeConsumer + /*TODO rename to unexpected field consumer?*/) throws IOException { MultiSearchRequest multiRequest = new MultiSearchRequest(); IndicesOptions indicesOptions = IndicesOptions.fromRequest(restRequest, multiRequest.indicesOptions()); multiRequest.indicesOptions(indicesOptions); @@ -112,7 +124,7 @@ public static MultiSearchRequest parseRequest(RestRequest restRequest, boolean a searchRequest.source(SearchSourceBuilder.fromXContent(parser, false)); RestSearchAction.checkRestTotalHits(restRequest, searchRequest); multiRequest.add(searchRequest); - }); + }, typeConsumer); List requests = multiRequest.requests(); for (SearchRequest request : requests) { // preserve if it's set on the request @@ -129,8 +141,11 @@ public static MultiSearchRequest parseRequest(RestRequest restRequest, boolean a /** * Parses a multi-line {@link RestRequest} body, instantiating a {@link SearchRequest} for each line and applying the given consumer. */ - public static void parseMultiLineRequest(RestRequest request, IndicesOptions indicesOptions, boolean allowExplicitIndex, - CheckedBiConsumer consumer) throws IOException { + public static void parseMultiLineRequest(RestRequest request, + IndicesOptions indicesOptions, + boolean allowExplicitIndex, + CheckedBiConsumer consumer, + Function typeConsumer) throws IOException { String[] indices = Strings.splitStringByCommaToArray(request.param("index")); String searchType = request.param("search_type"); @@ -141,7 +156,7 @@ public static void parseMultiLineRequest(RestRequest request, IndicesOptions ind final XContent xContent = sourceTuple.v1().xContent(); final BytesReference data = sourceTuple.v2(); MultiSearchRequest.readMultiLineFormat(data, xContent, consumer, indices, indicesOptions, routing, - searchType, ccsMinimizeRoundtrips, request.getXContentRegistry(), allowExplicitIndex); + searchType, ccsMinimizeRoundtrips, request.getXContentRegistry(), allowExplicitIndex, typeConsumer); } @Override diff --git a/server/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java b/server/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java index 3cbf7e4ceea6f..716aa5ba9f410 100644 --- a/server/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java @@ -229,7 +229,7 @@ private MultiSearchRequest parseMultiSearchRequest(String sample) throws IOExcep (searchRequest, parser) -> { searchRequest.source(SearchSourceBuilder.fromXContent(parser, false)); request.add(searchRequest); - }); + }, k->false); return request; } @@ -256,7 +256,8 @@ public void testMultiLineSerialization() throws IOException { parsedRequest.add(r); }; MultiSearchRequest.readMultiLineFormat(new BytesArray(originalBytes), xContentType.xContent(), - consumer, null, null, null, null, null, xContentRegistry(), true); + consumer, null, null, null, null, null, xContentRegistry(), true, + key -> false); assertEquals(originalRequest, parsedRequest); } } diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/FakeRestRequest.java b/test/framework/src/main/java/org/elasticsearch/test/rest/FakeRestRequest.java index fc99063198c87..62381877164d4 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/rest/FakeRestRequest.java +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/FakeRestRequest.java @@ -196,6 +196,11 @@ public Builder(NamedXContentRegistry xContentRegistry) { this.xContentRegistry = xContentRegistry; } + public Builder addHeaders(Map> headers) { + this.headers.putAll(headers); + return this; + } + public Builder withHeaders(Map> headers) { this.headers.putAll(headers); return this;