Skip to content

Commit 76135bf

Browse files
authored
[Rest Api Compatibility] Allow first empty line for msearch (#75886)
The support for this lenient behaviour was removed in #41011 however since this is a change that affects a request shape it should still be available to use it in rest api compatibility relates #51816
1 parent 3e02b86 commit 76135bf

File tree

4 files changed

+99
-4
lines changed

4 files changed

+99
-4
lines changed

server/src/main/java/org/elasticsearch/action/search/MultiSearchRequest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public class MultiSearchRequest extends ActionRequest implements CompositeIndice
5353
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestSearchAction.class);
5454
public static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" +
5555
" Specifying types in search requests is deprecated.";
56-
56+
public static final String FIRST_LINE_EMPTY_DEPRECATION_MESSAGE =
57+
"support for empty first line before any action metadata in msearch API is deprecated " +
58+
"and will be removed in the next major version";
5759
public static final int MAX_CONCURRENT_SEARCH_REQUESTS_DEFAULT = 0;
5860

5961
private int maxConcurrentSearchRequests = 0;
@@ -185,6 +187,13 @@ public static void readMultiLineFormat(BytesReference data,
185187
if (nextMarker == -1) {
186188
break;
187189
}
190+
// support first line with \n
191+
if (restApiVersion == RestApiVersion.V_7 && nextMarker == 0) {
192+
deprecationLogger.compatibleApiWarning("msearch_first_line_empty", FIRST_LINE_EMPTY_DEPRECATION_MESSAGE);
193+
from = nextMarker + 1;
194+
continue;
195+
}
196+
188197
SearchRequest searchRequest = new SearchRequest();
189198
if (indices != null) {
190199
searchRequest.indices(indices);

server/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,15 @@ public void testMsearchTerminatedByNewline() throws Exception {
209209
assertEquals(3, msearchRequest.requests().size());
210210
}
211211

212+
private MultiSearchRequest parseMultiSearchRequestFromString(String request, RestApiVersion restApiVersion) throws IOException {
213+
return parseMultiSearchRequest(createRestRequest(request.getBytes(StandardCharsets.UTF_8), restApiVersion));
214+
}
215+
212216
private MultiSearchRequest parseMultiSearchRequest(String sample) throws IOException {
213-
byte[] data = StreamsUtils.copyToBytesFromClasspath(sample);
214-
RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry())
215-
.withContent(new BytesArray(data), XContentType.JSON).build();
217+
return parseMultiSearchRequest(createRestRequest(sample, null));
218+
}
219+
220+
private MultiSearchRequest parseMultiSearchRequest(RestRequest restRequest) throws IOException {
216221

217222
MultiSearchRequest request = new MultiSearchRequest();
218223
RestMultiSearchAction.parseMultiLineRequest(restRequest, SearchRequest.DEFAULT_INDICES_OPTIONS, true,
@@ -223,6 +228,26 @@ private MultiSearchRequest parseMultiSearchRequest(String sample) throws IOExcep
223228
return request;
224229
}
225230

231+
private RestRequest createRestRequest(String sample, RestApiVersion restApiVersion) throws IOException {
232+
byte[] data = StreamsUtils.copyToBytesFromClasspath(sample);
233+
return createRestRequest(data, restApiVersion);
234+
}
235+
236+
private FakeRestRequest createRestRequest(byte[] data, RestApiVersion restApiVersion) {
237+
if (restApiVersion != null) {
238+
final List<String> contentTypeHeader =
239+
Collections.singletonList(compatibleMediaType(XContentType.VND_JSON, RestApiVersion.V_7));
240+
return new FakeRestRequest.Builder(xContentRegistry())
241+
.withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader))
242+
.withContent(new BytesArray(data), null)
243+
.build();
244+
} else {
245+
return new FakeRestRequest.Builder(xContentRegistry())
246+
.withContent(new BytesArray(data), XContentType.JSON).build();
247+
}
248+
}
249+
250+
226251
@Override
227252
protected NamedXContentRegistry xContentRegistry() {
228253
return new NamedXContentRegistry(singletonList(new NamedXContentRegistry.Entry(QueryBuilder.class,
@@ -271,6 +296,48 @@ public void testWritingExpandWildcards() throws IOException {
271296
randomBoolean(), randomBoolean(), randomBoolean()), "none");
272297
}
273298

299+
public void testEmptyFirstLine1() throws Exception {
300+
MultiSearchRequest request = parseMultiSearchRequestFromString(
301+
"\n" +
302+
"\n" +
303+
"{ \"query\": {\"match_all\": {}}}\n" +
304+
"{}\n" +
305+
"{ \"query\": {\"match_all\": {}}}\n" +
306+
"\n" +
307+
"{ \"query\": {\"match_all\": {}}}\n" +
308+
"{}\n" +
309+
"{ \"query\": {\"match_all\": {}}}\n",
310+
RestApiVersion.V_7);
311+
assertThat(request.requests().size(), equalTo(4));
312+
for (SearchRequest searchRequest : request.requests()) {
313+
assertThat(searchRequest.indices().length, equalTo(0));
314+
assertThat(searchRequest.source().query(), instanceOf(MatchAllQueryBuilder.class));
315+
}
316+
assertWarnings("support for empty first line before any action metadata in msearch API is deprecated and will be removed " +
317+
"in the next major version");
318+
}
319+
320+
public void testEmptyFirstLine2() throws Exception {
321+
MultiSearchRequest request = parseMultiSearchRequestFromString(
322+
"\n" +
323+
"{}\n" +
324+
"{ \"query\": {\"match_all\": {}}}\n" +
325+
"\n" +
326+
"{ \"query\": {\"match_all\": {}}}\n" +
327+
"{}\n" +
328+
"{ \"query\": {\"match_all\": {}}}\n" +
329+
"\n" +
330+
"{ \"query\": {\"match_all\": {}}}\n",
331+
RestApiVersion.V_7);
332+
assertThat(request.requests().size(), equalTo(4));
333+
for (SearchRequest searchRequest : request.requests()) {
334+
assertThat(searchRequest.indices().length, equalTo(0));
335+
assertThat(searchRequest.source().query(), instanceOf(MatchAllQueryBuilder.class));
336+
}
337+
assertWarnings("support for empty first line before any action metadata in msearch API is deprecated and will be removed " +
338+
"in the next major version");
339+
}
340+
274341
private void assertExpandWildcardsValue(IndicesOptions options, String expectedValue) throws IOException {
275342
SearchRequest request = new SearchRequest();
276343
request.indicesOptions(options);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
{ "query": {"match_all": {}}}
4+
{}
5+
{ "query": {"match_all": {}}}
6+
7+
{ "query": {"match_all": {}}}
8+
{}
9+
{ "query": {"match_all": {}}}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
3+
{}
4+
{ "query": {"match_all": {}}}
5+
6+
{ "query": {"match_all": {}}}
7+
{}
8+
{ "query": {"match_all": {}}}
9+
10+
{ "query": {"match_all": {}}}

0 commit comments

Comments
 (0)