Skip to content

Commit

Permalink
[Rest Api Compatibility] Allow first empty line for msearch
Browse files Browse the repository at this point in the history
The support for this buggy behaviour was removed in elastic#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 elastic#51816
  • Loading branch information
pgomulka committed Jul 30, 2021
1 parent 98554b4 commit e9e1f35
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public class MultiSearchRequest extends ActionRequest implements CompositeIndice
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestSearchAction.class);
public static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" +
" Specifying types in search requests is deprecated.";

public static final String FIRST_LINE_EMPTY_DEPRECATION_MESSAGE =
"support for empty first line before any action metadata in msearch API is deprecated " +
"and will be removed in the next major version";
public static final int MAX_CONCURRENT_SEARCH_REQUESTS_DEFAULT = 0;

private int maxConcurrentSearchRequests = 0;
Expand Down Expand Up @@ -185,6 +187,13 @@ public static void readMultiLineFormat(BytesReference data,
if (nextMarker == -1) {
break;
}
// support first line with \n
if (restApiVersion == RestApiVersion.V_7 && nextMarker == 0) {
deprecationLogger.compatibleApiWarning("msearch_first_line_empty", FIRST_LINE_EMPTY_DEPRECATION_MESSAGE);
from = nextMarker + 1;
continue;
}

SearchRequest searchRequest = new SearchRequest();
if (indices != null) {
searchRequest.indices(indices);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,15 @@ public void testMsearchTerminatedByNewline() throws Exception {
assertEquals(3, msearchRequest.requests().size());
}

private MultiSearchRequest parseMultiSearchRequestFromString(String request, RestApiVersion restApiVersion) throws IOException {
return parseMultiSearchRequest(createRestRequest(request.getBytes(StandardCharsets.UTF_8), restApiVersion));
}

private MultiSearchRequest parseMultiSearchRequest(String sample) throws IOException {
byte[] data = StreamsUtils.copyToBytesFromClasspath(sample);
RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry())
.withContent(new BytesArray(data), XContentType.JSON).build();
return parseMultiSearchRequest(createRestRequest(sample, null));
}

private MultiSearchRequest parseMultiSearchRequest(RestRequest restRequest) throws IOException {

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

private RestRequest createRestRequest(String sample, RestApiVersion restApiVersion) throws IOException {
byte[] data = StreamsUtils.copyToBytesFromClasspath(sample);
return createRestRequest(data, restApiVersion);
}

private FakeRestRequest createRestRequest(byte[] data, RestApiVersion restApiVersion) {
if (restApiVersion != null) {
final List<String> contentTypeHeader =
Collections.singletonList(compatibleMediaType(XContentType.VND_JSON, RestApiVersion.V_7));
return new FakeRestRequest.Builder(xContentRegistry())
.withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader))
.withContent(new BytesArray(data), null)
.build();
} else {
return new FakeRestRequest.Builder(xContentRegistry())
.withContent(new BytesArray(data), XContentType.JSON).build();
}
}


@Override
protected NamedXContentRegistry xContentRegistry() {
return new NamedXContentRegistry(singletonList(new NamedXContentRegistry.Entry(QueryBuilder.class,
Expand Down Expand Up @@ -271,6 +296,48 @@ public void testWritingExpandWildcards() throws IOException {
randomBoolean(), randomBoolean(), randomBoolean()), "none");
}

public void testEmptyFirstLine1() throws Exception {
MultiSearchRequest request = parseMultiSearchRequestFromString(
"\n" +
"\n" +
"{ \"query\": {\"match_all\": {}}}\n" +
"{}\n" +
"{ \"query\": {\"match_all\": {}}}\n" +
"\n" +
"{ \"query\": {\"match_all\": {}}}\n" +
"{}\n" +
"{ \"query\": {\"match_all\": {}}}\n",
RestApiVersion.V_7);
assertThat(request.requests().size(), equalTo(4));
for (SearchRequest searchRequest : request.requests()) {
assertThat(searchRequest.indices().length, equalTo(0));
assertThat(searchRequest.source().query(), instanceOf(MatchAllQueryBuilder.class));
}
assertWarnings("support for empty first line before any action metadata in msearch API is deprecated and will be removed " +
"in the next major version");
}

public void testEmptyFirstLine2() throws Exception {
MultiSearchRequest request = parseMultiSearchRequestFromString(
"\n" +
"{}\n" +
"{ \"query\": {\"match_all\": {}}}\n" +
"\n" +
"{ \"query\": {\"match_all\": {}}}\n" +
"{}\n" +
"{ \"query\": {\"match_all\": {}}}\n" +
"\n" +
"{ \"query\": {\"match_all\": {}}}\n",
RestApiVersion.V_7);
assertThat(request.requests().size(), equalTo(4));
for (SearchRequest searchRequest : request.requests()) {
assertThat(searchRequest.indices().length, equalTo(0));
assertThat(searchRequest.source().query(), instanceOf(MatchAllQueryBuilder.class));
}
assertWarnings("support for empty first line before any action metadata in msearch API is deprecated and will be removed " +
"in the next major version");
}

private void assertExpandWildcardsValue(IndicesOptions options, String expectedValue) throws IOException {
SearchRequest request = new SearchRequest();
request.indicesOptions(options);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


{ "query": {"match_all": {}}}
{}
{ "query": {"match_all": {}}}

{ "query": {"match_all": {}}}
{}
{ "query": {"match_all": {}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


{}
{ "query": {"match_all": {}}}

{ "query": {"match_all": {}}}
{}
{ "query": {"match_all": {}}}

{ "query": {"match_all": {}}}

0 comments on commit e9e1f35

Please sign in to comment.