Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rest Api Compatibility] Allow first empty line for msearch #75886

Merged
merged 1 commit into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": {}}}