Skip to content
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
5 changes: 5 additions & 0 deletions docs/changelog/138583.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 138583
summary: "Fix: Correctly pickup MRT value for `msearch`'s search requests"
area: CCS
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@
import org.elasticsearch.TransportVersion;
import org.elasticsearch.action.search.MultiSearchRequest;
import org.elasticsearch.action.search.MultiSearchResponse.Item;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.search.RestMultiSearchAction;
import org.elasticsearch.search.DummyQueryBuilder;
import org.elasticsearch.search.SearchService;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.TransportVersionUtils;
import org.elasticsearch.test.rest.FakeRestRequest;
import org.elasticsearch.usage.UsageService;
import org.elasticsearch.xcontent.XContentType;
import org.hamcrest.Matchers;

import java.io.IOException;
import java.util.Map;
import java.util.Optional;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFirstHit;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
Expand Down Expand Up @@ -119,4 +130,90 @@ public TransportVersion getMinimalSupportedVersion() {
}
);
}

public void testMrtValuesArePickedCorrectly() throws IOException {

{
// If no MRT is specified, all searches should default to true.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it default to true?
Is that because _msearch always uses _search rather than _async_search and _search defaults to true?

Copy link
Contributor Author

@pawankartik-elastic pawankartik-elastic Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. And when the SearchRequest object is instantiated, MRT defaults to true.

String body = """
{"index": "index-1" }
{"query" : {"match" : { "message": "this is a test"}}}
{"index": "index-2" }
{"query" : {"match_all" : {}}}
""";

MultiSearchRequest mreq = parseRequest(body, Map.of());
for (SearchRequest req : mreq.requests()) {
assertTrue(req.isCcsMinimizeRoundtrips());
}
}

{
// MRT query param is false, so all searches should use this value.
String body = """
{"index": "index-1" }
{"query" : {"match" : { "message": "this is a test"}}}
{"index": "index-2" }
{"query" : {"match_all" : {}}}
""";

MultiSearchRequest mreq = parseRequest(body, Map.of("ccs_minimize_roundtrips", "false"));
for (SearchRequest req : mreq.requests()) {
assertFalse(req.isCcsMinimizeRoundtrips());
}
}

{
// Query param is absent but MRT is specified for each request.
String body = """
{"index": "index-1", "ccs_minimize_roundtrips": false }
{"query" : {"match" : { "message": "this is a test"}}}
{"index": "index-2", "ccs_minimize_roundtrips": false }
{"query" : {"match_all" : {}}}
""";

MultiSearchRequest mreq = parseRequest(body, Map.of());
for (SearchRequest req : mreq.requests()) {
assertFalse(req.isCcsMinimizeRoundtrips());
}
}

{
/*
* The first request overrides the query param and should use MRT=true.
* The second request should use the query param value.
*/
String body = """
{"index": "index-1", "ccs_minimize_roundtrips": true }
{"query" : {"match" : { "message": "this is a test"}}}
{"index": "index-2" }
{"query" : {"match_all" : {}}}
""";

MultiSearchRequest mreq = parseRequest(body, Map.of("ccs_minimize_roundtrips", "false"));

assertThat(mreq.requests().size(), Matchers.is(2));
assertTrue(mreq.requests().getFirst().isCcsMinimizeRoundtrips());
assertFalse(mreq.requests().getLast().isCcsMinimizeRoundtrips());
}
}

private RestRequest mkRequest(String body, Map<String, String> params) {
return new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.POST)
.withPath("/index*/_msearch")
.withParams(params)
.withContent(new BytesArray(body), XContentType.JSON)
.build();
}

private MultiSearchRequest parseRequest(String body, Map<String, String> params) throws IOException {
return RestMultiSearchAction.parseRequest(
mkRequest(body, params),
true,
new UsageService().getSearchUsageHolder(),
(ignored) -> true,
// Disable CPS for these tests.
Optional.of(false)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,6 @@ public static MultiSearchRequest parseRequest(
RestSearchAction.validateSearchRequest(restRequest, searchRequest);
if (searchRequest.pointInTimeBuilder() != null) {
RestSearchAction.preparePointInTime(searchRequest, restRequest);
} else {
searchRequest.setCcsMinimizeRoundtrips(
SearchParamsParser.parseCcsMinimizeRoundtrips(crossProjectEnabled, restRequest, searchRequest.isCcsMinimizeRoundtrips())
);
}
multiRequest.add(searchRequest);
}, extraParamParser, crossProjectEnabled, multiRequest.getProjectRouting());
Expand Down