Skip to content

Commit 0284f3d

Browse files
authored
Standardize runtime field emit methods (#61752)
This standardizes that name for all of the "emit value" methods used by runtime field scripts to `emitValue`. To make dates possible this adds a a `toEpochMilli` method to convert from `TemporalAccessor` into millis since epoch.
1 parent 95cdff7 commit 0284f3d

34 files changed

+132
-141
lines changed

x-pack/plugin/runtime-fields/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/runtimefields/rest/CoreTestsWithRuntimeFieldsIT.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,17 @@ private static String painlessToLoadFromSource(String name, String type) {
184184
}
185185

186186
private static final Map<String, String> PAINLESS_TO_EMIT = Map.ofEntries(
187-
Map.entry(BooleanFieldMapper.CONTENT_TYPE, "value(parse(value));"),
188-
Map.entry(DateFieldMapper.CONTENT_TYPE, "millis(parse(value.toString()));"),
187+
Map.entry(BooleanFieldMapper.CONTENT_TYPE, "emitValue(parse(value));"),
188+
Map.entry(DateFieldMapper.CONTENT_TYPE, "emitValue(parse(value.toString()));"),
189189
Map.entry(
190190
NumberType.DOUBLE.typeName(),
191-
"value(value instanceof Number ? ((Number) value).doubleValue() : Double.parseDouble(value.toString()));"
191+
"emitValue(value instanceof Number ? ((Number) value).doubleValue() : Double.parseDouble(value.toString()));"
192192
),
193-
Map.entry(KeywordFieldMapper.CONTENT_TYPE, "value(value.toString());"),
194-
Map.entry(IpFieldMapper.CONTENT_TYPE, "stringValue(value.toString());"),
193+
Map.entry(KeywordFieldMapper.CONTENT_TYPE, "emitValue(value.toString());"),
194+
Map.entry(IpFieldMapper.CONTENT_TYPE, "emitValue(value.toString());"),
195195
Map.entry(
196196
NumberType.LONG.typeName(),
197-
"value(value instanceof Number ? ((Number) value).longValue() : Long.parseLong(value.toString()));"
197+
"emitValue(value instanceof Number ? ((Number) value).longValue() : Long.parseLong(value.toString()));"
198198
)
199199
);
200200

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/AbstractLongScriptFieldScript.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public final int count() {
4949
return count;
5050
}
5151

52-
protected void collectValue(long v) {
52+
protected final void emitValue(long v) {
5353
if (values.length < count + 1) {
5454
values = ArrayUtil.grow(values, count + 1);
5555
}

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/BooleanScriptFieldScript.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public final int falses() {
6666
return falses;
6767
}
6868

69-
private void collectValue(boolean v) {
69+
protected final void emitValue(boolean v) {
7070
if (v) {
7171
trues++;
7272
} else {
@@ -78,15 +78,15 @@ public static boolean parse(Object str) {
7878
return Booleans.parseBoolean(str.toString());
7979
}
8080

81-
public static class Value {
81+
public static class EmitValue {
8282
private final BooleanScriptFieldScript script;
8383

84-
public Value(BooleanScriptFieldScript script) {
84+
public EmitValue(BooleanScriptFieldScript script) {
8585
this.script = script;
8686
}
8787

8888
public void value(boolean v) {
89-
script.collectValue(v);
89+
script.emitValue(v);
9090
}
9191
}
9292
}

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/DateScriptFieldScript.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,22 @@ public DateScriptFieldScript(Map<String, Object> params, SearchLookup searchLook
4444
this.formatter = formatter;
4545
}
4646

47-
public static class Millis {
48-
private final DateScriptFieldScript script;
49-
50-
public Millis(DateScriptFieldScript script) {
51-
this.script = script;
52-
}
53-
54-
public void millis(long v) {
55-
script.collectValue(v);
56-
}
47+
public static long toEpochMilli(TemporalAccessor v) {
48+
// TemporalAccessor is a nanos API so we have to convert.
49+
long millis = Math.multiplyExact(v.getLong(ChronoField.INSTANT_SECONDS), 1000);
50+
millis = Math.addExact(millis, v.get(ChronoField.NANO_OF_SECOND) / 1_000_000);
51+
return millis;
5752
}
5853

59-
public static class Date {
54+
public static class EmitValue {
6055
private final DateScriptFieldScript script;
6156

62-
public Date(DateScriptFieldScript script) {
57+
public EmitValue(DateScriptFieldScript script) {
6358
this.script = script;
6459
}
6560

66-
public void date(TemporalAccessor v) {
67-
// TemporalAccessor is a nanos API so we have to convert.
68-
long millis = Math.multiplyExact(v.getLong(ChronoField.INSTANT_SECONDS), 1000);
69-
millis = Math.addExact(millis, v.get(ChronoField.NANO_OF_SECOND) / 1_000_000);
70-
script.collectValue(millis);
61+
public void emitValue(long v) {
62+
script.emitValue(v);
7163
}
7264
}
7365

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/DoubleScriptFieldScript.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,22 @@ public final int count() {
6868
return count;
6969
}
7070

71-
private void collectValue(double v) {
71+
protected final void emitValue(double v) {
7272
if (values.length < count + 1) {
7373
values = ArrayUtil.grow(values, count + 1);
7474
}
7575
values[count++] = v;
7676
}
7777

78-
public static class Value {
78+
public static class EmitValue {
7979
private final DoubleScriptFieldScript script;
8080

81-
public Value(DoubleScriptFieldScript script) {
81+
public EmitValue(DoubleScriptFieldScript script) {
8282
this.script = script;
8383
}
8484

85-
public void value(double v) {
86-
script.collectValue(v);
85+
public void emitValue(double v) {
86+
script.emitValue(v);
8787
}
8888
}
8989
}

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/IpScriptFieldScript.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,22 @@ public final int count() {
9292
return count;
9393
}
9494

95-
private void collectValue(String v) {
95+
protected final void emitValue(String v) {
9696
if (values.length < count + 1) {
9797
values = ArrayUtil.grow(values, count + 1);
9898
}
9999
values[count++] = new BytesRef(InetAddressPoint.encode(InetAddresses.forString(v)));
100100
}
101101

102-
public static class StringValue {
102+
public static class EmitValue {
103103
private final IpScriptFieldScript script;
104104

105-
public StringValue(IpScriptFieldScript script) {
105+
public EmitValue(IpScriptFieldScript script) {
106106
this.script = script;
107107
}
108108

109-
public void stringValue(String v) {
110-
script.collectValue(v);
109+
public void emitValue(String v) {
110+
script.emitValue(v);
111111
}
112112
}
113113
}

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/LongScriptFieldScript.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ public LongScriptFieldScript(Map<String, Object> params, SearchLookup searchLook
3838
super(params, searchLookup, ctx);
3939
}
4040

41-
public static class Value {
41+
public static class EmitValue {
4242
private final LongScriptFieldScript script;
4343

44-
public Value(LongScriptFieldScript script) {
44+
public EmitValue(LongScriptFieldScript script) {
4545
this.script = script;
4646
}
4747

48-
public void value(long v) {
49-
script.collectValue(v);
48+
public void emitValue(long v) {
49+
script.emitValue(v);
5050
}
5151
}
5252
}

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/StringScriptFieldScript.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,19 @@ public final List<String> resultsForDoc(int docId) {
5454
return results;
5555
}
5656

57-
public static class Value {
57+
protected final void emitValue(String v) {
58+
results.add(v);
59+
}
60+
61+
public static class EmitValue {
5862
private final StringScriptFieldScript script;
5963

60-
public Value(StringScriptFieldScript script) {
64+
public EmitValue(StringScriptFieldScript script) {
6165
this.script = script;
6266
}
6367

64-
public void value(String v) {
65-
script.results.add(v);
68+
public void emitValue(String v) {
69+
script.emitValue(v);
6670
}
6771
}
6872
}

x-pack/plugin/runtime-fields/src/main/resources/org/elasticsearch/xpack/runtimefields/boolean_whitelist.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
# The whitelist for boolean-valued runtime fields
88

9+
# These two whitelists are required for painless to find the classes
910
class org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript @no_import {
10-
11+
}
12+
class org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript$Factory @no_import {
1113
}
1214

1315
static_import {
14-
void value(org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript, boolean) bound_to org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript$Value
16+
# The `emitValue` callback to collect values for the field
17+
void emitValue(org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript, boolean) bound_to org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript$EmitValue
18+
# Parse a value from the source to a boolean
1519
boolean parse(def) from_class org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript
1620
}
1721

18-
# This import is required to make painless happy and it isn't 100% clear why
19-
class org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript$Factory @no_import {
20-
}

x-pack/plugin/runtime-fields/src/main/resources/org/elasticsearch/xpack/runtimefields/date_whitelist.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66

77
# The whitelist for date-valued runtime fields
88

9+
# These two whitelists are required for painless to find the classes
910
class org.elasticsearch.xpack.runtimefields.DateScriptFieldScript @no_import {
1011
}
12+
class org.elasticsearch.xpack.runtimefields.DateScriptFieldScript$Factory @no_import {
13+
}
1114

1215
static_import {
13-
void millis(org.elasticsearch.xpack.runtimefields.DateScriptFieldScript, long) bound_to org.elasticsearch.xpack.runtimefields.DateScriptFieldScript$Millis
14-
void date(org.elasticsearch.xpack.runtimefields.DateScriptFieldScript, TemporalAccessor) bound_to org.elasticsearch.xpack.runtimefields.DateScriptFieldScript$Date
16+
# The `emitValue` callback to collect values for the field
17+
void emitValue(org.elasticsearch.xpack.runtimefields.DateScriptFieldScript, long) bound_to org.elasticsearch.xpack.runtimefields.DateScriptFieldScript$EmitValue
18+
# Parse a value from the source to millis since epoch
1519
long parse(org.elasticsearch.xpack.runtimefields.DateScriptFieldScript, def) bound_to org.elasticsearch.xpack.runtimefields.DateScriptFieldScript$Parse
20+
# Add an easy method to convert temporalAccessors to millis since epoch.
21+
long toEpochMilli(java.time.temporal.TemporalAccessor) from_class org.elasticsearch.xpack.runtimefields.DateScriptFieldScript
1622
}
1723

18-
# This import is required to make painless happy and it isn't 100% clear why
19-
class org.elasticsearch.xpack.runtimefields.DateScriptFieldScript$Factory @no_import {
20-
}
24+
25+

0 commit comments

Comments
 (0)