From a75811adbb85fcc6ce57b1bbba3d4f6ab0100eda Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Tue, 20 Apr 2021 07:38:35 +0100 Subject: [PATCH] Update RuntimeField to use IInlineScript --- .../CommonOptions/Scripting/InlineScript.cs | 1 + .../Mapping/RuntimeFields/RuntimeField.cs | 19 ++++++--- .../HighLevel/Mapping/FluentMapping.doc.cs | 41 ++++++++++++++++++- .../PutMapping/PutMappingApiTest.cs | 16 ++++++-- .../RuntimeFields/RuntimeFieldsTests.cs | 4 +- tests/Tests/Search/Search/SearchApiTests.cs | 6 +-- .../Search/SearchingRuntimeFields.doc.cs | 3 +- .../TransformApiWithSettingsTests.cs | 3 +- 8 files changed, 73 insertions(+), 20 deletions(-) diff --git a/src/Nest/CommonOptions/Scripting/InlineScript.cs b/src/Nest/CommonOptions/Scripting/InlineScript.cs index 84ad480635a..e0cba963768 100644 --- a/src/Nest/CommonOptions/Scripting/InlineScript.cs +++ b/src/Nest/CommonOptions/Scripting/InlineScript.cs @@ -8,6 +8,7 @@ namespace Nest { [InterfaceDataContract] + [ReadAs(typeof(InlineScript))] public interface IInlineScript : IScript { [DataMember(Name ="source")] diff --git a/src/Nest/Mapping/RuntimeFields/RuntimeField.cs b/src/Nest/Mapping/RuntimeFields/RuntimeField.cs index 0e8eb0630f0..37ec14029a5 100644 --- a/src/Nest/Mapping/RuntimeFields/RuntimeField.cs +++ b/src/Nest/Mapping/RuntimeFields/RuntimeField.cs @@ -2,6 +2,7 @@ // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information +using System; using System.Runtime.Serialization; using Elasticsearch.Net.Utf8Json; @@ -22,7 +23,7 @@ public interface IRuntimeField /// The script to be evaluated for field calculation at search time. /// [DataMember(Name = "script")] - IStoredScript Script { get; set; } + IInlineScript Script { get; set; } /// /// The datatype of the runtime field. @@ -36,7 +37,7 @@ public class RuntimeField : IRuntimeField /// public string Format { get; set; } /// - public IStoredScript Script { get; set; } + public IInlineScript Script { get; set; } /// public FieldType Type { get; set; } } @@ -47,13 +48,19 @@ public class RuntimeFieldDescriptor public RuntimeFieldDescriptor(FieldType fieldType) => Self.Type = fieldType; string IRuntimeField.Format { get; set; } - IStoredScript IRuntimeField.Script { get; set; } + IInlineScript IRuntimeField.Script { get; set; } FieldType IRuntimeField.Type { get; set; } + /// public RuntimeFieldDescriptor Format(string format) => Assign(format, (a, v) => a.Format = v); - - public RuntimeFieldDescriptor Script(IStoredScript script) => Assign(script, (a, v) => a.Script = v); - public RuntimeFieldDescriptor Script(string source) => Assign(source, (a, v) => a.Script = new PainlessScript(source)); + /// + public RuntimeFieldDescriptor Script(IInlineScript script) => Assign(script, (a, v) => a.Script = v); + + /// + public RuntimeFieldDescriptor Script(string source) => Assign(source, (a, v) => a.Script = new InlineScript(source)); + + /// + public RuntimeFieldDescriptor Script(Func selector) => Assign(selector, (a, v) => a.Script = v?.Invoke(new InlineScriptDescriptor())); } } diff --git a/tests/Tests/ClientConcepts/HighLevel/Mapping/FluentMapping.doc.cs b/tests/Tests/ClientConcepts/HighLevel/Mapping/FluentMapping.doc.cs index fcae1eea90f..8cfd341a28a 100644 --- a/tests/Tests/ClientConcepts/HighLevel/Mapping/FluentMapping.doc.cs +++ b/tests/Tests/ClientConcepts/HighLevel/Mapping/FluentMapping.doc.cs @@ -463,7 +463,6 @@ public void MappingRuntimeFields() type = "keyword", script = new { - lang = "painless", source = "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))" } } @@ -487,6 +486,46 @@ public void MappingRuntimeFields() //hide Expect(expected).FromRequest(createIndexResponse); + + /** + * One may also include and use parameters in the script. + */ + createIndexResponse = _client.Indices.Create("myindex", c => c + .Map(m => m + .RuntimeFields(rtf => rtf + .RuntimeField("birthDayOfWeek", FieldType.Keyword, f => f + .Script(s => s + .Source("emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT) + params.suffix)") + .Params(p => p.Add("suffix", " with a suffix.")) + ))) + ) + ); + + //json + var finalExpected = new + { + mappings = new + { + runtime = new + { + birthDayOfWeek = new + { + type = "keyword", + script = new + { + source = "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT) + params.suffix)", + @params = new Dictionary + { + {"suffix", " with a suffix." } + } + } + } + } + } + }; + + //hide + Expect(finalExpected).FromRequest(createIndexResponse); } } } diff --git a/tests/Tests/Indices/MappingManagement/PutMapping/PutMappingApiTest.cs b/tests/Tests/Indices/MappingManagement/PutMapping/PutMappingApiTest.cs index 42d7a020b0c..b71d8f3223f 100644 --- a/tests/Tests/Indices/MappingManagement/PutMapping/PutMappingApiTest.cs +++ b/tests/Tests/Indices/MappingManagement/PutMapping/PutMappingApiTest.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information using System; +using System.Collections.Generic; using Elastic.Elasticsearch.Xunit.XunitPlumbing; using Elasticsearch.Net; using Nest; @@ -344,7 +345,7 @@ public class PutMappingWithRuntimeFieldsTests : ApiTestBase + { + { "foo", " is a good day." } + }}}} } }; @@ -365,7 +369,7 @@ public PutMappingWithRuntimeFieldsTests(ReadOnlyCluster cluster, EndpointUsage u .Index(CallIsolatedValue) .RuntimeFields(rtf => rtf .RuntimeField("runtime_date", FieldType.Date, rf => rf.Format("yyyy-MM-dd")) - .RuntimeField("runtime_scripted", FieldType.Keyword, rf=> rf.Script(new PainlessScript(ScriptValue)))); + .RuntimeField("runtime_scripted", FieldType.Keyword, rf=> rf.Script(s => s.Source(ScriptValue).Lang(ScriptLang.Painless).Params(p => p.Add("foo", " is a good day."))))); protected override LazyResponses ClientUsage() => Calls( (client, f) => client.Indices.PutMapping(f), @@ -388,8 +392,12 @@ protected override LazyResponses ClientUsage() => Calls( type = "keyword", script = new { + source = ScriptValue, lang = "painless", - source = ScriptValue + @params = new Dictionary + { + { "foo", " is a good day." } + } } } } diff --git a/tests/Tests/Mapping/RuntimeFields/RuntimeFieldsTests.cs b/tests/Tests/Mapping/RuntimeFields/RuntimeFieldsTests.cs index 81e611f264f..33e7ecfe80b 100644 --- a/tests/Tests/Mapping/RuntimeFields/RuntimeFieldsTests.cs +++ b/tests/Tests/Mapping/RuntimeFields/RuntimeFieldsTests.cs @@ -44,7 +44,7 @@ public RuntimeFieldsTests(WritableCluster cluster, EndpointUsage usage) : base(n {RuntimeFieldNameOne, new RuntimeField { Type = FieldType.Keyword, - Script = new PainlessScript(ScriptValue) + Script = new InlineScript(ScriptValue) }}, {RuntimeFieldNameTwo, new RuntimeField { @@ -108,7 +108,7 @@ public RuntimeFieldsTests(WritableCluster cluster, EndpointUsage usage) : base(n {RuntimeFieldNameOne, new RuntimeField { Type = FieldType.Keyword, - Script = new PainlessScript(ScriptValue) + Script = new InlineScript(ScriptValue) }}, {RuntimeFieldNameTwo, new RuntimeField { diff --git a/tests/Tests/Search/Search/SearchApiTests.cs b/tests/Tests/Search/Search/SearchApiTests.cs index ff871f0185e..482a8e7b822 100644 --- a/tests/Tests/Search/Search/SearchApiTests.cs +++ b/tests/Tests/Search/Search/SearchApiTests.cs @@ -681,7 +681,6 @@ public SearchApiRuntimeFieldsTests(ReadOnlyCluster cluster, EndpointUsage usage) { script = new { - lang = "painless", source = RuntimeFieldScript }, type = "keyword" @@ -710,10 +709,11 @@ public SearchApiRuntimeFieldsTests(ReadOnlyCluster cluster, EndpointUsage usage) .And(RuntimeFieldName), RuntimeFields = new RuntimeFields { - { RuntimeFieldName, new RuntimeField + { + RuntimeFieldName, new RuntimeField { Type = FieldType.Keyword, - Script = new PainlessScript(RuntimeFieldScript) + Script = new InlineScript(RuntimeFieldScript) } } } diff --git a/tests/Tests/Search/SearchingRuntimeFields.doc.cs b/tests/Tests/Search/SearchingRuntimeFields.doc.cs index 34a4934c7d1..bfd98a9738d 100644 --- a/tests/Tests/Search/SearchingRuntimeFields.doc.cs +++ b/tests/Tests/Search/SearchingRuntimeFields.doc.cs @@ -129,7 +129,6 @@ public void SearchQueryRuntimeFields() { script = new { - lang = "painless", source = "if (doc['type'].size() != 0) {emit(doc['type'].value.toUpperCase())}" }, type = "keyword" @@ -155,7 +154,7 @@ public void SearchQueryRuntimeFields() { "search_runtime_field", new RuntimeField { Type = FieldType.Keyword, - Script = new PainlessScript("if (doc['type'].size() != 0) {emit(doc['type'].value.toUpperCase())}") + Script = new InlineScript("if (doc['type'].size() != 0) {emit(doc['type'].value.toUpperCase())}") } } } diff --git a/tests/Tests/XPack/Transform/TransformApiWithSettingsTests.cs b/tests/Tests/XPack/Transform/TransformApiWithSettingsTests.cs index 0a42d47d77f..e1444061acf 100644 --- a/tests/Tests/XPack/Transform/TransformApiWithSettingsTests.cs +++ b/tests/Tests/XPack/Transform/TransformApiWithSettingsTests.cs @@ -347,7 +347,6 @@ protected override LazyResponses ClientUsage() => Calls( { script = new { - lang = "painless", source = RuntimeFieldScript }, type = "keyword" @@ -410,7 +409,7 @@ protected override LazyResponses ClientUsage() => Calls( { RuntimeFieldName, new RuntimeField { Type = FieldType.Keyword, - Script = new PainlessScript(RuntimeFieldScript) + Script = new InlineScript(RuntimeFieldScript) } } }