Skip to content

ESQL: List/get query API #124832

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

Merged
merged 24 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d6b0a92
ESQL: List/get query API
GalLalouche Mar 13, 2025
d961e27
Added super simple test
GalLalouche Mar 16, 2025
55e7625
More tests
GalLalouche Mar 17, 2025
8ad4d6d
Add operators to constants, rename
GalLalouche Mar 17, 2025
fd08751
ESQL: List/get query API
GalLalouche Mar 13, 2025
f1aab5d
Added super simple test
GalLalouche Mar 16, 2025
af2b5ee
More tests
GalLalouche Mar 17, 2025
1d95cbc
Add operators to constants, rename
GalLalouche Mar 17, 2025
cb4ec47
Update docs/changelog/124832.yaml
GalLalouche Mar 18, 2025
2fa0085
Merge branch 'main' into feature/list_query_api
GalLalouche Mar 23, 2025
ff0fe90
Moved test
GalLalouche Mar 23, 2025
758319a
Merge remote-tracking branch 'origin/feature/list_query_api' into fea…
GalLalouche Mar 23, 2025
894dad6
TEMP
GalLalouche Mar 25, 2025
30f5ef1
Merge branch 'main' into feature/list_query_api
GalLalouche Mar 27, 2025
9bcdb10
More CR fixes
GalLalouche Mar 27, 2025
6686702
Merge branch 'main' into feature/list_query_api
GalLalouche Mar 27, 2025
9847add
Add null URL to rest API
GalLalouche Mar 27, 2025
1d0d15c
Merge branch 'main' into feature/list_query_api
GalLalouche Mar 30, 2025
9fe9243
Merge branch 'main' into feature/list_query_api
GalLalouche Apr 1, 2025
f786222
Update privileges
GalLalouche Apr 1, 2025
6134a8e
Security fixes
GalLalouche Apr 2, 2025
789362f
Merge branch 'main' into feature/list_query_api
GalLalouche Apr 2, 2025
0cedb9a
CR fixes
GalLalouche Apr 8, 2025
09dc300
Merge branch 'main' into feature/list_query_api
GalLalouche Apr 8, 2025
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
Prev Previous commit
Next Next commit
ESQL: List/get query API
  • Loading branch information
GalLalouche committed Mar 19, 2025
commit fd0875143f1267c39e6d1f35655977b18bae4d6c
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,12 @@ public void testAsyncGetWithoutContentType() throws IOException {

}

public void testListApi_noRunningQueries_returnsAnEmptyArray() throws Exception {
Request request = prepareListQueriesRequest();
Response response = performRequest(request);
assertThat(entityToMap(response.getEntity(), XContentType.JSON), is(Map.of("queries", Map.of())));
}

protected static Request prepareRequestWithOptions(RequestObjectBuilder requestObject, Mode mode) throws IOException {
requestObject.build();
Request request = prepareRequest(mode);
Expand Down Expand Up @@ -1515,21 +1521,22 @@ static String runEsqlAsTextWithFormat(RequestObjectBuilder builder, String forma
}

private static Request prepareRequest(Mode mode) {
Request request = new Request("POST", "/_query" + (mode == ASYNC ? "/async" : ""));
request.addParameter("error_trace", "true"); // Helps with debugging in case something crazy happens on the server.
request.addParameter("pretty", "true"); // Improves error reporting readability
return request;
return finishRequest(new Request("POST", "/_query" + (mode == ASYNC ? "/async" : "")));
}

private static Request prepareAsyncGetRequest(String id) {
Request request = new Request("GET", "/_query/async/" + id + "?wait_for_completion_timeout=60s");
request.addParameter("error_trace", "true"); // Helps with debugging in case something crazy happens on the server.
request.addParameter("pretty", "true"); // Improves error reporting readability
return request;
return finishRequest(new Request("GET", "/_query/async/" + id + "?wait_for_completion_timeout=60s"));
}

private static Request prepareAsyncDeleteRequest(String id) {
Request request = new Request("DELETE", "/_query/async/" + id);
return finishRequest(new Request("DELETE", "/_query/async/" + id));
}

private static Request prepareListQueriesRequest() {
return finishRequest(new Request("GET", "/_query/queries/"));
}

private static Request finishRequest(Request request) {
request.addParameter("error_trace", "true"); // Helps with debugging in case something crazy happens on the server.
request.addParameter("pretty", "true"); // Improves error reporting readability
return request;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.action;

import org.elasticsearch.action.ActionType;
import org.elasticsearch.xpack.esql.plugin.EsqlGetQueryResponse;

public class EsqlGetQueryAction extends ActionType<EsqlGetQueryResponse> {
public static final EsqlGetQueryAction INSTANCE = new EsqlGetQueryAction();
public static final String NAME = "cluster:data/read/esql/query";

private EsqlGetQueryAction() {
super(NAME);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.action;

import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;

import java.io.IOException;

public class EsqlGetQueryRequest extends ActionRequest {
private final String id;

public EsqlGetQueryRequest(String id) {
this.id = id;
}

public String id() {
return id;
}

public EsqlGetQueryRequest(StreamInput streamInput) throws IOException {
super(streamInput);
id = streamInput.readString();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(id);
}

@Override
public ActionRequestValidationException validate() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.action;

import org.elasticsearch.action.ActionType;
import org.elasticsearch.xpack.esql.plugin.EsqlListQueriesResponse;

public class EsqlListQueriesAction extends ActionType<EsqlListQueriesResponse> {
public static final EsqlListQueriesAction INSTANCE = new EsqlListQueriesAction();
public static final String NAME = "cluster:data/read/esql/queries";

private EsqlListQueriesAction() {
super(NAME);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.action;

import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.common.io.stream.StreamInput;

import java.io.IOException;

public class EsqlListQueriesRequest extends ActionRequest {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should rely on org.elasticsearch.transport.EmptyRequest instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like that's a test class... is it used in production?

public EsqlListQueriesRequest() {}

public EsqlListQueriesRequest(StreamInput streamInput) throws IOException {
super(streamInput);
}

@Override
public ActionRequestValidationException validate() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.action;

import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.logging.LogManager;
import org.elasticsearch.logging.Logger;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.Scope;
import org.elasticsearch.rest.ServerlessScope;
import org.elasticsearch.rest.action.RestToXContentListener;

import java.io.IOException;
import java.util.List;

import static org.elasticsearch.rest.RestRequest.Method.GET;

@ServerlessScope(Scope.PUBLIC)
public class RestEsqlListQueriesAction extends BaseRestHandler {
private static final Logger LOGGER = LogManager.getLogger(RestEsqlListQueriesAction.class);

@Override
public String getName() {
return "esql_list_queries";
}

@Override
public List<Route> routes() {
return List.of(new Route(GET, "/_query/queries/{id}"), new Route(GET, "/_query/queries"));
Copy link
Contributor

@idegtiarenko idegtiarenko Mar 19, 2025

Choose a reason for hiding this comment

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

The repetition in the endpoint looks surprising to me. Why not /_queries or may be /_cluster/queries?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

For the time being we're looking to keep the ESQL APIs under the ESQL prefix(_query) and avoid potential clashes with other rest APIs.
The decision hasn't been finalized though.

Copy link
Member

Choose a reason for hiding this comment

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

Let's sort this one out after this PR.

Copy link
Member

Choose a reason for hiding this comment

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

We can move this - we haven't finalized this API.

}

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
return restChannelConsumer(request, client);
}

private static RestChannelConsumer restChannelConsumer(RestRequest request, NodeClient client) {
LOGGER.debug("Beginning execution of ESQL list queries.");

String id = request.param("id");
return id != null
? (channel -> client.execute(EsqlGetQueryAction.INSTANCE, new EsqlGetQueryRequest(id), new RestToXContentListener<>(channel)))
: (channel -> client.execute(
EsqlListQueriesAction.INSTANCE,
new EsqlListQueriesRequest(),
new RestToXContentListener<>(channel)
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.plugin;

import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.List;

public class EsqlGetQueryResponse extends ActionResponse implements ToXContentObject {
// 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.
public record DetailedQuery(
String id,
long startTimeMillis,
long runningTimeNanos,
String query,
String coordinatingNode,
List<String> dataNodes
) implements Writeable, ToXContentObject {
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(id);
out.writeLong(startTimeMillis);
out.writeLong(runningTimeNanos);
out.writeString(query);
out.writeString(coordinatingNode);
out.writeStringCollection(dataNodes);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("id", id);
builder.field("startTimeMillis", startTimeMillis);
builder.field("runningTimeNanos", runningTimeNanos);
builder.field("query", query);
builder.field("coordinatingNode", coordinatingNode);
builder.field("dataNodes", dataNodes);
builder.endObject();
return builder;
}
}

private final DetailedQuery query;

public EsqlGetQueryResponse(DetailedQuery query) {
this.query = query;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeWriteable(query);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return query.toXContent(builder, params);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.plugin;

import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.List;

public class EsqlListQueriesResponse extends ActionResponse implements ToXContentObject {
private final List<Query> queries;

public record Query(String id, long startTimeMillis, long runningTimeNanos, String query) implements Writeable, ToXContentObject {
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(id);
out.writeLong(startTimeMillis);
out.writeLong(runningTimeNanos);
out.writeString(query);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("id", id);
builder.field("startTimeMillis", startTimeMillis);
builder.field("runningTimeNanos", runningTimeNanos);
builder.field("query", query);
builder.endObject();
return builder;
}
}

public EsqlListQueriesResponse(List<Query> queries) {
this.queries = queries;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeCollection(queries);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("queries");
// for (Query query : queries) {
// query.toXContent(builder, params);
// }
builder.endObject();
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@
import org.elasticsearch.xpack.esql.EsqlUsageTransportAction;
import org.elasticsearch.xpack.esql.action.EsqlAsyncGetResultAction;
import org.elasticsearch.xpack.esql.action.EsqlAsyncStopAction;
import org.elasticsearch.xpack.esql.action.EsqlGetQueryAction;
import org.elasticsearch.xpack.esql.action.EsqlListQueriesAction;
import org.elasticsearch.xpack.esql.action.EsqlQueryAction;
import org.elasticsearch.xpack.esql.action.EsqlQueryRequestBuilder;
import org.elasticsearch.xpack.esql.action.EsqlResolveFieldsAction;
import org.elasticsearch.xpack.esql.action.EsqlSearchShardsAction;
import org.elasticsearch.xpack.esql.action.RestEsqlAsyncQueryAction;
import org.elasticsearch.xpack.esql.action.RestEsqlDeleteAsyncResultAction;
import org.elasticsearch.xpack.esql.action.RestEsqlGetAsyncResultAction;
import org.elasticsearch.xpack.esql.action.RestEsqlListQueriesAction;
import org.elasticsearch.xpack.esql.action.RestEsqlQueryAction;
import org.elasticsearch.xpack.esql.action.RestEsqlStopAsyncAction;
import org.elasticsearch.xpack.esql.enrich.EnrichLookupOperator;
Expand Down Expand Up @@ -229,7 +232,9 @@ public List<Setting<?>> getSettings() {
new ActionHandler<>(XPackInfoFeatureAction.ESQL, EsqlInfoTransportAction.class),
new ActionHandler<>(EsqlResolveFieldsAction.TYPE, EsqlResolveFieldsAction.class),
new ActionHandler<>(EsqlSearchShardsAction.TYPE, EsqlSearchShardsAction.class),
new ActionHandler<>(EsqlAsyncStopAction.INSTANCE, TransportEsqlAsyncStopAction.class)
new ActionHandler<>(EsqlAsyncStopAction.INSTANCE, TransportEsqlAsyncStopAction.class),
new ActionHandler<>(EsqlListQueriesAction.INSTANCE, TransportEsqlListQueriesAction.class),
new ActionHandler<>(EsqlGetQueryAction.INSTANCE, TransportEsqlGetQueryAction.class)
);
}

Expand All @@ -250,7 +255,8 @@ public List<RestHandler> getRestHandlers(
new RestEsqlAsyncQueryAction(),
new RestEsqlGetAsyncResultAction(),
new RestEsqlStopAsyncAction(),
new RestEsqlDeleteAsyncResultAction()
new RestEsqlDeleteAsyncResultAction(),
new RestEsqlListQueriesAction()
);
}

Expand Down
Loading