Skip to content

Commit 2333f38

Browse files
authored
Add EQ|QL version support with its default value (#791)
1 parent fff5381 commit 2333f38

File tree

9 files changed

+228
-11
lines changed

9 files changed

+228
-11
lines changed

java-client/src/main-flavored/java/co/elastic/clients/elasticsearch/_helpers/esql/EsqlHelper.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import co.elastic.clients.elasticsearch._types.FieldValue;
2323
import co.elastic.clients.elasticsearch.esql.ElasticsearchEsqlAsyncClient;
2424
import co.elastic.clients.elasticsearch.esql.ElasticsearchEsqlClient;
25+
import co.elastic.clients.elasticsearch.esql.EsqlVersion;
2526
import co.elastic.clients.elasticsearch.esql.QueryRequest;
2627
import co.elastic.clients.json.JsonData;
2728
import co.elastic.clients.transport.endpoints.BinaryResponse;
@@ -36,9 +37,9 @@ public class EsqlHelper {
3637
//----- Synchronous
3738

3839
public static <T> T query(
39-
ElasticsearchEsqlClient client, EsqlAdapter<T> adapter, String query, Object... params
40+
ElasticsearchEsqlClient client, EsqlVersion version, EsqlAdapter<T> adapter, String query, Object... params
4041
) throws IOException {
41-
QueryRequest request = buildRequest(adapter, query, params);
42+
QueryRequest request = buildRequest(version, adapter, query, params);
4243
BinaryResponse response = client.query(request);
4344
return adapter.deserialize(client, request, response);
4445
}
@@ -52,9 +53,9 @@ public static <T> T query(ElasticsearchEsqlClient client, EsqlAdapter<T> adapter
5253
//----- Asynchronous
5354

5455
public static <T> CompletableFuture<T> queryAsync(
55-
ElasticsearchEsqlAsyncClient client, EsqlAdapter<T> adapter, String query, Object... params
56+
ElasticsearchEsqlAsyncClient client, EsqlVersion version, EsqlAdapter<T> adapter, String query, Object... params
5657
) {
57-
return doQueryAsync(client, adapter, buildRequest(adapter, query, params));
58+
return doQueryAsync(client, adapter, buildRequest(version, adapter, query, params));
5859
}
5960

6061
public static <T> CompletableFuture<T> queryAsync(
@@ -79,9 +80,19 @@ private static <T> CompletableFuture<T> doQueryAsync(
7980

8081
//----- Utilities
8182

82-
private static QueryRequest buildRequest(EsqlAdapter<?> adapter, String query, Object... params) {
83+
private static QueryRequest buildRequest(EsqlVersion version, EsqlAdapter<?> adapter, String query, Object... params) {
84+
if (version == null) {
85+
version = EsqlVersion.getDefault();
86+
}
87+
if (version == null) {
88+
throw new IllegalStateException(
89+
"ES|QL default version not set. Either specify it explicitly or set a default value");
90+
}
91+
EsqlVersion v = version;
92+
8393
return QueryRequest.of(esql -> esql
8494
.format(adapter.format())
95+
.version(v)
8596
.columnar(adapter.columnar())
8697
.query(query)
8798
.params(asFieldValues(params))
@@ -99,6 +110,7 @@ private static QueryRequest buildRequest(EsqlAdapter<?> adapter, QueryRequest re
99110
.locale(request.locale())
100111
.params(request.params())
101112
.query(request.query())
113+
.version(request.version())
102114
);
103115
}
104116

java-client/src/main/java/co/elastic/clients/elasticsearch/esql/ElasticsearchEsqlAsyncClient.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,24 @@ public final CompletableFuture<BinaryResponse> query(
111111
* values for query parameters, if any
112112
*/
113113
public final <T> CompletableFuture<T> query(EsqlAdapter<T> adapter, String query, Object... parameters) {
114-
return EsqlHelper.queryAsync(this, adapter, query, parameters);
114+
return EsqlHelper.queryAsync(this, null, adapter, query, parameters);
115+
}
116+
117+
/**
118+
* Executes an ES|QL request and adapts its result to a target type.
119+
*
120+
* @param version
121+
* the ES|QL language version
122+
* @param adapter
123+
* the ES|QL response adapter
124+
* @param query
125+
* the ES|QL query
126+
* @param parameters
127+
* values for query parameters, if any
128+
*/
129+
public final <T> CompletableFuture<T> query(EsqlVersion version, EsqlAdapter<T> adapter, String query,
130+
Object... parameters) {
131+
return EsqlHelper.queryAsync(this, version, adapter, query, parameters);
115132
}
116133

117134
/**

java-client/src/main/java/co/elastic/clients/elasticsearch/esql/ElasticsearchEsqlClient.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,24 @@ public final BinaryResponse query(Function<QueryRequest.Builder, ObjectBuilder<Q
113113
*/
114114
public final <T> T query(EsqlAdapter<T> adapter, String query, Object... parameters)
115115
throws IOException, ElasticsearchException {
116-
return EsqlHelper.query(this, adapter, query, parameters);
116+
return EsqlHelper.query(this, null, adapter, query, parameters);
117+
}
118+
119+
/**
120+
* Executes an ES|QL request and adapts its result to a target type.
121+
*
122+
* @param version
123+
* the ES|QL language version
124+
* @param adapter
125+
* the ES|QL response adapter
126+
* @param query
127+
* the ES|QL query
128+
* @param parameters
129+
* values for query parameters, if any
130+
*/
131+
public final <T> T query(EsqlVersion version, EsqlAdapter<T> adapter, String query, Object... parameters)
132+
throws IOException, ElasticsearchException {
133+
return EsqlHelper.query(this, version, adapter, query, parameters);
117134
}
118135

119136
/**
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package co.elastic.clients.elasticsearch.esql;
21+
22+
import co.elastic.clients.json.JsonEnum;
23+
import co.elastic.clients.json.JsonpDeserializable;
24+
import co.elastic.clients.json.JsonpDeserializer;
25+
26+
//----------------------------------------------------------------
27+
// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST.
28+
//----------------------------------------------------------------
29+
//
30+
// This code is generated from the Elasticsearch API specification
31+
// at https://github.com/elastic/elasticsearch-specification
32+
//
33+
// Manual updates to this file will be lost when the code is
34+
// re-generated.
35+
//
36+
// If you find a property that is missing or wrongly typed, please
37+
// open an issue or a PR on the API specification repository.
38+
//
39+
//----------------------------------------------------------------
40+
41+
/**
42+
*
43+
* @see <a href="../doc-files/api-spec.html#esql._types.EsqlVersion">API
44+
* specification</a>
45+
*/
46+
@JsonpDeserializable
47+
public enum EsqlVersion implements JsonEnum {
48+
/**
49+
* Run against the first version of ES|QL.
50+
*/
51+
V2024_04_01("2024.04.01"),
52+
53+
;
54+
55+
private final String jsonValue;
56+
57+
EsqlVersion(String jsonValue) {
58+
this.jsonValue = jsonValue;
59+
}
60+
61+
public String jsonValue() {
62+
return this.jsonValue;
63+
}
64+
65+
private static EsqlVersion DEFAULT_VERSION = V2024_04_01;
66+
67+
/**
68+
* The default ES|QL language version used when not explicitly specified.
69+
*/
70+
public static EsqlVersion getDefault() {
71+
return DEFAULT_VERSION;
72+
}
73+
74+
/**
75+
* Set the default ES|QL language version to be used. This is a global
76+
* application-wide setting.
77+
*/
78+
public static void setDefault(EsqlVersion version) {
79+
DEFAULT_VERSION = version;
80+
}
81+
82+
public static final JsonEnum.Deserializer<EsqlVersion> _DESERIALIZER = new JsonEnum.Deserializer<>(
83+
EsqlVersion.values());
84+
}

java-client/src/main/java/co/elastic/clients/elasticsearch/esql/QueryRequest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public class QueryRequest extends RequestBase implements JsonpSerializable {
9090

9191
private final String query;
9292

93+
private final EsqlVersion version;
94+
9395
// ---------------------------------------------------------------------------------------------
9496

9597
private QueryRequest(Builder builder) {
@@ -101,6 +103,7 @@ private QueryRequest(Builder builder) {
101103
this.locale = builder.locale;
102104
this.params = ApiTypeHelper.unmodifiable(builder.params);
103105
this.query = ApiTypeHelper.requireNonNull(builder.query, this, "query");
106+
this.version = ApiTypeHelper.requireNonNull(builder.version, this, "version");
104107

105108
}
106109

@@ -182,6 +185,16 @@ public final String query() {
182185
return this.query;
183186
}
184187

188+
/**
189+
* Required - The version of the ES|QL language in which the &quot;query&quot;
190+
* field was written.
191+
* <p>
192+
* API name: {@code version}
193+
*/
194+
public final EsqlVersion version() {
195+
return this.version;
196+
}
197+
185198
/**
186199
* Serialize this object to JSON.
187200
*/
@@ -221,6 +234,9 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
221234
generator.writeKey("query");
222235
generator.write(this.query);
223236

237+
generator.writeKey("version");
238+
this.version.serialize(generator, mapper);
239+
224240
}
225241

226242
// ---------------------------------------------------------------------------------------------
@@ -250,6 +266,8 @@ public static class Builder extends RequestBase.AbstractBuilder<Builder> impleme
250266

251267
private String query;
252268

269+
private EsqlVersion version;
270+
253271
/**
254272
* By default, ES|QL returns results as rows. For example, FROM returns each
255273
* individual document as one row. For the JSON, YAML, CBOR and smile formats,
@@ -365,6 +383,17 @@ public final Builder query(String value) {
365383
return this;
366384
}
367385

386+
/**
387+
* Required - The version of the ES|QL language in which the &quot;query&quot;
388+
* field was written.
389+
* <p>
390+
* API name: {@code version}
391+
*/
392+
public final Builder version(EsqlVersion value) {
393+
this.version = value;
394+
return this;
395+
}
396+
368397
@Override
369398
protected Builder self() {
370399
return this;
@@ -381,6 +410,10 @@ public QueryRequest build() {
381410

382411
return new QueryRequest(this);
383412
}
413+
{
414+
// Use the default ES|QL language version if not set explicitly
415+
this.version = EsqlVersion.getDefault();
416+
}
384417
}
385418

386419
// ---------------------------------------------------------------------------------------------
@@ -398,6 +431,7 @@ protected static void setupQueryRequestDeserializer(ObjectDeserializer<QueryRequ
398431
op.add(Builder::locale, JsonpDeserializer.stringDeserializer(), "locale");
399432
op.add(Builder::params, JsonpDeserializer.arrayDeserializer(FieldValue._DESERIALIZER), "params");
400433
op.add(Builder::query, JsonpDeserializer.stringDeserializer(), "query");
434+
op.add(Builder::version, EsqlVersion._DESERIALIZER, "version");
401435

402436
}
403437

java-client/src/test/java/co/elastic/clients/elasticsearch/ElasticsearchTestServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ public synchronized ElasticsearchTestServer start() {
117117
return this;
118118
}
119119

120-
Version version = Version.VERSION.major() < 8 ? new Version(7,17,5,false) : new Version(8,12,0,false);
120+
Version version = Version.VERSION.major() < 8 ? new Version(7,17,5,false) : new Version(8,14,0,false);
121121

122122
// Note we could use version.major() + "." + version.minor() + "-SNAPSHOT" but plugins won't install on a snapshot version
123-
String esImage = "docker.elastic.co/elasticsearch/elasticsearch:" + version;
123+
String esImage = "docker.elastic.co/elasticsearch/elasticsearch:" + version + "-SNAPSHOT";
124124

125125
DockerImageName image;
126126
if (plugins.length == 0) {

java-client/src/test/java/co/elastic/clients/elasticsearch/_helpers/esql/EsqlAdapterEndToEndTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void objectsTest() throws Exception {
134134
{
135135
EmpData emp = it.next();
136136
assertEquals("10042", emp.empNo);
137-
assertEquals(Arrays.asList("Architect", "Business Analyst", "Internship", "Junior Developer"), emp.jobPositions);
137+
assertEquals(Arrays.asList("Architect", "Business Analyst", "Junior Developer", "Internship"), emp.jobPositions);
138138

139139
assertEquals("1993-03-21T00:00:00Z[UTC]",
140140
DateTimeFormatter.ISO_DATE_TIME.format(emp.hireDate.toInstant().atZone(ZoneId.of("UTC")))
@@ -172,7 +172,7 @@ public void asyncObjects() throws Exception {
172172
{
173173
EmpData emp = it.next();
174174
assertEquals("10042", emp.empNo);
175-
assertEquals(Arrays.asList("Architect", "Business Analyst", "Internship", "Junior Developer"), emp.jobPositions);
175+
assertEquals(Arrays.asList("Architect", "Business Analyst", "Junior Developer", "Internship"), emp.jobPositions);
176176

177177
assertEquals("1993-03-21T00:00:00Z[UTC]",
178178
DateTimeFormatter.ISO_DATE_TIME.format(emp.hireDate.toInstant().atZone(ZoneId.of("UTC")))
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package co.elastic.clients.elasticsearch._helpers.esql;
21+
22+
import co.elastic.clients.elasticsearch.esql.EsqlVersion;
23+
import co.elastic.clients.elasticsearch.esql.QueryRequest;
24+
import co.elastic.clients.util.MissingRequiredPropertyException;
25+
import org.junit.jupiter.api.Assertions;
26+
import org.junit.jupiter.api.Test;
27+
28+
public class EsqlVersionTest extends Assertions {
29+
30+
@Test
31+
public void testDefaultVersion() {
32+
33+
// If version is missing, the default one is used.
34+
QueryRequest r = QueryRequest.of(q -> q
35+
.query("foo")
36+
);
37+
38+
assertEquals(EsqlVersion.getDefault(), r.version());
39+
}
40+
41+
@Test
42+
public void testSetVersionToNull() {
43+
44+
assertThrows(MissingRequiredPropertyException.class, () -> {
45+
// If version is explicitly set to null, the default isn't used
46+
QueryRequest r = QueryRequest.of(q -> q
47+
.version(null)
48+
.query("foo")
49+
);
50+
});
51+
}
52+
}

java-client/src/test/java/co/elastic/clients/elasticsearch/spec_issues/SpecIssuesTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public void i0254_suggesterTest() throws Exception {
164164
}
165165

166166
@Test
167+
@Disabled("Plugins cannot be installed on snapshot versions of ES")
167168
public void i0249_variantKind() throws Exception {
168169
try (ElasticsearchTestServer server = new ElasticsearchTestServer("analysis-icu").start()) {
169170

0 commit comments

Comments
 (0)