Skip to content

Standardize runtime field emit methods #61752

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,17 @@ private static String painlessToLoadFromSource(String name, String type) {
}

private static final Map<String, String> PAINLESS_TO_EMIT = Map.ofEntries(
Map.entry(BooleanFieldMapper.CONTENT_TYPE, "value(parse(value));"),
Map.entry(DateFieldMapper.CONTENT_TYPE, "millis(parse(value.toString()));"),
Map.entry(BooleanFieldMapper.CONTENT_TYPE, "emitValue(parse(value));"),
Map.entry(DateFieldMapper.CONTENT_TYPE, "emitValue(parse(value.toString()));"),
Map.entry(
NumberType.DOUBLE.typeName(),
"value(value instanceof Number ? ((Number) value).doubleValue() : Double.parseDouble(value.toString()));"
"emitValue(value instanceof Number ? ((Number) value).doubleValue() : Double.parseDouble(value.toString()));"
),
Map.entry(KeywordFieldMapper.CONTENT_TYPE, "value(value.toString());"),
Map.entry(IpFieldMapper.CONTENT_TYPE, "stringValue(value.toString());"),
Map.entry(KeywordFieldMapper.CONTENT_TYPE, "emitValue(value.toString());"),
Map.entry(IpFieldMapper.CONTENT_TYPE, "emitValue(value.toString());"),
Map.entry(
NumberType.LONG.typeName(),
"value(value instanceof Number ? ((Number) value).longValue() : Long.parseLong(value.toString()));"
"emitValue(value instanceof Number ? ((Number) value).longValue() : Long.parseLong(value.toString()));"
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public final int count() {
return count;
}

protected void collectValue(long v) {
protected final void emitValue(long v) {
if (values.length < count + 1) {
values = ArrayUtil.grow(values, count + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public final int falses() {
return falses;
}

private void collectValue(boolean v) {
protected final void emitValue(boolean v) {
if (v) {
trues++;
} else {
Expand All @@ -78,15 +78,15 @@ public static boolean parse(Object str) {
return Booleans.parseBoolean(str.toString());
}

public static class Value {
public static class EmitValue {
private final BooleanScriptFieldScript script;

public Value(BooleanScriptFieldScript script) {
public EmitValue(BooleanScriptFieldScript script) {
this.script = script;
}

public void value(boolean v) {
script.collectValue(v);
script.emitValue(v);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,22 @@ public DateScriptFieldScript(Map<String, Object> params, SearchLookup searchLook
this.formatter = formatter;
}

public static class Millis {
private final DateScriptFieldScript script;

public Millis(DateScriptFieldScript script) {
this.script = script;
}

public void millis(long v) {
script.collectValue(v);
}
public static long toEpochMilli(TemporalAccessor v) {
// TemporalAccessor is a nanos API so we have to convert.
long millis = Math.multiplyExact(v.getLong(ChronoField.INSTANT_SECONDS), 1000);
millis = Math.addExact(millis, v.get(ChronoField.NANO_OF_SECOND) / 1_000_000);
return millis;
}

public static class Date {
public static class EmitValue {
private final DateScriptFieldScript script;

public Date(DateScriptFieldScript script) {
public EmitValue(DateScriptFieldScript script) {
this.script = script;
}

public void date(TemporalAccessor v) {
// TemporalAccessor is a nanos API so we have to convert.
long millis = Math.multiplyExact(v.getLong(ChronoField.INSTANT_SECONDS), 1000);
millis = Math.addExact(millis, v.get(ChronoField.NANO_OF_SECOND) / 1_000_000);
script.collectValue(millis);
public void emitValue(long v) {
script.emitValue(v);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,22 @@ public final int count() {
return count;
}

private void collectValue(double v) {
protected final void emitValue(double v) {
if (values.length < count + 1) {
values = ArrayUtil.grow(values, count + 1);
}
values[count++] = v;
}

public static class Value {
public static class EmitValue {
private final DoubleScriptFieldScript script;

public Value(DoubleScriptFieldScript script) {
public EmitValue(DoubleScriptFieldScript script) {
this.script = script;
}

public void value(double v) {
script.collectValue(v);
public void emitValue(double v) {
script.emitValue(v);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,22 @@ public final int count() {
return count;
}

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

public static class StringValue {
public static class EmitValue {
private final IpScriptFieldScript script;

public StringValue(IpScriptFieldScript script) {
public EmitValue(IpScriptFieldScript script) {
this.script = script;
}

public void stringValue(String v) {
script.collectValue(v);
public void emitValue(String v) {
script.emitValue(v);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public LongScriptFieldScript(Map<String, Object> params, SearchLookup searchLook
super(params, searchLookup, ctx);
}

public static class Value {
public static class EmitValue {
private final LongScriptFieldScript script;

public Value(LongScriptFieldScript script) {
public EmitValue(LongScriptFieldScript script) {
this.script = script;
}

public void value(long v) {
script.collectValue(v);
public void emitValue(long v) {
script.emitValue(v);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,19 @@ public final List<String> resultsForDoc(int docId) {
return results;
}

public static class Value {
protected final void emitValue(String v) {
results.add(v);
}

public static class EmitValue {
private final StringScriptFieldScript script;

public Value(StringScriptFieldScript script) {
public EmitValue(StringScriptFieldScript script) {
this.script = script;
}

public void value(String v) {
script.results.add(v);
public void emitValue(String v) {
script.emitValue(v);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@

# The whitelist for boolean-valued runtime fields

# These two whitelists are required for painless to find the classes
class org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript @no_import {

}
class org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript$Factory @no_import {
}

static_import {
void value(org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript, boolean) bound_to org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript$Value
# The `emitValue` callback to collect values for the field
void emitValue(org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript, boolean) bound_to org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript$EmitValue
# Parse a value from the source to a boolean
boolean parse(def) from_class org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript
}

# This import is required to make painless happy and it isn't 100% clear why
class org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript$Factory @no_import {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@

# The whitelist for date-valued runtime fields

# These two whitelists are required for painless to find the classes
class org.elasticsearch.xpack.runtimefields.DateScriptFieldScript @no_import {
}
class org.elasticsearch.xpack.runtimefields.DateScriptFieldScript$Factory @no_import {
}

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

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


Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

# The whitelist for double-valued runtime fields

# These two whitelists are required for painless to find the classes
class org.elasticsearch.xpack.runtimefields.DoubleScriptFieldScript @no_import {
}
class org.elasticsearch.xpack.runtimefields.DoubleScriptFieldScript$Factory @no_import {
}

static_import {
void value(org.elasticsearch.xpack.runtimefields.DoubleScriptFieldScript, double) bound_to org.elasticsearch.xpack.runtimefields.DoubleScriptFieldScript$Value
# The `emitValue` callback to collect values for the field
void emitValue(org.elasticsearch.xpack.runtimefields.DoubleScriptFieldScript, double) bound_to org.elasticsearch.xpack.runtimefields.DoubleScriptFieldScript$EmitValue
}

# This import is required to make painless happy and it isn't 100% clear why
class org.elasticsearch.xpack.runtimefields.DoubleScriptFieldScript$Factory @no_import {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

# The whitelist for ip-valued runtime fields

# These two whitelists are required for painless to find the classes
class org.elasticsearch.xpack.runtimefields.IpScriptFieldScript @no_import {
}

static_import {
void stringValue(org.elasticsearch.xpack.runtimefields.IpScriptFieldScript, String) bound_to org.elasticsearch.xpack.runtimefields.IpScriptFieldScript$StringValue
class org.elasticsearch.xpack.runtimefields.IpScriptFieldScript$Factory @no_import {
}

# This import is required to make painless happy and it isn't 100% clear why
class org.elasticsearch.xpack.runtimefields.IpScriptFieldScript$Factory @no_import {
static_import {
# The `emitValue` callback to collect values for the field
void emitValue(org.elasticsearch.xpack.runtimefields.IpScriptFieldScript, String) bound_to org.elasticsearch.xpack.runtimefields.IpScriptFieldScript$EmitValue
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

# The whitelist for long-valued runtime fields

# These two whitelists are required for painless to find the classes
class org.elasticsearch.xpack.runtimefields.LongScriptFieldScript @no_import {
}

static_import {
void value(org.elasticsearch.xpack.runtimefields.LongScriptFieldScript, long) bound_to org.elasticsearch.xpack.runtimefields.LongScriptFieldScript$Value
class org.elasticsearch.xpack.runtimefields.LongScriptFieldScript$Factory @no_import {
}

# This import is required to make painless happy and it isn't 100% clear why
class org.elasticsearch.xpack.runtimefields.LongScriptFieldScript$Factory @no_import {
static_import {
# The `emitValue` callback to collect values for the field
void emitValue(org.elasticsearch.xpack.runtimefields.LongScriptFieldScript, long) bound_to org.elasticsearch.xpack.runtimefields.LongScriptFieldScript$EmitValue
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

# The whitelist for string-valued runtime fields

# These two whitelists are required for painless to find the classes
class org.elasticsearch.xpack.runtimefields.StringScriptFieldScript @no_import {
}

static_import {
void value(org.elasticsearch.xpack.runtimefields.StringScriptFieldScript, String) bound_to org.elasticsearch.xpack.runtimefields.StringScriptFieldScript$Value
class org.elasticsearch.xpack.runtimefields.StringScriptFieldScript$Factory @no_import {
}

# This import is required to make painless happy and it isn't 100% clear why
class org.elasticsearch.xpack.runtimefields.StringScriptFieldScript$Factory @no_import {
static_import {
# The `emitValue` callback to collect values for the field
void emitValue(org.elasticsearch.xpack.runtimefields.StringScriptFieldScript, String) bound_to org.elasticsearch.xpack.runtimefields.StringScriptFieldScript$EmitValue
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class BooleanScriptFieldScriptTests extends ScriptFieldScriptTestCase<Boo
) {
@Override
public void execute() {
new BooleanScriptFieldScript.Value(this).value(false);
emitValue(false);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class DateScriptFieldScriptTests extends ScriptFieldScriptTestCase<DateSc
) {
@Override
public void execute() {
new DateScriptFieldScript.Millis(this).millis(1595431354874L);
emitValue(1595431354874L);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class DoubleScriptFieldScriptTests extends ScriptFieldScriptTestCase<Doub
) {
@Override
public void execute() {
new DoubleScriptFieldScript.Value(this).value(1.0);
emitValue(1.0);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class IpScriptFieldScriptTests extends ScriptFieldScriptTestCase<IpScript
public static final IpScriptFieldScript.Factory DUMMY = (params, lookup) -> ctx -> new IpScriptFieldScript(params, lookup, ctx) {
@Override
public void execute() {
new IpScriptFieldScript.StringValue(this).stringValue("192.168.0.1");
emitValue("192.168.0.1");
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class LongScriptFieldScriptTests extends ScriptFieldScriptTestCase<LongSc
public static final LongScriptFieldScript.Factory DUMMY = (params, lookup) -> ctx -> new LongScriptFieldScript(params, lookup, ctx) {
@Override
public void execute() {
new LongScriptFieldScript.Value(this).value(1);
emitValue(1);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class StringScriptFieldScriptTests extends ScriptFieldScriptTestCase<Stri
) {
@Override
public void execute() {
new StringScriptFieldScript.Value(this).value("foo");
emitValue("foo");
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ private BooleanScriptFieldScript.Factory factory(String code) {
@Override
public void execute() {
for (Object foo : (List<?>) getSource().get("foo")) {
new BooleanScriptFieldScript.Value(this).value(parse(foo));
emitValue(parse(foo));
}
}
};
Expand All @@ -422,9 +422,7 @@ public void execute() {
@Override
public void execute() {
for (Object foo : (List<?>) getSource().get("foo")) {
new BooleanScriptFieldScript.Value(this).value(
(Boolean) foo ^ ((Boolean) getParams().get("param"))
);
emitValue((Boolean) foo ^ ((Boolean) getParams().get("param")));
}
}
};
Expand Down
Loading