Skip to content

Commit f9ef2c0

Browse files
EQL: accept project_routing as query parameter (#138559)
1 parent cbe2233 commit f9ef2c0

File tree

6 files changed

+21
-1
lines changed

6 files changed

+21
-1
lines changed

docs/changelog/138559.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 138559
2+
summary: Accept `project_routing` as query parameter
3+
area: EQL
4+
type: enhancement
5+
issues: []

x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlRestTestCase.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ public void checkSearchContent() throws Exception {
6161
""", validQuery), "query malformed, empty clause found" },
6262
{ String.format(Locale.ROOT, """
6363
{"query": "%s", "max_samples_per_key": 0}
64-
""", validQuery), "max_samples_per_key must be greater than 0" } };
64+
""", validQuery), "max_samples_per_key must be greater than 0" },
65+
{ String.format(Locale.ROOT, """
66+
{"query": "%s", "project_routing": "foo"}
67+
""", validQuery), "[project_routing] is only allowed when cross-project search is enabled" } };
6568

6669
public void testBadRequests() throws Exception {
6770
createIndex(defaultValidationIndexName, (String) null);

x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/action/EqlSearchRequest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public class EqlSearchRequest extends LegacyActionRequest implements IndicesRequ
9090
static final String KEY_MAX_SAMPLES_PER_KEY = "max_samples_per_key";
9191
static final String KEY_ALLOW_PARTIAL_SEARCH_RESULTS = "allow_partial_search_results";
9292
static final String KEY_ALLOW_PARTIAL_SEQUENCE_RESULTS = "allow_partial_sequence_results";
93+
static final String KEY_PROJECT_ROUTING = "project_routing";
9394

9495
static final ParseField FILTER = new ParseField(KEY_FILTER);
9596
static final ParseField TIMESTAMP_FIELD = new ParseField(KEY_TIMESTAMP_FIELD);
@@ -106,6 +107,7 @@ public class EqlSearchRequest extends LegacyActionRequest implements IndicesRequ
106107
static final ParseField MAX_SAMPLES_PER_KEY = new ParseField(KEY_MAX_SAMPLES_PER_KEY);
107108
static final ParseField ALLOW_PARTIAL_SEARCH_RESULTS = new ParseField(KEY_ALLOW_PARTIAL_SEARCH_RESULTS);
108109
static final ParseField ALLOW_PARTIAL_SEQUENCE_RESULTS = new ParseField(KEY_ALLOW_PARTIAL_SEQUENCE_RESULTS);
110+
static final ParseField PROJECT_ROUTING = new ParseField(KEY_PROJECT_ROUTING);
109111

110112
private static final ObjectParser<EqlSearchRequest, Void> PARSER = objectParser(EqlSearchRequest::new);
111113

@@ -301,6 +303,7 @@ protected static <R extends EqlSearchRequest> ObjectParser<R, Void> objectParser
301303
parser.declareInt(EqlSearchRequest::maxSamplesPerKey, MAX_SAMPLES_PER_KEY);
302304
parser.declareBoolean(EqlSearchRequest::allowPartialSearchResults, ALLOW_PARTIAL_SEARCH_RESULTS);
303305
parser.declareBoolean(EqlSearchRequest::allowPartialSequenceResults, ALLOW_PARTIAL_SEQUENCE_RESULTS);
306+
parser.declareString(EqlSearchRequest::projectRouting, PROJECT_ROUTING);
304307
return parser;
305308
}
306309

x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/RestEqlSearchAction.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.elasticsearch.xpack.eql.action.EqlSearchAction;
3030
import org.elasticsearch.xpack.eql.action.EqlSearchRequest;
3131
import org.elasticsearch.xpack.eql.action.EqlSearchResponse;
32+
import org.elasticsearch.xpack.ql.InvalidArgumentException;
3233

3334
import java.io.IOException;
3435
import java.util.List;
@@ -87,6 +88,10 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
8788
eqlRequest.allowPartialSequenceResults(
8889
request.paramAsBoolean("allow_partial_sequence_results", eqlRequest.allowPartialSequenceResults())
8990
);
91+
eqlRequest.projectRouting(request.param("project_routing", eqlRequest.getProjectRouting()));
92+
if (crossProjectEnabled == false && eqlRequest.getProjectRouting() != null) {
93+
throw new InvalidArgumentException("[project_routing] is only allowed when cross-project search is enabled");
94+
}
9095
}
9196

9297
return channel -> {

x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlRequestParserTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public void testSearchRequestParser() throws IOException {
6868
"timestamp_field": "tsf",
6969
"event_category_field": "etf",
7070
"size": "101",
71+
"project_routing": "_alias:_origin",
7172
"query": "file where user != 'SYSTEM' by file_path"
7273
}""", EqlSearchRequest::fromXContent);
7374
assertArrayEquals(new String[] { "endgame-*" }, request.indices());
@@ -81,6 +82,7 @@ public void testSearchRequestParser() throws IOException {
8182
assertEquals(101, request.size());
8283
assertEquals(1000, request.fetchSize());
8384
assertEquals("file where user != 'SYSTEM' by file_path", request.query());
85+
assertEquals("_alias:_origin", request.getProjectRouting());
8486
}
8587

8688
private EqlSearchRequest generateRequest(String index, String json, Function<XContentParser, EqlSearchRequest> fromXContent)

x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlSearchRequestTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ protected EqlSearchRequest createTestInstance() {
8181
.keepOnCompletion(randomBoolean())
8282
.allowPartialSearchResults(randomBoolean())
8383
.allowPartialSequenceResults(randomBoolean())
84+
.projectRouting(randomAlphaOfLength(10))
8485
.fetchFields(randomFetchFields)
8586
.runtimeMappings(randomRuntimeMappings())
8687
.resultPosition(randomFrom("tail", "head"))
@@ -143,6 +144,7 @@ protected EqlSearchRequest mutateInstanceForVersion(EqlSearchRequest instance, T
143144
mutatedInstance.allowPartialSequenceResults(
144145
version.supports(TransportVersions.V_8_18_0) ? instance.allowPartialSequenceResults() : false
145146
);
147+
mutatedInstance.projectRouting(instance.getProjectRouting());
146148

147149
return mutatedInstance;
148150
}

0 commit comments

Comments
 (0)