Skip to content

Commit 2df6157

Browse files
authored
Compatible logic for Removes typed endpoint from search and related APIs (#54572)
This adds a compatible APIs for typed endpoints removed in #41640 202 failing tests These 2 tests are explicitly fixed by this PR CompatRestIT. test {yaml=mtermvectors/21_deprecated_with_types/Deprecated camel case and _ parameters should fail in Term Vectors query} CompatRestIT. test {yaml=mtermvectors/30_mix_typeless_typeful/mtermvectors without types on an index that has types} I think more yml tests should be added to 7.x to cover these endpoints 17th june 1306tests | 197failures | 16ignored | 10m56.91sduration
1 parent c21f493 commit 2df6157

File tree

34 files changed

+1361
-78
lines changed

34 files changed

+1361
-78
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ public void testMultiSearch() throws IOException {
12231223
};
12241224
MultiSearchRequest.readMultiLineFormat(new BytesArray(EntityUtils.toByteArray(request.getEntity())),
12251225
REQUEST_BODY_CONTENT_TYPE.xContent(), consumer, null, multiSearchRequest.indicesOptions(), null, null, null,
1226-
xContentRegistry(), true);
1226+
xContentRegistry(), true, key -> false);
12271227
assertEquals(requests, multiSearchRequest.requests());
12281228
}
12291229

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateAction.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.HashSet;
3333
import java.util.List;
3434
import java.util.Set;
35+
import java.util.function.Function;
3536

3637
import static java.util.Arrays.asList;
3738
import static org.elasticsearch.rest.RestRequest.Method.GET;
@@ -49,7 +50,7 @@ public class RestMultiSearchTemplateAction extends BaseRestHandler {
4950
}
5051

5152

52-
private final boolean allowExplicitIndex;
53+
protected final boolean allowExplicitIndex;
5354

5455
public RestMultiSearchTemplateAction(Settings settings) {
5556
this.allowExplicitIndex = MULTI_ALLOW_EXPLICIT_INDEX.get(settings);
@@ -79,6 +80,19 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client
7980
* Parses a {@link RestRequest} body and returns a {@link MultiSearchTemplateRequest}
8081
*/
8182
public static MultiSearchTemplateRequest parseRequest(RestRequest restRequest, boolean allowExplicitIndex) throws IOException {
83+
return parseRequest(restRequest,allowExplicitIndex, k->false);
84+
}
85+
86+
/**
87+
* Parses a {@link RestRequest} body and returns a {@link MultiSearchTemplateRequest}
88+
* @param typeConsumer - A function used to validate if a provided xContent key is allowed.
89+
* This is useful for xContent compatibility to determine
90+
* if a key is allowed to be present in version agnostic manner.
91+
* The provided function should return false if the key is not allowed.
92+
*/
93+
public static MultiSearchTemplateRequest parseRequest(RestRequest restRequest,
94+
boolean allowExplicitIndex,
95+
Function<String,Boolean> typeConsumer) throws IOException {
8296
MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest();
8397
if (restRequest.hasParam("max_concurrent_searches")) {
8498
multiRequest.maxConcurrentSearchRequests(restRequest.paramAsInt("max_concurrent_searches", 0));
@@ -94,7 +108,7 @@ public static MultiSearchTemplateRequest parseRequest(RestRequest restRequest, b
94108
throw new IllegalArgumentException("Malformed search template");
95109
}
96110
RestSearchAction.checkRestTotalHits(restRequest, searchRequest);
97-
});
111+
}, typeConsumer);
98112
return multiRequest;
99113
}
100114

modules/rest-compatibility/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919

2020
esplugin {
2121
description 'Adds a compatiblity layer for the prior major versions REST API'
22-
classname 'org.elasticsearch.rest.compat.RestCompatPlugin'
22+
classname 'org.elasticsearch.compat.RestCompatPlugin'
23+
}
24+
25+
dependencies {
26+
implementation project(':modules:lang-mustache')
27+
implementation project(':modules:reindex')
2328
}
2429

2530
integTest.enabled = false
Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* not use this file except in compliance with the License.
88
* You may obtain a copy of the License at
99
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
10+
* http://www.apache.org/licenses/LICENSE-2.0
1111
*
1212
* Unless required by applicable law or agreed to in writing,
1313
* software distributed under the License is distributed on an
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
package org.elasticsearch.rest.compat;
20+
package org.elasticsearch.compat;
2121

2222
import org.elasticsearch.Version;
2323
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
@@ -26,13 +26,21 @@
2626
import org.elasticsearch.common.settings.IndexScopedSettings;
2727
import org.elasticsearch.common.settings.Settings;
2828
import org.elasticsearch.common.settings.SettingsFilter;
29+
import org.elasticsearch.index.reindex.RestDeleteByQueryActionV7;
30+
import org.elasticsearch.index.reindex.RestUpdateByQueryActionV7;
2931
import org.elasticsearch.plugins.ActionPlugin;
3032
import org.elasticsearch.plugins.Plugin;
3133
import org.elasticsearch.rest.RestController;
3234
import org.elasticsearch.rest.RestHandler;
33-
import org.elasticsearch.rest.compat.version7.RestCreateIndexActionV7;
34-
import org.elasticsearch.rest.compat.version7.RestGetActionV7;
35-
import org.elasticsearch.rest.compat.version7.RestIndexActionV7;
35+
import org.elasticsearch.rest.action.admin.indices.RestCreateIndexActionV7;
36+
import org.elasticsearch.rest.action.document.RestGetActionV7;
37+
import org.elasticsearch.rest.action.document.RestIndexActionV7;
38+
import org.elasticsearch.rest.action.document.RestMultiTermVectorsActionV7;
39+
import org.elasticsearch.rest.action.document.RestTermVectorsActionV7;
40+
import org.elasticsearch.rest.action.search.RestMultiSearchActionV7;
41+
import org.elasticsearch.rest.action.search.RestSearchActionV7;
42+
import org.elasticsearch.script.mustache.RestMultiSearchTemplateActionV7;
43+
import org.elasticsearch.script.mustache.RestSearchTemplateActionV7;
3644

3745
import java.util.Collections;
3846
import java.util.List;
@@ -52,11 +60,19 @@ public List<RestHandler> getRestHandlers(
5260
) {
5361
if (Version.CURRENT.major == 8) {
5462
return List.of(
63+
new RestDeleteByQueryActionV7(),
64+
new RestUpdateByQueryActionV7(),
65+
new RestCreateIndexActionV7(),
5566
new RestGetActionV7(),
5667
new RestIndexActionV7.CompatibleRestIndexAction(),
5768
new RestIndexActionV7.CompatibleCreateHandler(),
5869
new RestIndexActionV7.CompatibleAutoIdHandler(nodesInCluster),
59-
new RestCreateIndexActionV7()
70+
new RestTermVectorsActionV7(),
71+
new RestMultiTermVectorsActionV7(),
72+
new RestSearchActionV7(),
73+
new RestMultiSearchActionV7(settings),
74+
new RestSearchTemplateActionV7(),
75+
new RestMultiSearchTemplateActionV7(settings)
6076
);
6177
}
6278
return Collections.emptyList();
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.compat;
21+
22+
import org.elasticsearch.common.Strings;
23+
import org.elasticsearch.rest.RestRequest;
24+
25+
import java.util.Set;
26+
import java.util.function.Function;
27+
28+
public class TypeConsumer implements Function<String, Boolean> {
29+
30+
private final RestRequest request;
31+
private final Set<String> fieldNames;
32+
private boolean foundTypeInBody = false;
33+
34+
public TypeConsumer(RestRequest request, String... fieldNames) {
35+
this.request = request;
36+
this.fieldNames = Set.of(fieldNames);
37+
}
38+
39+
@Override
40+
public Boolean apply(String fieldName) {
41+
if (fieldNames.contains(fieldName)) {
42+
foundTypeInBody = true;
43+
return true;
44+
}
45+
return false;
46+
}
47+
48+
public boolean hasTypes() {
49+
// TODO can params be types too? or _types?
50+
String[] types = Strings.splitStringByCommaToArray(request.param("type"));
51+
return types.length > 0 || foundTypeInBody;
52+
}
53+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.reindex;
21+
22+
import org.elasticsearch.Version;
23+
import org.elasticsearch.client.node.NodeClient;
24+
import org.elasticsearch.common.logging.DeprecationLogger;
25+
import org.elasticsearch.rest.RestRequest;
26+
import org.elasticsearch.rest.action.search.RestSearchActionV7;
27+
28+
import java.io.IOException;
29+
import java.util.List;
30+
31+
import static org.elasticsearch.rest.RestRequest.Method.POST;
32+
33+
public class RestDeleteByQueryActionV7 extends RestDeleteByQueryAction {
34+
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestDeleteByQueryActionV7.class);
35+
36+
@Override
37+
public List<Route> routes() {
38+
return List.of(new Route(POST, "/{index}/{type}/_delete_by_query"));
39+
}
40+
41+
@Override
42+
public String getName() {
43+
return super.getName() + "_v7";
44+
}
45+
46+
@Override
47+
public Version compatibleWithVersion() {
48+
return Version.V_7_0_0;
49+
}
50+
51+
@Override
52+
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
53+
if (request.hasParam("type")) {
54+
deprecationLogger.deprecate("search_with_types", RestSearchActionV7.TYPES_DEPRECATION_MESSAGE);
55+
request.param("type");
56+
}
57+
return super.prepareRequest(request, client);
58+
}
59+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.reindex;
21+
22+
import org.elasticsearch.Version;
23+
import org.elasticsearch.client.node.NodeClient;
24+
import org.elasticsearch.common.logging.DeprecationLogger;
25+
import org.elasticsearch.rest.RestRequest;
26+
import org.elasticsearch.rest.action.search.RestSearchActionV7;
27+
28+
import java.io.IOException;
29+
import java.util.List;
30+
31+
import static org.elasticsearch.rest.RestRequest.Method.POST;
32+
33+
public class RestUpdateByQueryActionV7 extends RestUpdateByQueryAction {
34+
35+
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestUpdateByQueryActionV7.class);
36+
37+
@Override
38+
public List<Route> routes() {
39+
return List.of(new Route(POST, "/{index}/{type}/_update_by_query"));
40+
}
41+
42+
@Override
43+
public String getName() {
44+
return super.getName() + "_v7";
45+
}
46+
47+
@Override
48+
public Version compatibleWithVersion() {
49+
return Version.V_7_0_0;
50+
}
51+
52+
@Override
53+
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
54+
if (request.hasParam("type")) {
55+
deprecationLogger.deprecate("search_with_types", RestSearchActionV7.TYPES_DEPRECATION_MESSAGE);
56+
request.param("type");
57+
}
58+
return super.prepareRequest(request, client);
59+
}
60+
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* not use this file except in compliance with the License.
88
* You may obtain a copy of the License at
99
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
10+
* http://www.apache.org/licenses/LICENSE-2.0
1111
*
1212
* Unless required by applicable law or agreed to in writing,
1313
* software distributed under the License is distributed on an
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
package org.elasticsearch.rest.compat.version7;
20+
package org.elasticsearch.rest.action.admin.indices;
2121

2222
import org.elasticsearch.Version;
2323
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
@@ -28,7 +28,6 @@
2828
import org.elasticsearch.index.mapper.MapperService;
2929
import org.elasticsearch.rest.RestRequest;
3030
import org.elasticsearch.rest.action.RestToXContentListener;
31-
import org.elasticsearch.rest.action.admin.indices.RestCreateIndexAction;
3231

3332
import java.io.IOException;
3433
import java.util.Collections;
@@ -46,7 +45,7 @@ public class RestCreateIndexActionV7 extends RestCreateIndexAction {
4645

4746
@Override
4847
public String getName() {
49-
return "create_index_action_v7";
48+
return super.getName() + "_v7";
5049
}
5150

5251
@Override
@@ -56,19 +55,19 @@ public Version compatibleWithVersion() {
5655

5756
@Override
5857
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
59-
CreateIndexRequest createIndexRequest = prepareRequest(request);
58+
CreateIndexRequest createIndexRequest = prepareV7Request(request);
6059
return channel -> client.admin().indices().create(createIndexRequest, new RestToXContentListener<>(channel));
6160
}
6261

6362
// default scope for testing
64-
CreateIndexRequest prepareRequest(RestRequest request) {
63+
CreateIndexRequest prepareV7Request(RestRequest request) {
6564
CreateIndexRequest createIndexRequest = new CreateIndexRequest(request.param("index"));
6665

6766
if (request.hasContent()) {
6867
Map<String, Object> sourceAsMap = XContentHelper.convertToMap(request.requiredContent(), false, request.getXContentType()).v2();
6968

7069
request.param(INCLUDE_TYPE_NAME_PARAMETER);// just consume, it is always replaced with _doc
71-
sourceAsMap = prepareMappings(sourceAsMap, request);
70+
sourceAsMap = prepareMappingsV7(sourceAsMap, request);
7271

7372
createIndexRequest.source(sourceAsMap, LoggingDeprecationHandler.INSTANCE);
7473
}
@@ -79,7 +78,7 @@ CreateIndexRequest prepareRequest(RestRequest request) {
7978
return createIndexRequest;
8079
}
8180

82-
static Map<String, Object> prepareMappings(Map<String, Object> source, RestRequest request) {
81+
static Map<String, Object> prepareMappingsV7(Map<String, Object> source, RestRequest request) {
8382
final boolean includeTypeName = request.paramAsBoolean(INCLUDE_TYPE_NAME_PARAMETER, false);
8483

8584
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)