Skip to content

Commit d554936

Browse files
committed
ESQL: List/get query API
1 parent af6eb8c commit d554936

10 files changed

+448
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.action;
9+
10+
import org.elasticsearch.action.ActionType;
11+
import org.elasticsearch.xpack.esql.plugin.EsqlGetQueryResponse;
12+
13+
public class EsqlGetQueryAction extends ActionType<EsqlGetQueryResponse> {
14+
public static final EsqlGetQueryAction INSTANCE = new EsqlGetQueryAction();
15+
public static final String NAME = "cluster:data/read/esql/query";
16+
17+
private EsqlGetQueryAction() {
18+
super(NAME);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.action;
9+
10+
import org.elasticsearch.action.ActionRequest;
11+
import org.elasticsearch.action.ActionRequestValidationException;
12+
import org.elasticsearch.common.io.stream.StreamInput;
13+
import org.elasticsearch.common.io.stream.StreamOutput;
14+
15+
import java.io.IOException;
16+
17+
public class EsqlGetQueryRequest extends ActionRequest {
18+
private final String id;
19+
20+
public EsqlGetQueryRequest(String id) {
21+
this.id = id;
22+
}
23+
24+
public String id() {
25+
return id;
26+
}
27+
28+
public EsqlGetQueryRequest(StreamInput streamInput) throws IOException {
29+
super(streamInput);
30+
id = streamInput.readString();
31+
}
32+
33+
@Override
34+
public void writeTo(StreamOutput out) throws IOException {
35+
out.writeString(id);
36+
}
37+
38+
@Override
39+
public ActionRequestValidationException validate() {
40+
return null;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.action;
9+
10+
import org.elasticsearch.action.ActionType;
11+
import org.elasticsearch.xpack.esql.plugin.EsqlListQueriesResponse;
12+
13+
public class EsqlListQueriesAction extends ActionType<EsqlListQueriesResponse> {
14+
public static final EsqlListQueriesAction INSTANCE = new EsqlListQueriesAction();
15+
public static final String NAME = "cluster:data/read/esql/queries";
16+
17+
private EsqlListQueriesAction() {
18+
super(NAME);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.action;
9+
10+
import org.elasticsearch.action.ActionRequest;
11+
import org.elasticsearch.action.ActionRequestValidationException;
12+
import org.elasticsearch.common.io.stream.StreamInput;
13+
14+
import java.io.IOException;
15+
16+
public class EsqlListQueriesRequest extends ActionRequest {
17+
public EsqlListQueriesRequest() {}
18+
19+
public EsqlListQueriesRequest(StreamInput streamInput) throws IOException {
20+
super(streamInput);
21+
}
22+
23+
@Override
24+
public ActionRequestValidationException validate() {
25+
return null;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.action;
9+
10+
import org.elasticsearch.client.internal.node.NodeClient;
11+
import org.elasticsearch.logging.LogManager;
12+
import org.elasticsearch.logging.Logger;
13+
import org.elasticsearch.rest.BaseRestHandler;
14+
import org.elasticsearch.rest.RestRequest;
15+
import org.elasticsearch.rest.Scope;
16+
import org.elasticsearch.rest.ServerlessScope;
17+
import org.elasticsearch.rest.action.RestToXContentListener;
18+
19+
import java.io.IOException;
20+
import java.util.List;
21+
22+
import static org.elasticsearch.rest.RestRequest.Method.GET;
23+
24+
@ServerlessScope(Scope.PUBLIC)
25+
public class RestEsqlListQueriesAction extends BaseRestHandler {
26+
private static final Logger LOGGER = LogManager.getLogger(RestEsqlListQueriesAction.class);
27+
28+
@Override
29+
public String getName() {
30+
return "esql_list_queries";
31+
}
32+
33+
@Override
34+
public List<Route> routes() {
35+
return List.of(new Route(GET, "/_query/queries/{id}"), new Route(GET, "/_query/queries"));
36+
}
37+
38+
@Override
39+
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
40+
return restChannelConsumer(request, client);
41+
}
42+
43+
private static RestChannelConsumer restChannelConsumer(RestRequest request, NodeClient client) {
44+
LOGGER.debug("Beginning execution of ESQL list queries.");
45+
46+
String id = request.param("id");
47+
return id != null
48+
? (channel -> client.execute(EsqlGetQueryAction.INSTANCE, new EsqlGetQueryRequest(id), new RestToXContentListener<>(channel)))
49+
: (channel -> client.execute(
50+
EsqlListQueriesAction.INSTANCE,
51+
new EsqlListQueriesRequest(),
52+
new RestToXContentListener<>(channel)
53+
));
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.plugin;
9+
10+
import org.elasticsearch.action.ActionResponse;
11+
import org.elasticsearch.common.io.stream.StreamOutput;
12+
import org.elasticsearch.common.io.stream.Writeable;
13+
import org.elasticsearch.xcontent.ToXContentObject;
14+
import org.elasticsearch.xcontent.XContentBuilder;
15+
16+
import java.io.IOException;
17+
import java.util.List;
18+
19+
public class EsqlGetQueryResponse extends ActionResponse implements ToXContentObject {
20+
// This is rather limited at the moment, as we don't extract information such as CPU and memory usage, owning user, etc. for the task.
21+
public record DetailedQuery(
22+
String id,
23+
long startTimeMillis,
24+
long runningTimeNanos,
25+
String query,
26+
String coordinatingNode,
27+
List<String> dataNodes
28+
) implements Writeable, ToXContentObject {
29+
@Override
30+
public void writeTo(StreamOutput out) throws IOException {
31+
out.writeString(id);
32+
out.writeLong(startTimeMillis);
33+
out.writeLong(runningTimeNanos);
34+
out.writeString(query);
35+
out.writeString(coordinatingNode);
36+
out.writeStringCollection(dataNodes);
37+
}
38+
39+
@Override
40+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
41+
builder.startObject();
42+
builder.field("id", id);
43+
builder.field("startTimeMillis", startTimeMillis);
44+
builder.field("runningTimeNanos", runningTimeNanos);
45+
builder.field("query", query);
46+
builder.field("coordinatingNode", coordinatingNode);
47+
builder.field("dataNodes", dataNodes);
48+
builder.endObject();
49+
return builder;
50+
}
51+
}
52+
53+
private final DetailedQuery query;
54+
55+
public EsqlGetQueryResponse(DetailedQuery query) {
56+
this.query = query;
57+
}
58+
59+
@Override
60+
public void writeTo(StreamOutput out) throws IOException {
61+
out.writeWriteable(query);
62+
}
63+
64+
@Override
65+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
66+
return query.toXContent(builder, params);
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.plugin;
9+
10+
import org.elasticsearch.action.ActionResponse;
11+
import org.elasticsearch.common.io.stream.StreamOutput;
12+
import org.elasticsearch.common.io.stream.Writeable;
13+
import org.elasticsearch.xcontent.ToXContentObject;
14+
import org.elasticsearch.xcontent.XContentBuilder;
15+
16+
import java.io.IOException;
17+
import java.util.List;
18+
19+
public class EsqlListQueriesResponse extends ActionResponse implements ToXContentObject {
20+
private final List<Query> queries;
21+
22+
public record Query(String id, long startTimeMillis, long runningTimeNanos, String query) implements Writeable, ToXContentObject {
23+
@Override
24+
public void writeTo(StreamOutput out) throws IOException {
25+
out.writeString(id);
26+
out.writeLong(startTimeMillis);
27+
out.writeLong(runningTimeNanos);
28+
out.writeString(query);
29+
}
30+
31+
@Override
32+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
33+
builder.startObject();
34+
builder.field("id", id);
35+
builder.field("startTimeMillis", startTimeMillis);
36+
builder.field("runningTimeNanos", runningTimeNanos);
37+
builder.field("query", query);
38+
builder.endObject();
39+
return builder;
40+
}
41+
}
42+
43+
public EsqlListQueriesResponse(List<Query> queries) {
44+
this.queries = queries;
45+
}
46+
47+
@Override
48+
public void writeTo(StreamOutput out) throws IOException {
49+
out.writeCollection(queries);
50+
}
51+
52+
@Override
53+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
54+
builder.startArray();
55+
for (Query query : queries) {
56+
query.toXContent(builder, params);
57+
}
58+
builder.endArray();
59+
return builder;
60+
}
61+
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plugin/EsqlPlugin.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@
5353
import org.elasticsearch.xpack.esql.EsqlUsageTransportAction;
5454
import org.elasticsearch.xpack.esql.action.EsqlAsyncGetResultAction;
5555
import org.elasticsearch.xpack.esql.action.EsqlAsyncStopAction;
56+
import org.elasticsearch.xpack.esql.action.EsqlGetQueryAction;
57+
import org.elasticsearch.xpack.esql.action.EsqlListQueriesAction;
5658
import org.elasticsearch.xpack.esql.action.EsqlQueryAction;
5759
import org.elasticsearch.xpack.esql.action.EsqlQueryRequestBuilder;
5860
import org.elasticsearch.xpack.esql.action.EsqlResolveFieldsAction;
5961
import org.elasticsearch.xpack.esql.action.EsqlSearchShardsAction;
6062
import org.elasticsearch.xpack.esql.action.RestEsqlAsyncQueryAction;
6163
import org.elasticsearch.xpack.esql.action.RestEsqlDeleteAsyncResultAction;
6264
import org.elasticsearch.xpack.esql.action.RestEsqlGetAsyncResultAction;
65+
import org.elasticsearch.xpack.esql.action.RestEsqlListQueriesAction;
6366
import org.elasticsearch.xpack.esql.action.RestEsqlQueryAction;
6467
import org.elasticsearch.xpack.esql.action.RestEsqlStopAsyncAction;
6568
import org.elasticsearch.xpack.esql.enrich.EnrichLookupOperator;
@@ -170,7 +173,9 @@ public List<Setting<?>> getSettings() {
170173
new ActionHandler<>(XPackInfoFeatureAction.ESQL, EsqlInfoTransportAction.class),
171174
new ActionHandler<>(EsqlResolveFieldsAction.TYPE, EsqlResolveFieldsAction.class),
172175
new ActionHandler<>(EsqlSearchShardsAction.TYPE, EsqlSearchShardsAction.class),
173-
new ActionHandler<>(EsqlAsyncStopAction.INSTANCE, TransportEsqlAsyncStopAction.class)
176+
new ActionHandler<>(EsqlAsyncStopAction.INSTANCE, TransportEsqlAsyncStopAction.class),
177+
new ActionHandler<>(EsqlListQueriesAction.INSTANCE, TransportEsqlListQueriesAction.class),
178+
new ActionHandler<>(EsqlGetQueryAction.INSTANCE, TransportEsqlGetQueryAction.class)
174179
);
175180
}
176181

@@ -191,7 +196,8 @@ public List<RestHandler> getRestHandlers(
191196
new RestEsqlAsyncQueryAction(),
192197
new RestEsqlGetAsyncResultAction(),
193198
new RestEsqlStopAsyncAction(),
194-
new RestEsqlDeleteAsyncResultAction()
199+
new RestEsqlDeleteAsyncResultAction(),
200+
new RestEsqlListQueriesAction()
195201
);
196202
}
197203

0 commit comments

Comments
 (0)