From 7f73c4f5059c1bd5460b66019d0b9a72e5cc30f1 Mon Sep 17 00:00:00 2001 From: Stuart Cam Date: Wed, 13 Dec 2017 17:37:16 +1100 Subject: [PATCH 1/3] 6.0 Script changes - Groovy, JavaScript, and Python languages removededit The Groovy, JavaScript, and Python scripting languages were deprecated in elasticsearch 5.0 and have now been removed. Use painless instead. - lang can no longer be specified when using a stored script as part of a requestedit The lang variable can no longer be specified as part of a request that uses a stored script otherwise an error will occur. Note that a request using a stored script is different from a request that puts a stored script. The language of the script has already been stored as part of the cluster state and an id is sufficient to access all of the information necessary to execute a stored script. - lang can no longer be used when putting, getting, or deleting a stored scriptedit Stored scripts can no longer have the lang parameter specified as part of the url when performing PUT, GET, and DELETE actions on the _scripts/ path. All stored scripts must have a unique id as the namespace is only id now and no longer lang and id. --- src/Nest/Modules/Scripting/IStoredScript.cs | 43 ++----------------- .../Scripting/PutScript/PutScriptRequest.cs | 5 --- .../Scripting/PutScript/PutScriptApiTests.cs | 2 +- 3 files changed, 4 insertions(+), 46 deletions(-) diff --git a/src/Nest/Modules/Scripting/IStoredScript.cs b/src/Nest/Modules/Scripting/IStoredScript.cs index d5c0fdbce54..406f5dfc835 100644 --- a/src/Nest/Modules/Scripting/IStoredScript.cs +++ b/src/Nest/Modules/Scripting/IStoredScript.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; -using Elasticsearch.Net; -using Newtonsoft.Json; +using Newtonsoft.Json; namespace Nest { @@ -8,67 +6,32 @@ namespace Nest [JsonConverter(typeof(ReadAsTypeJsonConverter))] public interface IStoredScript { - [JsonProperty("lang")] - string Lang { get; set; } - [JsonProperty("source")] string Source { get; set; } } public class StoredScript : IStoredScript { - [JsonProperty("lang")] - string IStoredScript.Lang { get; set; } [JsonProperty("source")] string IStoredScript.Source { get; set; } //used for deserialization internal StoredScript() { } - protected StoredScript(string lang, string source) + protected StoredScript(string source) { - ((IStoredScript) this).Lang = lang; ((IStoredScript) this).Source = source; } } public class PainlessScript : StoredScript { - private static readonly string Lang = ScriptLang.Painless.GetStringValue(); - public PainlessScript(string source) : base(Lang, source) { } - } - public class GroovyScript : StoredScript - { - private static readonly string Lang = ScriptLang.Groovy.GetStringValue(); - public GroovyScript(string source) : base(Lang, source) { } - } - public class JavaScriptScript : StoredScript - { - private static readonly string Lang = ScriptLang.JS.GetStringValue(); - public JavaScriptScript(string source) : base(Lang, source) { } - } - public class PythonScript : StoredScript - { - private static readonly string Lang = ScriptLang.Python.GetStringValue(); - public PythonScript(string source) : base(Lang, source) { } - } - public class LuceneExpressionScript : StoredScript - { - private static readonly string Lang = ScriptLang.Expression.GetStringValue(); - public LuceneExpressionScript(string source) : base(Lang, source) { } - } - public class MustacheScript : StoredScript - { - private static readonly string Lang = ScriptLang.Mustache.GetStringValue(); - public MustacheScript(string source) : base(Lang, source) { } + public PainlessScript(string source) : base(source) { } } public class StoredScriptDescriptor : DescriptorBase, IStoredScript { - string IStoredScript.Lang { get; set; } string IStoredScript.Source { get; set; } - public StoredScriptDescriptor Lang(string lang) => Assign(a => a.Lang = lang); - public StoredScriptDescriptor Source(string source) => Assign(a => a.Source = source); } } diff --git a/src/Nest/Modules/Scripting/PutScript/PutScriptRequest.cs b/src/Nest/Modules/Scripting/PutScript/PutScriptRequest.cs index cb764fa906e..742d959c7ce 100644 --- a/src/Nest/Modules/Scripting/PutScript/PutScriptRequest.cs +++ b/src/Nest/Modules/Scripting/PutScript/PutScriptRequest.cs @@ -23,10 +23,5 @@ public PutScriptDescriptor Script(Func se Assign(a => a.Script = selector?.Invoke(new StoredScriptDescriptor())); public PutScriptDescriptor Painless(string source) => Assign(a => a.Script = new PainlessScript(source)); - public PutScriptDescriptor Groovy(string source) => Assign(a => a.Script = new GroovyScript(source)); - public PutScriptDescriptor JavaScript(string source) => Assign(a => a.Script = new JavaScriptScript(source)); - public PutScriptDescriptor Python(string source) => Assign(a => a.Script = new PythonScript(source)); - public PutScriptDescriptor LuceneExpression(string source) => Assign(a => a.Script = new LuceneExpressionScript(source)); - public PutScriptDescriptor Mustache(string source) => Assign(a => a.Script = new MustacheScript(source)); } } diff --git a/src/Tests/Modules/Scripting/PutScript/PutScriptApiTests.cs b/src/Tests/Modules/Scripting/PutScript/PutScriptApiTests.cs index 5201cd7ac8e..937f11b5773 100644 --- a/src/Tests/Modules/Scripting/PutScript/PutScriptApiTests.cs +++ b/src/Tests/Modules/Scripting/PutScript/PutScriptApiTests.cs @@ -30,7 +30,7 @@ protected override LazyResponses ClientUsage() => Calls( protected override object ExpectJson { get; } = new { - script = new { lang = "painless", source = "1+1" } + script = new { source = "1+1" } }; protected override PutScriptDescriptor NewDescriptor() => new PutScriptDescriptor(_name); From 09d53501c1f8b2d1527ee08208909c69fb3db8f7 Mon Sep 17 00:00:00 2001 From: Stuart Cam Date: Thu, 14 Dec 2017 11:53:28 +1100 Subject: [PATCH 2/3] Bring bak mustache and lucene expression templates --- src/Nest/Modules/Scripting/IStoredScript.cs | 39 +++++++++++++++++-- .../Scripting/PutScript/PutScriptRequest.cs | 2 + 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/Nest/Modules/Scripting/IStoredScript.cs b/src/Nest/Modules/Scripting/IStoredScript.cs index 406f5dfc835..34bd1a16c1f 100644 --- a/src/Nest/Modules/Scripting/IStoredScript.cs +++ b/src/Nest/Modules/Scripting/IStoredScript.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using Elasticsearch.Net; +using Newtonsoft.Json; namespace Nest { @@ -6,31 +7,61 @@ namespace Nest [JsonConverter(typeof(ReadAsTypeJsonConverter))] public interface IStoredScript { + [JsonProperty("lang")] + string Lang { get; set; } + [JsonProperty("source")] string Source { get; set; } } + public class StoredScript : IStoredScript { + [JsonProperty("lang")] + string IStoredScript.Lang { get; set; } + [JsonProperty("source")] string IStoredScript.Source { get; set; } //used for deserialization - internal StoredScript() { } + internal StoredScript() + { + } - protected StoredScript(string source) + /// + /// Stored Script constructor + /// + /// Used only for Mustache and Lucene Expression templates. + /// Script source + protected StoredScript(string lang, string source) { + ((IStoredScript) this).Lang = lang; ((IStoredScript) this).Source = source; } } public class PainlessScript : StoredScript { - public PainlessScript(string source) : base(source) { } + public PainlessScript(string source) : base(null, source) + { + } + } + + public class LuceneExpressionScript : StoredScript + { + private static readonly string Lang = ScriptLang.Expression.GetStringValue(); + public LuceneExpressionScript(string source) : base(Lang, source) { } + } + + public class MustacheScript : StoredScript + { + private static readonly string Lang = ScriptLang.Mustache.GetStringValue(); + public MustacheScript(string source) : base(Lang, source) { } } public class StoredScriptDescriptor : DescriptorBase, IStoredScript { string IStoredScript.Source { get; set; } + string IStoredScript.Lang { get; set; } public StoredScriptDescriptor Source(string source) => Assign(a => a.Source = source); } diff --git a/src/Nest/Modules/Scripting/PutScript/PutScriptRequest.cs b/src/Nest/Modules/Scripting/PutScript/PutScriptRequest.cs index 742d959c7ce..040fc90630f 100644 --- a/src/Nest/Modules/Scripting/PutScript/PutScriptRequest.cs +++ b/src/Nest/Modules/Scripting/PutScript/PutScriptRequest.cs @@ -23,5 +23,7 @@ public PutScriptDescriptor Script(Func se Assign(a => a.Script = selector?.Invoke(new StoredScriptDescriptor())); public PutScriptDescriptor Painless(string source) => Assign(a => a.Script = new PainlessScript(source)); + public PutScriptDescriptor LuceneExpression(string source) => Assign(a => a.Script = new LuceneExpressionScript(source)); + public PutScriptDescriptor Mustache(string source) => Assign(a => a.Script = new MustacheScript(source)); } } From 958020e0d63b65146f87e6414f3838247e4d5da3 Mon Sep 17 00:00:00 2001 From: Russ Cam Date: Thu, 14 Dec 2017 14:24:31 +1100 Subject: [PATCH 3/3] Convert PutScriptApi to integration test Remove no longer supported languages from ScriptLang enum --- src/Nest/Modules/Scripting/IStoredScript.cs | 32 +++++++++++++------ .../Scripting/PutScript/PutScriptRequest.cs | 11 +++++++ src/Nest/Modules/Scripting/ScriptLang.cs | 9 ------ .../Scripting/PutScript/PutScriptApiTests.cs | 20 +++++++++--- 4 files changed, 49 insertions(+), 23 deletions(-) diff --git a/src/Nest/Modules/Scripting/IStoredScript.cs b/src/Nest/Modules/Scripting/IStoredScript.cs index 34bd1a16c1f..7425dac9d04 100644 --- a/src/Nest/Modules/Scripting/IStoredScript.cs +++ b/src/Nest/Modules/Scripting/IStoredScript.cs @@ -3,17 +3,27 @@ namespace Nest { + /// + /// A Stored script + /// [JsonObject(MemberSerialization = MemberSerialization.OptIn)] [JsonConverter(typeof(ReadAsTypeJsonConverter))] public interface IStoredScript { + /// + /// The script language + /// [JsonProperty("lang")] string Lang { get; set; } + /// + /// The script source + /// [JsonProperty("source")] string Source { get; set; } } + /// public class StoredScript : IStoredScript { [JsonProperty("lang")] @@ -23,27 +33,25 @@ public class StoredScript : IStoredScript string IStoredScript.Source { get; set; } //used for deserialization - internal StoredScript() - { - } + internal StoredScript() {} /// - /// Stored Script constructor + /// Instantiates a new instance of /// - /// Used only for Mustache and Lucene Expression templates. + /// Script language /// Script source protected StoredScript(string lang, string source) { - ((IStoredScript) this).Lang = lang; - ((IStoredScript) this).Source = source; + IStoredScript self = this; + self.Lang = lang; + self.Source = source; } } public class PainlessScript : StoredScript { - public PainlessScript(string source) : base(null, source) - { - } + private static readonly string Lang = ScriptLang.Painless.GetStringValue(); + public PainlessScript(string source) : base(Lang, source) { } } public class LuceneExpressionScript : StoredScript @@ -64,5 +72,9 @@ public class StoredScriptDescriptor : DescriptorBase Assign(a => a.Source = source); + + public StoredScriptDescriptor Lang(string lang) => Assign(a => a.Lang = lang); + + public StoredScriptDescriptor Lang(ScriptLang lang) => Assign(a => a.Lang = lang.GetStringValue()); } } diff --git a/src/Nest/Modules/Scripting/PutScript/PutScriptRequest.cs b/src/Nest/Modules/Scripting/PutScript/PutScriptRequest.cs index 040fc90630f..9b90e037628 100644 --- a/src/Nest/Modules/Scripting/PutScript/PutScriptRequest.cs +++ b/src/Nest/Modules/Scripting/PutScript/PutScriptRequest.cs @@ -22,8 +22,19 @@ public partial class PutScriptDescriptor public PutScriptDescriptor Script(Func selector) => Assign(a => a.Script = selector?.Invoke(new StoredScriptDescriptor())); + /// + /// A Painless language script + /// public PutScriptDescriptor Painless(string source) => Assign(a => a.Script = new PainlessScript(source)); + + /// + /// A Lucene expression language script + /// public PutScriptDescriptor LuceneExpression(string source) => Assign(a => a.Script = new LuceneExpressionScript(source)); + + /// + /// A Mustache template language script + /// public PutScriptDescriptor Mustache(string source) => Assign(a => a.Script = new MustacheScript(source)); } } diff --git a/src/Nest/Modules/Scripting/ScriptLang.cs b/src/Nest/Modules/Scripting/ScriptLang.cs index 96c1a2ee085..a4f7e141994 100644 --- a/src/Nest/Modules/Scripting/ScriptLang.cs +++ b/src/Nest/Modules/Scripting/ScriptLang.cs @@ -10,15 +10,6 @@ public enum ScriptLang [EnumMember(Value = "painless")] Painless, - [EnumMember(Value = "groovy")] - Groovy, - - [EnumMember(Value = "js")] - JS, - - [EnumMember(Value = "python")] - Python, - [EnumMember(Value = "expression")] Expression, diff --git a/src/Tests/Modules/Scripting/PutScript/PutScriptApiTests.cs b/src/Tests/Modules/Scripting/PutScript/PutScriptApiTests.cs index 937f11b5773..f53342a0284 100644 --- a/src/Tests/Modules/Scripting/PutScript/PutScriptApiTests.cs +++ b/src/Tests/Modules/Scripting/PutScript/PutScriptApiTests.cs @@ -1,5 +1,6 @@ using System; using Elasticsearch.Net; +using FluentAssertions; using Nest; using Tests.Framework; using Tests.Framework.Integration; @@ -9,13 +10,12 @@ namespace Tests.Modules.Scripting.PutScript { public class PutScriptApiTests - : ApiTestBase + : ApiIntegrationTestBase { public PutScriptApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } private static readonly string _name = "scrpt1"; - protected override LazyResponses ClientUsage() => Calls( fluent: (client, f) => client.PutScript(_name, f), fluentAsync: (client, f) => client.PutScriptAsync(_name, f), @@ -25,12 +25,18 @@ protected override LazyResponses ClientUsage() => Calls( protected override HttpMethod HttpMethod => HttpMethod.PUT; protected override string UrlPath => $"/_scripts/{_name}"; + protected override int ExpectStatusCode => 200; + protected override bool ExpectIsValid => true; protected override bool SupportsDeserialization => false; - protected override object ExpectJson { get; } = new + protected override object ExpectJson => new { - script = new { source = "1+1" } + script = new + { + lang = "painless", + source = "1+1" + } }; protected override PutScriptDescriptor NewDescriptor() => new PutScriptDescriptor(_name); @@ -42,5 +48,11 @@ protected override LazyResponses ClientUsage() => Calls( { Script = new PainlessScript("1+1") }; + + protected override void ExpectResponse(IPutScriptResponse response) + { + response.ShouldBeValid(); + response.Acknowledged.Should().BeTrue(); + } } }