Skip to content

Commit a59a00a

Browse files
Mpdreamzrusscam
authored andcommitted
Add support for the SQL APIs (#3414)
* Add QuerySql API methods and tests * Add ClearSqlCursor API methods and tests * Add TranslateSql API methods and tests
1 parent 47997f3 commit a59a00a

29 files changed

+961
-10
lines changed

src/CodeGeneration/ApiGenerator/ApiGenerator.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ public static void Generate(string downloadBranch, params string[] folders)
8080
"xpack.rollup.rollup_search.json",
8181
"xpack.rollup.start_job.json",
8282
"xpack.rollup.stop_job.json",
83-
"xpack.sql.clear_cursor.json",
84-
"xpack.sql.query.json",
85-
"xpack.sql.translate.json",
83+
8684
"xpack.ssl.certificates.json",
8785

8886
// 6.4 new API's

src/CodeGeneration/ApiGenerator/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ static void Main(string[] args)
4141
if (redownloadCoreSpecification)
4242
RestSpecDownloader.Download(downloadBranch);
4343

44-
ApiGenerator.Generate(downloadBranch, "Core", "Graph", "License", "Security", "Watcher", "Info", "MachineLearning", "Migration");
44+
ApiGenerator.Generate(downloadBranch, "Core", "Graph", "License", "Security", "Watcher", "Info",
45+
"MachineLearning", "Migration", "Sql");
4546
}
4647
}
4748
}

src/Elasticsearch.Net/Domain/RequestParameters/RequestParameters.Generated.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,6 +2563,23 @@ public partial class PutUserRequestParameters : RequestParameters<PutUserRequest
25632563
///</summary>
25642564
public Refresh? Refresh { get => Q<Refresh?>("refresh"); set => Q("refresh", value); }
25652565
}
2566+
///<summary>Request options for XpackSqlClearCursor<pre>Clear SQL cursor</pre></summary>
2567+
public partial class ClearSqlCursorRequestParameters : RequestParameters<ClearSqlCursorRequestParameters>
2568+
{
2569+
public override HttpMethod DefaultHttpMethod => HttpMethod.POST;
2570+
}
2571+
///<summary>Request options for XpackSqlQuery<pre>Execute SQL</pre></summary>
2572+
public partial class QuerySqlRequestParameters : RequestParameters<QuerySqlRequestParameters>
2573+
{
2574+
public override HttpMethod DefaultHttpMethod => HttpMethod.POST;
2575+
///<summary>a short version of the Accept header, e.g. json, yaml</summary>
2576+
public string Format { get => Q<string>("format"); set => Q("format", value); }
2577+
}
2578+
///<summary>Request options for XpackSqlTranslate<pre>Translate SQL into Elasticsearch queries</pre></summary>
2579+
public partial class TranslateSqlRequestParameters : RequestParameters<TranslateSqlRequestParameters>
2580+
{
2581+
public override HttpMethod DefaultHttpMethod => HttpMethod.POST;
2582+
}
25662583
///<summary>Request options for XpackWatcherAckWatch<pre>http://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-ack-watch.html</pre></summary>
25672584
public partial class AcknowledgeWatchRequestParameters : RequestParameters<AcknowledgeWatchRequestParameters>
25682585
{

src/Elasticsearch.Net/ElasticLowLevelClient.Generated.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3830,6 +3830,52 @@ public TResponse XpackSecurityPutUserPost<TResponse>(string username, PostData b
38303830
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
38313831
public Task<TResponse> XpackSecurityPutUserPostAsync<TResponse>(string username, PostData body, PutUserRequestParameters requestParameters = null, CancellationToken ctx = default(CancellationToken))
38323832
where TResponse : class, IElasticsearchResponse, new() => this.DoRequestAsync<TResponse>(POST, Url($"_xpack/security/user/{username.NotNull("username")}"), ctx, body, _params(requestParameters));
3833+
///<summary>POST on /_xpack/sql/close <para>Clear SQL cursor</para></summary>
3834+
///<param name="body">Specify the cursor value in the `cursor` element to clean the cursor.</param>
3835+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3836+
public TResponse XpackSqlClearCursor<TResponse>(PostData body, ClearSqlCursorRequestParameters requestParameters = null)
3837+
where TResponse : class, IElasticsearchResponse, new() => this.DoRequest<TResponse>(POST, Url($"_xpack/sql/close"), body, _params(requestParameters));
3838+
///<summary>POST on /_xpack/sql/close <para>Clear SQL cursor</para></summary>
3839+
///<param name="body">Specify the cursor value in the `cursor` element to clean the cursor.</param>
3840+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3841+
public Task<TResponse> XpackSqlClearCursorAsync<TResponse>(PostData body, ClearSqlCursorRequestParameters requestParameters = null, CancellationToken ctx = default(CancellationToken))
3842+
where TResponse : class, IElasticsearchResponse, new() => this.DoRequestAsync<TResponse>(POST, Url($"_xpack/sql/close"), ctx, body, _params(requestParameters));
3843+
///<summary>POST on /_xpack/sql <para>Execute SQL</para></summary>
3844+
///<param name="body">Use the `query` element to start a query. Use the `cursor` element to continue a query.</param>
3845+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3846+
public TResponse XpackSqlQuery<TResponse>(PostData body, QuerySqlRequestParameters requestParameters = null)
3847+
where TResponse : class, IElasticsearchResponse, new() => this.DoRequest<TResponse>(POST, Url($"_xpack/sql"), body, _params(requestParameters));
3848+
///<summary>POST on /_xpack/sql <para>Execute SQL</para></summary>
3849+
///<param name="body">Use the `query` element to start a query. Use the `cursor` element to continue a query.</param>
3850+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3851+
public Task<TResponse> XpackSqlQueryAsync<TResponse>(PostData body, QuerySqlRequestParameters requestParameters = null, CancellationToken ctx = default(CancellationToken))
3852+
where TResponse : class, IElasticsearchResponse, new() => this.DoRequestAsync<TResponse>(POST, Url($"_xpack/sql"), ctx, body, _params(requestParameters));
3853+
///<summary>GET on /_xpack/sql <para>Execute SQL</para></summary>
3854+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3855+
public TResponse XpackSqlQueryGet<TResponse>(QuerySqlRequestParameters requestParameters = null)
3856+
where TResponse : class, IElasticsearchResponse, new() => this.DoRequest<TResponse>(GET, Url($"_xpack/sql"), null, _params(requestParameters));
3857+
///<summary>GET on /_xpack/sql <para>Execute SQL</para></summary>
3858+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3859+
public Task<TResponse> XpackSqlQueryGetAsync<TResponse>(QuerySqlRequestParameters requestParameters = null, CancellationToken ctx = default(CancellationToken))
3860+
where TResponse : class, IElasticsearchResponse, new() => this.DoRequestAsync<TResponse>(GET, Url($"_xpack/sql"), ctx, null, _params(requestParameters));
3861+
///<summary>POST on /_xpack/sql/translate <para>Translate SQL into Elasticsearch queries</para></summary>
3862+
///<param name="body">Specify the query in the `query` element.</param>
3863+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3864+
public TResponse XpackSqlTranslate<TResponse>(PostData body, TranslateSqlRequestParameters requestParameters = null)
3865+
where TResponse : class, IElasticsearchResponse, new() => this.DoRequest<TResponse>(POST, Url($"_xpack/sql/translate"), body, _params(requestParameters));
3866+
///<summary>POST on /_xpack/sql/translate <para>Translate SQL into Elasticsearch queries</para></summary>
3867+
///<param name="body">Specify the query in the `query` element.</param>
3868+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3869+
public Task<TResponse> XpackSqlTranslateAsync<TResponse>(PostData body, TranslateSqlRequestParameters requestParameters = null, CancellationToken ctx = default(CancellationToken))
3870+
where TResponse : class, IElasticsearchResponse, new() => this.DoRequestAsync<TResponse>(POST, Url($"_xpack/sql/translate"), ctx, body, _params(requestParameters));
3871+
///<summary>GET on /_xpack/sql/translate <para>Translate SQL into Elasticsearch queries</para></summary>
3872+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3873+
public TResponse XpackSqlTranslateGet<TResponse>(TranslateSqlRequestParameters requestParameters = null)
3874+
where TResponse : class, IElasticsearchResponse, new() => this.DoRequest<TResponse>(GET, Url($"_xpack/sql/translate"), null, _params(requestParameters));
3875+
///<summary>GET on /_xpack/sql/translate <para>Translate SQL into Elasticsearch queries</para></summary>
3876+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3877+
public Task<TResponse> XpackSqlTranslateGetAsync<TResponse>(TranslateSqlRequestParameters requestParameters = null, CancellationToken ctx = default(CancellationToken))
3878+
where TResponse : class, IElasticsearchResponse, new() => this.DoRequestAsync<TResponse>(GET, Url($"_xpack/sql/translate"), ctx, null, _params(requestParameters));
38333879
///<summary>PUT on /_xpack/watcher/watch/{watch_id}/_ack <para>http://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-ack-watch.html</para></summary>
38343880
///<param name="watch_id">Watch ID</param>
38353881
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>

src/Elasticsearch.Net/IElasticLowLevelClient.Generated.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3104,6 +3104,42 @@ public partial interface IElasticLowLevelClient
31043104
///<param name="body">The user to add</param>
31053105
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
31063106
Task<TResponse> XpackSecurityPutUserPostAsync<TResponse>(string username, PostData body, PutUserRequestParameters requestParameters = null, CancellationToken ctx = default(CancellationToken)) where TResponse : class, IElasticsearchResponse, new();
3107+
///<summary>POST on /_xpack/sql/close <para>Clear SQL cursor</para></summary>
3108+
///<param name="body">Specify the cursor value in the `cursor` element to clean the cursor.</param>
3109+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3110+
TResponse XpackSqlClearCursor<TResponse>(PostData body, ClearSqlCursorRequestParameters requestParameters = null) where TResponse : class, IElasticsearchResponse, new();
3111+
///<summary>POST on /_xpack/sql/close <para>Clear SQL cursor</para></summary>
3112+
///<param name="body">Specify the cursor value in the `cursor` element to clean the cursor.</param>
3113+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3114+
Task<TResponse> XpackSqlClearCursorAsync<TResponse>(PostData body, ClearSqlCursorRequestParameters requestParameters = null, CancellationToken ctx = default(CancellationToken)) where TResponse : class, IElasticsearchResponse, new();
3115+
///<summary>POST on /_xpack/sql <para>Execute SQL</para></summary>
3116+
///<param name="body">Use the `query` element to start a query. Use the `cursor` element to continue a query.</param>
3117+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3118+
TResponse XpackSqlQuery<TResponse>(PostData body, QuerySqlRequestParameters requestParameters = null) where TResponse : class, IElasticsearchResponse, new();
3119+
///<summary>POST on /_xpack/sql <para>Execute SQL</para></summary>
3120+
///<param name="body">Use the `query` element to start a query. Use the `cursor` element to continue a query.</param>
3121+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3122+
Task<TResponse> XpackSqlQueryAsync<TResponse>(PostData body, QuerySqlRequestParameters requestParameters = null, CancellationToken ctx = default(CancellationToken)) where TResponse : class, IElasticsearchResponse, new();
3123+
///<summary>GET on /_xpack/sql <para>Execute SQL</para></summary>
3124+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3125+
TResponse XpackSqlQueryGet<TResponse>(QuerySqlRequestParameters requestParameters = null) where TResponse : class, IElasticsearchResponse, new();
3126+
///<summary>GET on /_xpack/sql <para>Execute SQL</para></summary>
3127+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3128+
Task<TResponse> XpackSqlQueryGetAsync<TResponse>(QuerySqlRequestParameters requestParameters = null, CancellationToken ctx = default(CancellationToken)) where TResponse : class, IElasticsearchResponse, new();
3129+
///<summary>POST on /_xpack/sql/translate <para>Translate SQL into Elasticsearch queries</para></summary>
3130+
///<param name="body">Specify the query in the `query` element.</param>
3131+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3132+
TResponse XpackSqlTranslate<TResponse>(PostData body, TranslateSqlRequestParameters requestParameters = null) where TResponse : class, IElasticsearchResponse, new();
3133+
///<summary>POST on /_xpack/sql/translate <para>Translate SQL into Elasticsearch queries</para></summary>
3134+
///<param name="body">Specify the query in the `query` element.</param>
3135+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3136+
Task<TResponse> XpackSqlTranslateAsync<TResponse>(PostData body, TranslateSqlRequestParameters requestParameters = null, CancellationToken ctx = default(CancellationToken)) where TResponse : class, IElasticsearchResponse, new();
3137+
///<summary>GET on /_xpack/sql/translate <para>Translate SQL into Elasticsearch queries</para></summary>
3138+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3139+
TResponse XpackSqlTranslateGet<TResponse>(TranslateSqlRequestParameters requestParameters = null) where TResponse : class, IElasticsearchResponse, new();
3140+
///<summary>GET on /_xpack/sql/translate <para>Translate SQL into Elasticsearch queries</para></summary>
3141+
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>
3142+
Task<TResponse> XpackSqlTranslateGetAsync<TResponse>(TranslateSqlRequestParameters requestParameters = null, CancellationToken ctx = default(CancellationToken)) where TResponse : class, IElasticsearchResponse, new();
31073143
///<summary>PUT on /_xpack/watcher/watch/{watch_id}/_ack <para>http://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-ack-watch.html</para></summary>
31083144
///<param name="watch_id">Watch ID</param>
31093145
///<param name="requestParameters">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>

src/Nest/CommonAbstractions/Infer/Fields/FieldsJsonConverter.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using Elasticsearch.Net;
34
using Newtonsoft.Json;
45

56
namespace Nest
@@ -16,22 +17,42 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
1617
{
1718
var fields = value as Fields;
1819
writer.WriteStartArray();
19-
var infer = serializer.GetConnectionSettings().Inferrer;
20-
foreach (var f in fields?.ListOfFields ?? Enumerable.Empty<Field>())
20+
if (fields != null)
2121
{
22-
writer.WriteValue(infer.Field(f));
22+
var infer = serializer.GetConnectionSettings().Inferrer;
23+
foreach (var f in fields.ListOfFields)
24+
{
25+
writer.WriteValue(infer.Field(f));
26+
}
2327
}
2428
writer.WriteEndArray();
2529
}
30+
2631
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
2732
{
2833
if (reader.TokenType != JsonToken.StartArray) return null;
2934
var fields = new Fields();
3035
while (reader.TokenType != JsonToken.EndArray)
3136
{
32-
var field = reader.ReadAsString();
33-
if (reader.TokenType == JsonToken.String)
34-
fields.And(field);
37+
// as per https://github.com/elastic/elasticsearch/pull/29639 this can now be an array of objects
38+
reader.Read();
39+
switch (reader.TokenType)
40+
{
41+
case JsonToken.String:
42+
fields.And((string)reader.Value);
43+
break;
44+
case JsonToken.StartObject:
45+
/// TODO 6.4 this is temporary until we add proper support for doc_values format
46+
reader.Read(); // "field";
47+
var field = reader.ReadAsString();
48+
fields.And(field);
49+
while (reader.TokenType != JsonToken.EndObject)
50+
{
51+
reader.Read();
52+
}
53+
reader.Read(); // "}";
54+
break;
55+
}
3556
}
3657
return fields;
3758
}

src/Nest/CommonAbstractions/LazyDocument/LazyDocumentJsonConverter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
2727

2828
public override bool CanConvert(Type objectType) => true;
2929
}
30+
3031
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Nest
4+
{
5+
[MapsApi("xpack.sql.clear_cursor.json")]
6+
[ContractJsonConverter(typeof(ReadAsTypeJsonConverter<ClearSqlCursorRequest>))]
7+
public partial interface IClearSqlCursorRequest
8+
{
9+
/// <summary>
10+
/// <para>You’ve reached the last page when there is no cursor returned in the results. Like Elasticsearch’s scroll,
11+
/// SQL may keep state in Elasticsearch to support the cursor.
12+
/// Unlike scroll, receiving the last page is enough to guarantee that the Elasticsearch state is cleared.
13+
/// </para>
14+
/// </summary>
15+
[JsonProperty("cursor")]
16+
string Cursor { get; set; }
17+
}
18+
19+
public partial class ClearSqlCursorRequest
20+
{
21+
/// <inheritdoc cref="IQuerySqlRequest.Cursor"/>>
22+
public string Cursor { get; set; }
23+
}
24+
25+
public partial class ClearSqlCursorDescriptor
26+
{
27+
string IClearSqlCursorRequest.Cursor { get; set; }
28+
29+
/// <inheritdoc cref="IQuerySqlRequest.Cursor"/>>
30+
public ClearSqlCursorDescriptor Cursor(string cursor) => Assign(a => a.Cursor = cursor);
31+
}
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Newtonsoft.Json;
4+
5+
namespace Nest
6+
{
7+
public interface IClearSqlCursorResponse : IResponse
8+
{
9+
[JsonProperty("succeeded")]
10+
bool Succeeded { get; }
11+
}
12+
13+
public class ClearSqlCursorResponse : ResponseBase, IClearSqlCursorResponse
14+
{
15+
public bool Succeeded { get; internal set; }
16+
}
17+
}

0 commit comments

Comments
 (0)