-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix #3320 Add Painless script execute API #3370
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) | ||
; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing
Context
?https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-execute-api.html
Can be added later though, as there is only one context at this point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, just seen the commit message 😄