-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Generate ExcecutePainlessScript methods/descriptors etc * Implement ExecutePainlessScript API with tests Omitting `context` for now since that only takes a single option now which is also the default. Waiting for the API to crystalize here. * make Result generic in preparation for 6.4
- Loading branch information
Showing
12 changed files
with
274 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/Nest/Modules/Scripting/ExecutePainlessScript/ElasticClient-ExecutePainlessScript.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Elasticsearch.Net; | ||
using System.Threading; | ||
|
||
namespace Nest | ||
{ | ||
public partial interface IElasticClient | ||
{ | ||
/// <summary> | ||
/// Executes an arbitrary Painless script and returns a result. | ||
/// Useful for testing the syntactical correctness of Painless scripts | ||
/// </summary> | ||
IExecutePainlessScriptResponse<TResult> ExecutePainlessScript<TResult>(Func<ExecutePainlessScriptDescriptor, IExecutePainlessScriptRequest> selector); | ||
|
||
/// <inheritdoc cref="ExecutePainlessScript{TResult}(System.Func{Nest.ExecutePainlessScriptDescriptor,Nest.IExecutePainlessScriptRequest})"/> | ||
IExecutePainlessScriptResponse<TResult> ExecutePainlessScript<TResult>(IExecutePainlessScriptRequest request); | ||
|
||
/// <inheritdoc cref="ExecutePainlessScript{TResult}(System.Func{Nest.ExecutePainlessScriptDescriptor,Nest.IExecutePainlessScriptRequest})"/> | ||
Task<IExecutePainlessScriptResponse<TResult>> ExecutePainlessScriptAsync<TResult>(Func<ExecutePainlessScriptDescriptor, IExecutePainlessScriptRequest> selector, | ||
CancellationToken cancellationToken = default(CancellationToken)); | ||
|
||
/// <inheritdoc cref="ExecutePainlessScript{TResult}(System.Func{Nest.ExecutePainlessScriptDescriptor,Nest.IExecutePainlessScriptRequest})"/> | ||
Task<IExecutePainlessScriptResponse<TResult>> ExecutePainlessScriptAsync<TResult>(IExecutePainlessScriptRequest request, CancellationToken cancellationToken = default(CancellationToken)); | ||
|
||
} | ||
|
||
public partial class ElasticClient | ||
{ | ||
/// <inheritdoc /> | ||
public IExecutePainlessScriptResponse<TResult> ExecutePainlessScript<TResult>(Func<ExecutePainlessScriptDescriptor, IExecutePainlessScriptRequest> selector) => | ||
this.ExecutePainlessScript<TResult>(selector?.Invoke(new ExecutePainlessScriptDescriptor())); | ||
|
||
/// <inheritdoc /> | ||
public IExecutePainlessScriptResponse<TResult> ExecutePainlessScript<TResult>(IExecutePainlessScriptRequest request) => | ||
this.Dispatcher.Dispatch<IExecutePainlessScriptRequest, ExecutePainlessScriptRequestParameters, ExecutePainlessScriptResponse<TResult>>( | ||
request, | ||
this.LowLevelDispatch.ScriptsPainlessExecuteDispatch<ExecutePainlessScriptResponse<TResult>> | ||
); | ||
|
||
/// <inheritdoc /> | ||
public Task<IExecutePainlessScriptResponse<TResult>> ExecutePainlessScriptAsync<TResult>(Func<ExecutePainlessScriptDescriptor, IExecutePainlessScriptRequest> selector, | ||
CancellationToken cancellationToken = default(CancellationToken)) => | ||
this.ExecutePainlessScriptAsync<TResult>(selector?.Invoke(new ExecutePainlessScriptDescriptor()), cancellationToken); | ||
|
||
/// <inheritdoc /> | ||
public Task<IExecutePainlessScriptResponse<TResult>> ExecutePainlessScriptAsync<TResult>(IExecutePainlessScriptRequest request, CancellationToken cancellationToken = default(CancellationToken)) => | ||
this.Dispatcher.DispatchAsync<IExecutePainlessScriptRequest, ExecutePainlessScriptRequestParameters, ExecutePainlessScriptResponse<TResult>, IExecutePainlessScriptResponse<TResult>>( | ||
request, | ||
cancellationToken, | ||
this.LowLevelDispatch.ScriptsPainlessExecuteDispatchAsync<ExecutePainlessScriptResponse<TResult>> | ||
); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Nest/Modules/Scripting/ExecutePainlessScript/ExecutePainlessScriptRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
|
||
namespace Nest | ||
{ | ||
public partial interface IExecutePainlessScriptRequest | ||
{ | ||
[JsonProperty("script")] | ||
IInlineScript Script { get; set; } | ||
} | ||
|
||
public partial class ExecutePainlessScriptRequest | ||
{ | ||
public IInlineScript Script { get; set; } | ||
} | ||
|
||
[DescriptorFor("ScriptsPainlessExecute")] | ||
public partial class ExecutePainlessScriptDescriptor | ||
{ | ||
IInlineScript IExecutePainlessScriptRequest.Script { get; set; } | ||
|
||
public ExecutePainlessScriptDescriptor Script(Func<InlineScriptDescriptor, IInlineScript> selector) => | ||
Assign(a => a.Script = selector?.Invoke(new InlineScriptDescriptor())); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Nest/Modules/Scripting/ExecutePainlessScript/ExecutePainlessScriptResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Nest | ||
{ | ||
public interface IExecutePainlessScriptResponse<TResult> : IResponse | ||
{ | ||
[JsonProperty("result")] | ||
TResult Result { get; } | ||
} | ||
|
||
public class ExecutePainlessScriptResponse<TResult> : ResponseBase, IExecutePainlessScriptResponse<TResult> | ||
{ | ||
public TResult Result { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/Tests/Tests/Modules/Scripting/ExecutePainlessScript/ExecutePainlessScriptApiTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Elastic.Xunit.XunitPlumbing; | ||
using Elasticsearch.Net; | ||
using FluentAssertions; | ||
using Nest; | ||
using Tests.Core.Extensions; | ||
using Tests.Core.ManagedElasticsearch.Clusters; | ||
using Tests.Framework; | ||
using Tests.Framework.Integration; | ||
using Tests.Framework.ManagedElasticsearch.Clusters; | ||
using Xunit; | ||
|
||
namespace Tests.Modules.Scripting.ExecutePainlessScript | ||
{ | ||
[SkipVersion("<6.3.0", "this API was introduced in 6.3.0")] | ||
public class ExecutePainlessScriptApiTests | ||
: ApiIntegrationTestBase<ReadOnlyCluster, IExecutePainlessScriptResponse<string>, IExecutePainlessScriptRequest, ExecutePainlessScriptDescriptor, ExecutePainlessScriptRequest> | ||
{ | ||
public ExecutePainlessScriptApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } | ||
|
||
private static readonly string _painlessScript = "params.count / params.total"; | ||
|
||
protected override LazyResponses ClientUsage() => Calls( | ||
fluent: (client, f) => client.ExecutePainlessScript<string>(f), | ||
fluentAsync: (client, f) => client.ExecutePainlessScriptAsync<string>(f), | ||
request: (client, r) => client.ExecutePainlessScript<string>(r), | ||
requestAsync: (client, r) => client.ExecutePainlessScriptAsync<string>(r) | ||
); | ||
|
||
protected override HttpMethod HttpMethod => HttpMethod.POST; | ||
protected override string UrlPath => "/_scripts/painless/_execute"; | ||
protected override int ExpectStatusCode => 200; | ||
protected override bool ExpectIsValid => true; | ||
|
||
protected override bool SupportsDeserialization => false; | ||
|
||
protected override object ExpectJson => new | ||
{ | ||
script = new | ||
{ | ||
source = _painlessScript, | ||
@params = new { count = 100.0, total = 1000.0 } | ||
}, | ||
}; | ||
|
||
protected override Func<ExecutePainlessScriptDescriptor, IExecutePainlessScriptRequest> Fluent => d => d | ||
.Script(s=>s | ||
.Source(_painlessScript) | ||
.Params(p => p.Add("count", 100.0).Add("total", 1000.0)) | ||
); | ||
|
||
protected override ExecutePainlessScriptRequest Initializer => new ExecutePainlessScriptRequest | ||
{ | ||
Script = new InlineScript(_painlessScript) | ||
{ | ||
Params = new Dictionary<string, object> | ||
{ | ||
{ "count", 100.0 }, | ||
{ "total", 1000.0 }, | ||
} | ||
} | ||
}; | ||
|
||
protected override void ExpectResponse(IExecutePainlessScriptResponse<string> response) | ||
{ | ||
response.ShouldBeValid(); | ||
response.Result.Should().NotBeNullOrWhiteSpace(); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Tests/Tests/Modules/Scripting/ExecutePainlessScript/ExecutePainlessScriptUrlTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Threading.Tasks; | ||
using Elastic.Xunit.XunitPlumbing; | ||
using Nest; | ||
using Tests.Framework; | ||
using static Tests.Framework.UrlTester; | ||
|
||
namespace Tests.Modules.Scripting.ExecutePainlessScript | ||
{ | ||
public class ExecutePainlessScriptUrlTests | ||
{ | ||
[U] public async Task Urls() | ||
{ | ||
var painless = "1 + 1"; | ||
var request = new ExecutePainlessScriptRequest | ||
{ | ||
Script = new InlineScript(painless) | ||
}; | ||
|
||
await POST("/_scripts/painless/_execute") | ||
.Fluent(c => c.ExecutePainlessScript<string>(f => f.Script(s => s.Source(painless)))) | ||
.Request(c => c.ExecutePainlessScript<string>(request)) | ||
.FluentAsync(c => c.ExecutePainlessScriptAsync<string>(f => f.Script(s => s.Source(painless)))) | ||
.RequestAsync(c => c.ExecutePainlessScriptAsync<string>(request)) | ||
; | ||
} | ||
} | ||
} |