Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
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
6 changes: 6 additions & 0 deletions docs/changelog/124832.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 124832
summary: List/get query API
area: ES|QL
type: feature
issues:
- 124827
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"esql.get_query": {
"documentation": {
"url": null,
"description": "Executes a get ESQL query request"
},
"stability": "experimental",
"visibility": "public",
"headers": {
"accept": [],
"content_type": [
"application/json"
]
},
"url": {
"paths": [
{
"path": "/_query/queries/{id}",
"methods": [
"GET"
],
"parts": {
"id": {
"type": "string",
"description": "The query ID"
}
}
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"esql.list_queries": {
"documentation": {
"url": null,
"description": "Executes a list ESQL queries request"
},
"stability": "experimental",
"visibility": "public",
"headers": {
"accept": [],
"content_type": [
"application/json"
]
},
"url": {
"paths": [
{
"path": "/_query/queries",
"methods": [
"GET"
]
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.test;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;

import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.isA;

/**
* A type-agnostic way of comparing integer values, not caring if it's a long or an integer.
*/
public abstract sealed class IntOrLongMatcher<T> extends BaseMatcher<T> {
public static IntOrLongMatcher<Integer> matches(int expected) {
return new IntMatcher(expected);
}

public static IntOrLongMatcher<Long> matches(long expected) {
return new LongMatcher(expected);
}

private static final class IntMatcher extends IntOrLongMatcher<Integer> {
private final int expected;

private IntMatcher(int expected) {
this.expected = expected;
}

@Override
public boolean matches(Object o) {
return switch (o) {
case Integer i -> expected == i;
case Long l -> expected == l;
default -> false;
};
}

@Override
public void describeTo(Description description) {
equalTo(expected).describeTo(description);
}
}

private static final class LongMatcher extends IntOrLongMatcher<Long> {
private final long expected;

LongMatcher(long expected) {
this.expected = expected;
}

@Override
public boolean matches(Object o) {
return switch (o) {
case Integer i -> expected == i;
case Long l -> expected == l;
default -> false;
};
}

@Override
public void describeTo(Description description) {
equalTo(expected).describeTo(description);
}
}

public static Matcher<Object> isIntOrLong() {
return anyOf(isA(Integer.class), isA(Long.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ private static String maybeRewriteSingleAuthenticationHeaderForVersion(
public static final String APM_ORIGIN = "apm";
public static final String OTEL_ORIGIN = "otel";
public static final String REINDEX_DATA_STREAM_ORIGIN = "reindex_data_stream";
public static final String ESQL_ORIGIN = "esql";

private ClientHelper() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public class ClusterPrivilegeResolver {
private static final Set<String> MONITOR_WATCHER_PATTERN = Set.of("cluster:monitor/xpack/watcher/*");
private static final Set<String> MONITOR_ROLLUP_PATTERN = Set.of("cluster:monitor/xpack/rollup/*");
private static final Set<String> MONITOR_ENRICH_PATTERN = Set.of("cluster:monitor/xpack/enrich/*", "cluster:admin/xpack/enrich/get");
private static final Set<String> MONITOR_ESQL_PATTERN = Set.of("cluster:monitor/xpack/esql/*");
// intentionally cluster:monitor/stats* to match cluster:monitor/stats, cluster:monitor/stats[n] and cluster:monitor/stats/remote
private static final Set<String> MONITOR_STATS_PATTERN = Set.of("cluster:monitor/stats*");

Expand Down Expand Up @@ -249,6 +250,7 @@ public class ClusterPrivilegeResolver {
public static final NamedClusterPrivilege MONITOR_WATCHER = new ActionClusterPrivilege("monitor_watcher", MONITOR_WATCHER_PATTERN);
public static final NamedClusterPrivilege MONITOR_ROLLUP = new ActionClusterPrivilege("monitor_rollup", MONITOR_ROLLUP_PATTERN);
public static final NamedClusterPrivilege MONITOR_ENRICH = new ActionClusterPrivilege("monitor_enrich", MONITOR_ENRICH_PATTERN);
public static final NamedClusterPrivilege MONITOR_ESQL = new ActionClusterPrivilege("monitor_esql", MONITOR_ESQL_PATTERN);
public static final NamedClusterPrivilege MONITOR_STATS = new ActionClusterPrivilege("monitor_stats", MONITOR_STATS_PATTERN);
public static final NamedClusterPrivilege MANAGE = new ActionClusterPrivilege("manage", ALL_CLUSTER_PATTERN, ALL_SECURITY_PATTERN);
public static final NamedClusterPrivilege MANAGE_INFERENCE = new ActionClusterPrivilege("manage_inference", MANAGE_INFERENCE_PATTERN);
Expand Down Expand Up @@ -431,6 +433,7 @@ public class ClusterPrivilegeResolver {
MONITOR_WATCHER,
MONITOR_ROLLUP,
MONITOR_ENRICH,
MONITOR_ESQL,
MONITOR_STATS,
MANAGE,
MANAGE_CONNECTOR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ public AsyncTaskManagementService(
this.threadPool = threadPool;
}

public static String ASYNC_ACTION_SUFFIX = "[a]";

public void asyncExecute(
Request request,
TimeValue waitForCompletionTimeout,
Expand All @@ -182,7 +184,7 @@ public void asyncExecute(
String nodeId = clusterService.localNode().getId();
try (var ignored = threadPool.getThreadContext().newTraceContext()) {
@SuppressWarnings("unchecked")
T searchTask = (T) taskManager.register("transport", action + "[a]", new AsyncRequestWrapper(request, nodeId));
T searchTask = (T) taskManager.register("transport", action + ASYNC_ACTION_SUFFIX, new AsyncRequestWrapper(request, nodeId));
boolean operationStarted = false;
try {
operation.execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.core.CheckedConsumer;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.TimeValue;
Expand All @@ -40,7 +39,6 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -1395,15 +1393,11 @@ static Map<String, Object> removeAsyncProperties(Map<String, Object> map) {
}

protected static Map<String, Object> entityToMap(HttpEntity entity, XContentType expectedContentType) throws IOException {
try (InputStream content = entity.getContent()) {
XContentType xContentType = XContentType.fromMediaType(entity.getContentType().getValue());
assertEquals(expectedContentType, xContentType);
var map = XContentHelper.convertToMap(xContentType.xContent(), content, false);
if (shouldLog()) {
LOGGER.info("entity={}", map);
}
return map;
var result = EsqlTestUtils.entityToMap(entity, expectedContentType);
if (shouldLog()) {
LOGGER.info("entity={}", result);
}
return result;
}

static void addAsyncParameters(RequestObjectBuilder requestObject, boolean keepOnCompletion) throws IOException {
Expand Down Expand Up @@ -1535,21 +1529,18 @@ 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 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
Expand Up @@ -7,6 +7,7 @@

package org.elasticsearch.xpack.esql;

import org.apache.http.HttpEntity;
import org.apache.lucene.document.InetAddressPoint;
import org.apache.lucene.sandbox.document.HalfFloatPoint;
import org.apache.lucene.util.BytesRef;
Expand All @@ -20,6 +21,7 @@
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.compute.data.AggregateMetricDoubleBlockBuilder;
import org.elasticsearch.compute.data.BlockFactory;
import org.elasticsearch.compute.data.BlockUtils;
Expand All @@ -39,6 +41,7 @@
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.transport.RemoteTransportException;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xcontent.json.JsonXContent;
import org.elasticsearch.xpack.esql.action.EsqlQueryResponse;
import org.elasticsearch.xpack.esql.analysis.EnrichResolution;
Expand Down Expand Up @@ -858,4 +861,16 @@ public static <T> T singleValue(Collection<T> collection) {
assertThat(collection, hasSize(1));
return collection.iterator().next();
}

public static Map<String, Object> jsonEntityToMap(HttpEntity entity) throws IOException {
return entityToMap(entity, XContentType.JSON);
}

public static Map<String, Object> entityToMap(HttpEntity entity, XContentType expectedContentType) throws IOException {
try (InputStream content = entity.getContent()) {
XContentType xContentType = XContentType.fromMediaType(entity.getContentType().getValue());
assertEquals(expectedContentType, xContentType);
return XContentHelper.convertToMap(xContentType.xContent(), content, false /* ordered */);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.elasticsearch.xpack.core.async.DeleteAsyncResultRequest;
import org.elasticsearch.xpack.core.async.GetAsyncResultRequest;
import org.elasticsearch.xpack.core.async.TransportDeleteAsyncResultAction;
import org.elasticsearch.xpack.esql.core.async.AsyncTaskManagementService;
import org.elasticsearch.xpack.esql.plugin.QueryPragmas;
import org.hamcrest.core.IsEqual;

Expand Down Expand Up @@ -136,7 +137,11 @@ public void testGetAsyncWhileQueryTaskIsBeingCancelled() throws Exception {
.toList();
assertThat(tasks.size(), greaterThanOrEqualTo(1));
});
client().admin().cluster().prepareCancelTasks().setActions(EsqlQueryAction.NAME + "[a]").get();
client().admin()
.cluster()
.prepareCancelTasks()
.setActions(EsqlQueryAction.NAME + AsyncTaskManagementService.ASYNC_ACTION_SUFFIX)
.get();
assertBusy(() -> {
List<TaskInfo> tasks = getEsqlQueryTasks().stream().filter(TaskInfo::cancelled).toList();
assertThat(tasks, not(empty()));
Expand Down
Loading
Loading