Skip to content

Commit bdc2b16

Browse files
authored
Render script params in script field xcontent (#59813)
This fixes the rendering of the `script` field in the `script` field type. Originally we weren't rendering the `params` and such because we couldn't parse them. But we can parse them now so we should render them. This updates the integration tests to include params and adds an integration test that fetches the mapping.
1 parent c8b5af9 commit bdc2b16

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ abstract class AbstractScriptMappedFieldType extends MappedFieldType {
3434

3535
void mapperXContentBody(XContentBuilder builder, Params params) throws IOException {
3636
builder.field("runtime_type", runtimeType());
37-
builder.field("script", script.getIdOrCode()); // TODO For some reason this doesn't allow us to do the full xcontent of the script.
37+
builder.field("script", script, params);
3838
}
3939

4040
@Override

x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/10_keyword.yml

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ setup:
2020
day_of_week:
2121
type: script
2222
runtime_type: keyword
23-
script: value(doc['timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))
23+
script: |
24+
value(doc['timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))
2425
days_starting_with_t:
2526
type: script
2627
runtime_type: keyword
@@ -30,6 +31,16 @@ setup:
3031
value(dow);
3132
}
3233
}
34+
prefixed_node:
35+
type: script
36+
runtime_type: keyword
37+
script:
38+
source: |
39+
for (String node : doc['node']) {
40+
value(params.prefix + node);
41+
}
42+
params:
43+
prefix: node_
3344

3445
- do:
3546
bulk:
@@ -49,16 +60,49 @@ setup:
4960
{"index":{}}
5061
{"timestamp": 1516297294000, "temperature": 202, "voltage": 4.0, "node": "c"}
5162
63+
---
64+
"get mapping":
65+
- do:
66+
indices.get_mapping:
67+
index: sensor
68+
- match: {sensor.mappings.properties.day_of_week.type: script }
69+
- match: {sensor.mappings.properties.day_of_week.runtime_type: keyword }
70+
- match:
71+
sensor.mappings.properties.day_of_week.script.source: |
72+
value(doc['timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))
73+
- match: {sensor.mappings.properties.day_of_week.script.lang: painless }
74+
- match: {sensor.mappings.properties.days_starting_with_t.type: script }
75+
- match: {sensor.mappings.properties.days_starting_with_t.runtime_type: keyword }
76+
- match:
77+
sensor.mappings.properties.days_starting_with_t.script.source: |
78+
for (String dow: doc['day_of_week']) {
79+
if (dow.startsWith('T')) {
80+
value(dow);
81+
}
82+
}
83+
- match: {sensor.mappings.properties.days_starting_with_t.script.lang: painless }
84+
- match: {sensor.mappings.properties.prefixed_node.type: script }
85+
- match: {sensor.mappings.properties.prefixed_node.runtime_type: keyword }
86+
- match:
87+
sensor.mappings.properties.prefixed_node.script.source: |
88+
for (String node : doc['node']) {
89+
value(params.prefix + node);
90+
}
91+
- match: {sensor.mappings.properties.prefixed_node.script.params: {prefix: node_} }
92+
- match: {sensor.mappings.properties.prefixed_node.script.lang: painless }
93+
5294
---
5395
"docvalue_fields":
5496
- do:
5597
search:
5698
index: sensor
5799
body:
58100
sort: timestamp
59-
docvalue_fields: [day_of_week]
101+
docvalue_fields: [day_of_week, days_starting_with_t, prefixed_node]
60102
- match: {hits.total.value: 6}
61103
- match: {hits.hits.0.fields.day_of_week: [Thursday] }
104+
- match: {hits.hits.0.fields.days_starting_with_t: [Thursday] }
105+
- match: {hits.hits.0.fields.prefixed_node: [node_c] }
62106

63107
---
64108
"terms agg":

x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/20_long.yml

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ setup:
2020
voltage_times_ten:
2121
type: script
2222
runtime_type: long
23-
script: |
24-
for (double v : doc['voltage']) {
25-
value((long)(v * 10));
26-
}
23+
script:
24+
source: |
25+
for (double v : doc['voltage']) {
26+
value((long)(v * params.multiplier));
27+
}
28+
params:
29+
multiplier: 10
2730
even_voltage_times_ten:
2831
type: script
2932
runtime_type: long
@@ -52,6 +55,31 @@ setup:
5255
{"index":{}}
5356
{"timestamp": 1516297294000, "temperature": 202, "voltage": 4.0, "node": "c"}
5457
58+
---
59+
"get mapping":
60+
- do:
61+
indices.get_mapping:
62+
index: sensor
63+
- match: {sensor.mappings.properties.voltage_times_ten.type: script }
64+
- match: {sensor.mappings.properties.voltage_times_ten.runtime_type: long }
65+
- match:
66+
sensor.mappings.properties.voltage_times_ten.script.source: |
67+
for (double v : doc['voltage']) {
68+
value((long)(v * params.multiplier));
69+
}
70+
- match: {sensor.mappings.properties.voltage_times_ten.script.params: {multiplier: 10} }
71+
- match: {sensor.mappings.properties.voltage_times_ten.script.lang: painless }
72+
- match: {sensor.mappings.properties.even_voltage_times_ten.type: script }
73+
- match: {sensor.mappings.properties.even_voltage_times_ten.runtime_type: long }
74+
- match:
75+
sensor.mappings.properties.even_voltage_times_ten.script.source: |
76+
for (long tenV: doc['voltage_times_ten']) {
77+
if (tenV % 2 == 0) {
78+
value(tenV);
79+
}
80+
}
81+
- match: {sensor.mappings.properties.even_voltage_times_ten.script.lang: painless }
82+
5583
---
5684
"docvalue_fields":
5785
- do:

0 commit comments

Comments
 (0)