Skip to content

Commit 8d04047

Browse files
authored
Update RuntimeField to use IInlineScript (#5546)
1 parent f5bf78c commit 8d04047

File tree

8 files changed

+73
-20
lines changed

8 files changed

+73
-20
lines changed

src/Nest/CommonOptions/Scripting/InlineScript.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Nest
99
{
1010
[InterfaceDataContract]
11+
[ReadAs(typeof(InlineScript))]
1112
public interface IInlineScript : IScript
1213
{
1314
[DataMember(Name ="source")]

src/Nest/Mapping/RuntimeFields/RuntimeField.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using System;
56
using System.Runtime.Serialization;
67
using Elasticsearch.Net.Utf8Json;
78

@@ -22,7 +23,7 @@ public interface IRuntimeField
2223
/// The script to be evaluated for field calculation at search time.
2324
/// </summary>
2425
[DataMember(Name = "script")]
25-
IStoredScript Script { get; set; }
26+
IInlineScript Script { get; set; }
2627

2728
/// <summary>
2829
/// The datatype of the runtime field.
@@ -36,7 +37,7 @@ public class RuntimeField : IRuntimeField
3637
/// <inheritdoc />
3738
public string Format { get; set; }
3839
/// <inheritdoc />
39-
public IStoredScript Script { get; set; }
40+
public IInlineScript Script { get; set; }
4041
/// <inheritdoc />
4142
public FieldType Type { get; set; }
4243
}
@@ -47,13 +48,19 @@ public class RuntimeFieldDescriptor
4748
public RuntimeFieldDescriptor(FieldType fieldType) => Self.Type = fieldType;
4849

4950
string IRuntimeField.Format { get; set; }
50-
IStoredScript IRuntimeField.Script { get; set; }
51+
IInlineScript IRuntimeField.Script { get; set; }
5152
FieldType IRuntimeField.Type { get; set; }
5253

54+
/// <inheritdoc cref="IRuntimeField.Format" />
5355
public RuntimeFieldDescriptor Format(string format) => Assign(format, (a, v) => a.Format = v);
54-
55-
public RuntimeFieldDescriptor Script(IStoredScript script) => Assign(script, (a, v) => a.Script = v);
5656

57-
public RuntimeFieldDescriptor Script(string source) => Assign(source, (a, v) => a.Script = new PainlessScript(source));
57+
/// <inheritdoc cref="IRuntimeField.Script" />
58+
public RuntimeFieldDescriptor Script(IInlineScript script) => Assign(script, (a, v) => a.Script = v);
59+
60+
/// <inheritdoc cref="IRuntimeField.Script" />
61+
public RuntimeFieldDescriptor Script(string source) => Assign(source, (a, v) => a.Script = new InlineScript(source));
62+
63+
/// <inheritdoc cref="IRuntimeField.Script" />
64+
public RuntimeFieldDescriptor Script(Func<InlineScriptDescriptor, IInlineScript> selector) => Assign(selector, (a, v) => a.Script = v?.Invoke(new InlineScriptDescriptor()));
5865
}
5966
}

tests/Tests/ClientConcepts/HighLevel/Mapping/FluentMapping.doc.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,6 @@ public void MappingRuntimeFields()
463463
type = "keyword",
464464
script = new
465465
{
466-
lang = "painless",
467466
source = "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"
468467
}
469468
}
@@ -487,6 +486,46 @@ public void MappingRuntimeFields()
487486

488487
//hide
489488
Expect(expected).FromRequest(createIndexResponse);
489+
490+
/**
491+
* One may also include and use parameters in the script.
492+
*/
493+
createIndexResponse = _client.Indices.Create("myindex", c => c
494+
.Map<Company>(m => m
495+
.RuntimeFields(rtf => rtf
496+
.RuntimeField("birthDayOfWeek", FieldType.Keyword, f => f
497+
.Script(s => s
498+
.Source("emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT) + params.suffix)")
499+
.Params(p => p.Add("suffix", " with a suffix."))
500+
)))
501+
)
502+
);
503+
504+
//json
505+
var finalExpected = new
506+
{
507+
mappings = new
508+
{
509+
runtime = new
510+
{
511+
birthDayOfWeek = new
512+
{
513+
type = "keyword",
514+
script = new
515+
{
516+
source = "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT) + params.suffix)",
517+
@params = new Dictionary<string, string>
518+
{
519+
{"suffix", " with a suffix." }
520+
}
521+
}
522+
}
523+
}
524+
}
525+
};
526+
527+
//hide
528+
Expect(finalExpected).FromRequest(createIndexResponse);
490529
}
491530
}
492531
}

tests/Tests/Indices/MappingManagement/PutMapping/PutMappingApiTest.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information
44

55
using System;
6+
using System.Collections.Generic;
67
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
78
using Elasticsearch.Net;
89
using Nest;
@@ -344,7 +345,7 @@ public class PutMappingWithRuntimeFieldsTests : ApiTestBase<ReadOnlyCluster, Put
344345
{
345346
// These test serialisation only. Integration tests take place in RuntimeFieldsTests.cs
346347

347-
private const string ScriptValue = "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))";
348+
private const string ScriptValue = "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT) + params.foo";
348349

349350
public PutMappingWithRuntimeFieldsTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
350351

@@ -357,15 +358,18 @@ public PutMappingWithRuntimeFieldsTests(ReadOnlyCluster cluster, EndpointUsage u
357358
RuntimeFields = new RuntimeFields
358359
{
359360
{ "runtime_date", new RuntimeField { Type = FieldType.Date, Format = "yyyy-MM-dd" } },
360-
{ "runtime_scripted", new RuntimeField { Type = FieldType.Keyword, Script = new PainlessScript(ScriptValue) } }
361+
{ "runtime_scripted", new RuntimeField { Type = FieldType.Keyword, Script = new InlineScript(ScriptValue) { Lang = "painless", Params = new Dictionary<string, object>
362+
{
363+
{ "foo", " is a good day." }
364+
}}}}
361365
}
362366
};
363367

364368
protected override Func<PutMappingDescriptor<Project>, IPutMappingRequest> Fluent => d => d
365369
.Index(CallIsolatedValue)
366370
.RuntimeFields(rtf => rtf
367371
.RuntimeField("runtime_date", FieldType.Date, rf => rf.Format("yyyy-MM-dd"))
368-
.RuntimeField("runtime_scripted", FieldType.Keyword, rf=> rf.Script(new PainlessScript(ScriptValue))));
372+
.RuntimeField("runtime_scripted", FieldType.Keyword, rf=> rf.Script(s => s.Source(ScriptValue).Lang(ScriptLang.Painless).Params(p => p.Add("foo", " is a good day.")))));
369373

370374
protected override LazyResponses ClientUsage() => Calls(
371375
(client, f) => client.Indices.PutMapping(f),
@@ -388,8 +392,12 @@ protected override LazyResponses ClientUsage() => Calls(
388392
type = "keyword",
389393
script = new
390394
{
395+
source = ScriptValue,
391396
lang = "painless",
392-
source = ScriptValue
397+
@params = new Dictionary<string, string>
398+
{
399+
{ "foo", " is a good day." }
400+
}
393401
}
394402
}
395403
}

tests/Tests/Mapping/RuntimeFields/RuntimeFieldsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public RuntimeFieldsTests(WritableCluster cluster, EndpointUsage usage) : base(n
4444
{RuntimeFieldNameOne, new RuntimeField
4545
{
4646
Type = FieldType.Keyword,
47-
Script = new PainlessScript(ScriptValue)
47+
Script = new InlineScript(ScriptValue)
4848
}},
4949
{RuntimeFieldNameTwo, new RuntimeField
5050
{
@@ -108,7 +108,7 @@ public RuntimeFieldsTests(WritableCluster cluster, EndpointUsage usage) : base(n
108108
{RuntimeFieldNameOne, new RuntimeField
109109
{
110110
Type = FieldType.Keyword,
111-
Script = new PainlessScript(ScriptValue)
111+
Script = new InlineScript(ScriptValue)
112112
}},
113113
{RuntimeFieldNameTwo, new RuntimeField
114114
{

tests/Tests/Search/Search/SearchApiTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,6 @@ public SearchApiRuntimeFieldsTests(ReadOnlyCluster cluster, EndpointUsage usage)
681681
{
682682
script = new
683683
{
684-
lang = "painless",
685684
source = RuntimeFieldScript
686685
},
687686
type = "keyword"
@@ -710,10 +709,11 @@ public SearchApiRuntimeFieldsTests(ReadOnlyCluster cluster, EndpointUsage usage)
710709
.And(RuntimeFieldName),
711710
RuntimeFields = new RuntimeFields
712711
{
713-
{ RuntimeFieldName, new RuntimeField
712+
{
713+
RuntimeFieldName, new RuntimeField
714714
{
715715
Type = FieldType.Keyword,
716-
Script = new PainlessScript(RuntimeFieldScript)
716+
Script = new InlineScript(RuntimeFieldScript)
717717
}
718718
}
719719
}

tests/Tests/Search/SearchingRuntimeFields.doc.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ public void SearchQueryRuntimeFields()
129129
{
130130
script = new
131131
{
132-
lang = "painless",
133132
source = "if (doc['type'].size() != 0) {emit(doc['type'].value.toUpperCase())}"
134133
},
135134
type = "keyword"
@@ -155,7 +154,7 @@ public void SearchQueryRuntimeFields()
155154
{ "search_runtime_field", new RuntimeField
156155
{
157156
Type = FieldType.Keyword,
158-
Script = new PainlessScript("if (doc['type'].size() != 0) {emit(doc['type'].value.toUpperCase())}")
157+
Script = new InlineScript("if (doc['type'].size() != 0) {emit(doc['type'].value.toUpperCase())}")
159158
}
160159
}
161160
}

tests/Tests/XPack/Transform/TransformApiWithSettingsTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ protected override LazyResponses ClientUsage() => Calls(
347347
{
348348
script = new
349349
{
350-
lang = "painless",
351350
source = RuntimeFieldScript
352351
},
353352
type = "keyword"
@@ -410,7 +409,7 @@ protected override LazyResponses ClientUsage() => Calls(
410409
{ RuntimeFieldName, new RuntimeField
411410
{
412411
Type = FieldType.Keyword,
413-
Script = new PainlessScript(RuntimeFieldScript)
412+
Script = new InlineScript(RuntimeFieldScript)
414413
}
415414
}
416415
}

0 commit comments

Comments
 (0)