Skip to content

Commit 25c416b

Browse files
authored
Deprecate types in search and multi search templates. (#35669)
This PR adds deprecation warnings to the relevant `Rest*Action` classes, plus tests in `Rest*ActionTests`. No updates to REST tests, the Java HLRC, or documentation were necessary, since they didn't make use of types.
1 parent 7e772e3 commit 25c416b

File tree

6 files changed

+193
-16
lines changed

6 files changed

+193
-16
lines changed

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

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

2020
package org.elasticsearch.script.mustache;
2121

22+
import org.apache.logging.log4j.LogManager;
2223
import org.elasticsearch.client.node.NodeClient;
24+
import org.elasticsearch.common.logging.DeprecationLogger;
2325
import org.elasticsearch.common.settings.Settings;
2426
import org.elasticsearch.rest.BaseRestHandler;
2527
import org.elasticsearch.rest.RestController;
@@ -36,7 +38,8 @@
3638
import static org.elasticsearch.rest.RestRequest.Method.POST;
3739

3840
public class RestMultiSearchTemplateAction extends BaseRestHandler {
39-
41+
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(
42+
LogManager.getLogger(RestMultiSearchAction.class));
4043
private static final Set<String> RESPONSE_PARAMS = Collections.singleton(RestSearchAction.TYPED_KEYS_PARAM);
4144

4245
private final boolean allowExplicitIndex;
@@ -75,6 +78,9 @@ public static MultiSearchTemplateRequest parseRequest(RestRequest restRequest, b
7578

7679
RestMultiSearchAction.parseMultiLineRequest(restRequest, multiRequest.indicesOptions(), allowExplicitIndex,
7780
(searchRequest, bytes) -> {
81+
if (searchRequest.types().length > 0) {
82+
deprecationLogger.deprecated(RestMultiSearchAction.TYPES_DEPRECATION_MESSAGE);
83+
}
7884
SearchTemplateRequest searchTemplateRequest = SearchTemplateRequest.fromXContent(bytes);
7985
if (searchTemplateRequest.getScript() != null) {
8086
searchTemplateRequest.setRequest(searchRequest);

modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateRequestTests.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.common.bytes.BytesArray;
2424
import org.elasticsearch.common.xcontent.XContentType;
2525
import org.elasticsearch.rest.RestRequest;
26+
import org.elasticsearch.rest.action.search.RestMultiSearchAction;
2627
import org.elasticsearch.script.ScriptType;
2728
import org.elasticsearch.search.Scroll;
2829
import org.elasticsearch.search.builder.SearchSourceBuilder;
@@ -77,6 +78,8 @@ public void testParseRequest() throws Exception {
7778
assertEquals(1, request.requests().get(0).getScriptParams().size());
7879
assertEquals(1, request.requests().get(1).getScriptParams().size());
7980
assertEquals(1, request.requests().get(2).getScriptParams().size());
81+
82+
assertWarnings(RestMultiSearchAction.TYPES_DEPRECATION_MESSAGE);
8083
}
8184

8285
public void testParseWithCarriageReturn() throws Exception {
@@ -105,10 +108,10 @@ public void testMaxConcurrentSearchRequests() {
105108
expectThrows(IllegalArgumentException.class, () ->
106109
request.maxConcurrentSearchRequests(randomIntBetween(Integer.MIN_VALUE, 0)));
107110
}
108-
111+
109112
public void testMultiSearchTemplateToJson() throws Exception {
110113
final int numSearchRequests = randomIntBetween(1, 10);
111-
MultiSearchTemplateRequest multiSearchTemplateRequest = new MultiSearchTemplateRequest();
114+
MultiSearchTemplateRequest multiSearchTemplateRequest = new MultiSearchTemplateRequest();
112115
for (int i = 0; i < numSearchRequests; i++) {
113116
// Create a random request.
114117
String[] indices = {"test"};
@@ -118,25 +121,25 @@ public void testMultiSearchTemplateToJson() throws Exception {
118121
// batched reduce size is currently not set-able on a per-request basis as it is a query string parameter only
119122
searchRequest.setBatchedReduceSize(SearchRequest.DEFAULT_BATCHED_REDUCE_SIZE);
120123
SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest(searchRequest);
121-
124+
122125
searchTemplateRequest.setScript("{\"query\": { \"match\" : { \"{{field}}\" : \"{{value}}\" }}}");
123126
searchTemplateRequest.setScriptType(ScriptType.INLINE);
124127
searchTemplateRequest.setProfile(randomBoolean());
125-
128+
126129
Map<String, Object> scriptParams = new HashMap<>();
127130
scriptParams.put("field", "name");
128131
scriptParams.put("value", randomAlphaOfLengthBetween(2, 5));
129132
searchTemplateRequest.setScriptParams(scriptParams);
130-
131-
multiSearchTemplateRequest.add(searchTemplateRequest);
133+
134+
multiSearchTemplateRequest.add(searchTemplateRequest);
132135
}
133136

134137
//Serialize the request
135138
String serialized = toJsonString(multiSearchTemplateRequest);
136-
139+
137140
//Deserialize the request
138141
RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry())
139-
.withContent(new BytesArray(serialized), XContentType.JSON).build();
142+
.withContent(new BytesArray(serialized), XContentType.JSON).build();
140143
MultiSearchTemplateRequest deser = RestMultiSearchTemplateAction.parseRequest(restRequest, true);
141144

142145
// For object equality purposes need to set the search requests' source to non-null
@@ -145,17 +148,17 @@ public void testMultiSearchTemplateToJson() throws Exception {
145148
if (sr.source() == null) {
146149
sr.source(new SearchSourceBuilder());
147150
}
148-
}
151+
}
149152
// Compare the deserialized request object with the original request object
150153
assertEquals(multiSearchTemplateRequest, deser);
151-
154+
152155
// Finally, serialize the deserialized request to compare JSON equivalence (in case Object.equals() fails to reveal a discrepancy)
153156
assertEquals(serialized, toJsonString(deser));
154157
}
155158

156159
protected String toJsonString(MultiSearchTemplateRequest multiSearchTemplateRequest) throws IOException {
157160
byte[] bytes = MultiSearchTemplateRequest.writeMultiLineFormat(multiSearchTemplateRequest, XContentType.JSON.xContent());
158161
return new String(bytes, StandardCharsets.UTF_8);
159-
}
162+
}
160163

161164
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
package org.elasticsearch.script.mustache;
20+
21+
import org.elasticsearch.client.node.NodeClient;
22+
import org.elasticsearch.common.bytes.BytesArray;
23+
import org.elasticsearch.common.settings.Settings;
24+
import org.elasticsearch.common.util.concurrent.ThreadContext;
25+
import org.elasticsearch.common.xcontent.XContentType;
26+
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
27+
import org.elasticsearch.rest.RestChannel;
28+
import org.elasticsearch.rest.RestController;
29+
import org.elasticsearch.rest.RestRequest;
30+
import org.elasticsearch.rest.action.search.RestMultiSearchAction;
31+
import org.elasticsearch.test.ESTestCase;
32+
import org.elasticsearch.test.rest.FakeRestChannel;
33+
import org.elasticsearch.test.rest.FakeRestRequest;
34+
import org.elasticsearch.usage.UsageService;
35+
36+
import java.nio.charset.StandardCharsets;
37+
import java.util.Collections;
38+
39+
import static org.mockito.Mockito.mock;
40+
41+
public class RestMultiSearchTemplateActionTests extends ESTestCase {
42+
private RestController controller;
43+
44+
public void setUp() throws Exception {
45+
super.setUp();
46+
controller = new RestController(Collections.emptySet(), null,
47+
mock(NodeClient.class),
48+
new NoneCircuitBreakerService(),
49+
new UsageService());
50+
new RestMultiSearchTemplateAction(Settings.EMPTY, controller);
51+
}
52+
53+
public void testTypeInPath() {
54+
String content = "{ \"index\": \"some_index\" } \n" +
55+
"{\"source\": {\"query\" : {\"match_all\" :{}}}} \n";
56+
BytesArray bytesContent = new BytesArray(content.getBytes(StandardCharsets.UTF_8));
57+
58+
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
59+
.withMethod(RestRequest.Method.GET)
60+
.withPath("/some_index/some_type/_msearch/template")
61+
.withContent(bytesContent, XContentType.JSON)
62+
.build();
63+
64+
performRequest(request);
65+
assertWarnings(RestMultiSearchAction.TYPES_DEPRECATION_MESSAGE);
66+
}
67+
68+
public void testTypeInBody() {
69+
String content = "{ \"index\": \"some_index\", \"type\": \"some_type\" } \n" +
70+
"{\"source\": {\"query\" : {\"match_all\" :{}}}} \n";
71+
BytesArray bytesContent = new BytesArray(content.getBytes(StandardCharsets.UTF_8));
72+
73+
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
74+
.withPath("/some_index/_msearch/template")
75+
.withContent(bytesContent, XContentType.JSON)
76+
.build();
77+
78+
performRequest(request);
79+
assertWarnings(RestMultiSearchAction.TYPES_DEPRECATION_MESSAGE);
80+
}
81+
82+
private void performRequest(RestRequest request) {
83+
RestChannel channel = new FakeRestChannel(request, false, 1);
84+
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
85+
controller.dispatchRequest(request, channel, threadContext);
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
package org.elasticsearch.script.mustache;
20+
21+
import org.elasticsearch.client.node.NodeClient;
22+
import org.elasticsearch.common.settings.Settings;
23+
import org.elasticsearch.common.util.concurrent.ThreadContext;
24+
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
25+
import org.elasticsearch.rest.RestChannel;
26+
import org.elasticsearch.rest.RestController;
27+
import org.elasticsearch.rest.RestRequest;
28+
import org.elasticsearch.rest.action.search.RestSearchAction;
29+
import org.elasticsearch.test.ESTestCase;
30+
import org.elasticsearch.test.rest.FakeRestChannel;
31+
import org.elasticsearch.test.rest.FakeRestRequest;
32+
import org.elasticsearch.usage.UsageService;
33+
34+
import java.util.Collections;
35+
import java.util.HashMap;
36+
import java.util.Map;
37+
38+
import static org.mockito.Mockito.mock;
39+
40+
public class RestSearchTemplateActionTests extends ESTestCase {
41+
private RestController controller;
42+
43+
public void setUp() throws Exception {
44+
super.setUp();
45+
controller = new RestController(Collections.emptySet(), null,
46+
mock(NodeClient.class),
47+
new NoneCircuitBreakerService(),
48+
new UsageService());
49+
new RestSearchTemplateAction(Settings.EMPTY, controller);
50+
}
51+
52+
public void testTypeInPath() {
53+
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
54+
.withMethod(RestRequest.Method.GET)
55+
.withPath("/some_index/some_type/_search/template")
56+
.build();
57+
58+
performRequest(request);
59+
assertWarnings(RestSearchAction.TYPES_DEPRECATION_MESSAGE);
60+
}
61+
62+
public void testTypeParameter() {
63+
Map<String, String> params = new HashMap<>();
64+
params.put("type", "some_type");
65+
66+
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
67+
.withMethod(RestRequest.Method.GET)
68+
.withPath("/some_index/_search/template")
69+
.withParams(params)
70+
.build();
71+
72+
performRequest(request);
73+
assertWarnings(RestSearchAction.TYPES_DEPRECATION_MESSAGE);
74+
}
75+
76+
private void performRequest(RestRequest request) {
77+
RestChannel channel = new FakeRestChannel(request, false, 1);
78+
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
79+
controller.dispatchRequest(request, channel, threadContext);
80+
}
81+
}

server/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@
4848
import static org.elasticsearch.rest.RestRequest.Method.POST;
4949

5050
public class RestMultiSearchAction extends BaseRestHandler {
51-
private static final Set<String> RESPONSE_PARAMS = Collections.singleton(RestSearchAction.TYPED_KEYS_PARAM);
52-
5351
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(
5452
LogManager.getLogger(RestMultiSearchAction.class));
55-
static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" +
53+
public static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" +
5654
" Specifying types in multi search requests is deprecated.";
5755

56+
private static final Set<String> RESPONSE_PARAMS = Collections.singleton(RestSearchAction.TYPED_KEYS_PARAM);
57+
5858
private final boolean allowExplicitIndex;
5959

6060
public RestMultiSearchAction(Settings settings, RestController controller) {

server/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class RestSearchAction extends BaseRestHandler {
5858
private static final Set<String> RESPONSE_PARAMS = Collections.singleton(TYPED_KEYS_PARAM);
5959

6060
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestSearchAction.class));
61-
static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" +
61+
public static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" +
6262
" Specifying types in search requests is deprecated.";
6363

6464
public RestSearchAction(Settings settings, RestController controller) {

0 commit comments

Comments
 (0)