Skip to content

Runtime fields to expose _source and stored fields like existing scripts #61775

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
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 @@ -180,7 +180,7 @@ private static String painlessToLoadFromSource(String name, String type) {
return null;
}
StringBuilder b = new StringBuilder();
b.append("def v = source['").append(name).append("'];\n");
b.append("def v = params._source['").append(name).append("'];\n");
b.append("if (v instanceof Iterable) {\n");
b.append(" for (def vv : ((Iterable) v)) {\n");
b.append(" if (vv != null) {\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
import org.apache.lucene.index.LeafReaderContext;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.script.AggregationScript;
import org.elasticsearch.script.DynamicMap;
import org.elasticsearch.script.ScriptCache;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.search.lookup.LeafSearchLookup;
import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.search.lookup.SourceLookup;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import static org.elasticsearch.common.unit.TimeValue.timeValueMillis;

Expand Down Expand Up @@ -45,13 +49,20 @@ public static <F> ScriptContext<F> newContext(String name, Class<F> factoryClass
);
}

private static final Map<String, Function<Object, Object>> PARAMS_FUNCTIONS = Map.of(
"_source",
value -> ((SourceLookup) value).loadSourceIfNeeded()
);

private final Map<String, Object> params;
private final LeafSearchLookup leafSearchLookup;

public AbstractScriptFieldScript(Map<String, Object> params, SearchLookup searchLookup, LeafReaderContext ctx) {
this.leafSearchLookup = searchLookup.getLeafSearchLookup(ctx);
// TODO how do other scripts get stored fields exposed? Through asMap? I don't see any getters for them.
this.params = params;
params = new HashMap<>(params);
params.put("_source", leafSearchLookup.source());
params.put("_fields", leafSearchLookup.fields());
this.params = new DynamicMap(params, PARAMS_FUNCTIONS);
}

/**
Expand All @@ -71,7 +82,7 @@ public final Map<String, Object> getParams() {
/**
* Expose the {@code _source} to the script.
*/
public final Map<String, Object> getSource() {
protected final Map<String, Object> getSource() {
return leafSearchLookup.source();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ setup:
type: double
node:
type: keyword
store: true
day_of_week:
type: runtime_script
runtime_type: keyword
Expand All @@ -27,7 +28,7 @@ setup:
type: runtime_script
runtime_type: keyword
script: |
Instant instant = Instant.ofEpochMilli(source['timestamp']);
Instant instant = Instant.ofEpochMilli(params._source.timestamp);
ZonedDateTime dt = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC"));
emitValue(dt.dayOfWeek.getDisplayName(TextStyle.FULL, Locale.ROOT));
# Test fetching many values
Expand All @@ -45,7 +46,7 @@ setup:
runtime_type: keyword
script:
source: |
for (String node : doc['node']) {
for (String node : params._fields.node.values) {
emitValue(params.prefix + node);
}
params:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ setup:
runtime_type: long
script:
source: |
emitValue((long)(source['voltage'] * params.multiplier));
emitValue((long)(params._source.voltage * params.multiplier));
params:
multiplier: 10
# Test fetching many values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ setup:
runtime_type: double
script:
source: |
emitValue(source['voltage'] / params.max);
emitValue(params._source.voltage / params.max);
params:
max: 5.8
# Test fetching many values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ setup:
runtime_type: date
script:
source: |
Instant instant = Instant.ofEpochMilli(parse(source['timestamp']));
Instant instant = Instant.ofEpochMilli(parse(params._source.timestamp));
ZonedDateTime dt = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC"));
emitValue(toEpochMilli(dt.plus(1, ChronoUnit.DAYS)));
# Test returning millis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ setup:
runtime_type: ip
script:
source: |
String m = source["message"];
String m = params._source.message;
int end = m.indexOf(" ");
emitValue(m.substring(0, end));
# Test emitting many values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ setup:
runtime_type: boolean
script:
source: |
emitValue(source['voltage'] >= 5.0);
emitValue(params._source.voltage >= 5.0);
# Test many booleans
big_vals:
type: runtime_script
Expand Down