Skip to content

Commit

Permalink
Add fields api to runtime fields contexts (#76265) (#76269)
Browse files Browse the repository at this point in the history
This changes extends AbstractFieldScript with DocBasedScript to make the fields API accessible to 
runtime fields contexts.
  • Loading branch information
elasticsearchmachine authored Aug 10, 2021
1 parent 94e05c4 commit a7a9d0e
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,39 @@ setup:
index: "my-index"
- match: { result: [ false, false, true, true ] }

---
"Execute with boolean field context (single-value, fields api)":
- do:
scripts_painless_execute:
body:
script:
source: "emit(field('rank').getValue(false) < params.max_rank);"
params:
max_rank: 5.0
context: "boolean_field"
context_setup:
document:
rank: 4
index: "my-index"
- match: { result: [ true ] }


---
"Execute with boolean field context (multi-value, fields api)":
- do:
scripts_painless_execute:
body:
script:
source: "emit(field('rank').getValue(false) < params.max_rank); emit(false); emit(false); emit(true);"
params:
max_rank: 5.0
context: "boolean_field"
context_setup:
document:
rank: 4
index: "my-index"
- match: { result: [ false, false, true, true ] }

---
"Execute with date field context (single-value)":
- do:
Expand Down Expand Up @@ -147,6 +180,35 @@ setup:
index: "my-index"
- match: { result: [ "2015-01-01T12:10:30.000Z", "2010-11-30T13:14:35.000Z" ] }

---
"Execute with date field context (single-value, fields api)":
- do:
scripts_painless_execute:
body:
script:
source: "emit(field('date').getValue(0).toInstant().toEpochMilli())"
context: "date_field"
context_setup:
document:
date: "2015-01-01T12:10:30Z"
index: "my-index"
- match: { result: [ "2015-01-01T12:10:30.000Z" ] }

---
"Execute with date field context (multi-value, fields api)":
- do:
scripts_painless_execute:
body:
script:
source: "emit(field('date0').values[0].toInstant().toEpochMilli()); emit(field('date1').values[0].toInstant().toEpochMilli());"
context: "date_field"
context_setup:
document:
date0: "2015-01-01T12:10:30Z"
date1: "2010-11-30T13:14:35Z"
index: "my-index"
- match: { result: [ "2015-01-01T12:10:30.000Z", "2010-11-30T13:14:35.000Z" ] }

---
"Execute with double field context (single-value)":
- do:
Expand Down Expand Up @@ -179,6 +241,38 @@ setup:
index: "my-index"
- match: { result: [ 20.0, 400.0, 55.0 ] }

---
"Execute with double field context (single-value, fields api)":
- do:
scripts_painless_execute:
body:
script:
source: "emit(field('rank').getValue(0.0) * params.max_rank)"
params:
max_rank: 5.0
context: "double_field"
context_setup:
document:
rank: 4
index: "my-index"
- match: { result: [ 20.0 ] }

---
"Execute with double field context (multi-value, fields api)":
- do:
scripts_painless_execute:
body:
script:
source: "emit(field('rank').getValue(0.0) * params.max_rank); emit(400.0); emit(55.0)"
params:
max_rank: 5.0
context: "double_field"
context_setup:
document:
rank: 4
index: "my-index"
- match: { result: [ 20.0, 400.0, 55.0 ] }

---
"Execute with geo point field context (single-value)":
- skip:
Expand Down Expand Up @@ -219,6 +313,47 @@ setup:
- close_to: { result.1.coordinates.1: { value: 41.0, error: 0.00001 } }
- match: { result.1.type: "Point" }

---
"Execute with geo point field context (single-value, fields api)":
- skip:
features: close_to
- do:
scripts_painless_execute:
body:
script:
source: "emit(field('point').getValue(null).lat + 1.0, field('point').getValue(null).lon - 1.0)"
context: "geo_point_field"
context_setup:
document:
point: "30.0,40.0"
index: "my-index"
- close_to: { result.0.coordinates.0: { value: 39.0, error: 0.00001 } }
- close_to: { result.0.coordinates.1: { value: 31.0, error: 0.00001 } }
- match: { result.0.type: "Point" }

---
"Execute with geo point field context (multi-value, fields api)":
- skip:
features: close_to
- do:
scripts_painless_execute:
body:
script:
source: "emit(field('p0').values[0].lat + 1.0, field('p0').values[0].lon - 1.0);
emit(field('p1').values[0].lat + 1.0, field('p1').values[0].lon - 1.0)"
context: "geo_point_field"
context_setup:
document:
p0: "30.0,40.0"
p1: "40.0,30.0"
index: "my-index"
- close_to: { result.0.coordinates.0: { value: 39.0, error: 0.00001 } }
- close_to: { result.0.coordinates.1: { value: 31.0, error: 0.00001 } }
- match: { result.0.type: "Point" }
- close_to: { result.1.coordinates.0: { value: 29.0, error: 0.00001 } }
- close_to: { result.1.coordinates.1: { value: 41.0, error: 0.00001 } }
- match: { result.1.type: "Point" }

---
"Execute with ip field context (single-value)":
- do:
Expand Down Expand Up @@ -247,6 +382,36 @@ setup:
index: "my-index"
- match: { result: [ "2001:db8::8a2e:370:7333", "192.168.1.254", "2001:db8::8a2e:370:7334" ] }

---
"Execute with ip field context (single-value, fields api)":
- do:
scripts_painless_execute:
body:
script:
source: "emit(field('ip').getValue('0.0.0.0'));"
context: "ip_field"
context_setup:
document:
ip: "192.168.1.254"
index: "my-index"
- match: { result: [ "192.168.1.254" ] }

---
"Execute with ip field context (multi-value, fields api)":
- do:
scripts_painless_execute:
body:
script:
source: "emit('2001:0db8:0000:0000:0000:8a2e:0370:7333');
emit(field('ip').getValue('0.0.0.0'));
emit('2001:db8::8a2e:370:7334')"
context: "ip_field"
context_setup:
document:
ip: "192.168.1.254"
index: "my-index"
- match: { result: [ "2001:db8::8a2e:370:7333", "192.168.1.254", "2001:db8::8a2e:370:7334" ] }

---
"Execute with long field context (single-value)":
- do:
Expand Down Expand Up @@ -279,6 +444,38 @@ setup:
index: "my-index"
- match: { result: [ 20, 35, 0, -90, 20 ] }

---
"Execute with long field context (single-value, fields api)":
- do:
scripts_painless_execute:
body:
script:
source: "emit(field('rank').getValue(0L) * (long)params.max_rank)"
params:
max_rank: 5.0
context: "long_field"
context_setup:
document:
rank: 4
index: "my-index"
- match: { result: [ 20 ] }

---
"Execute with long field context (multi-value, fields api)":
- do:
scripts_painless_execute:
body:
script:
source: "emit(field('rank').getValue(0L) * (long)params.max_rank); emit(35); emit(0); emit(-90); emit(20);"
params:
max_rank: 5.0
context: "long_field"
context_setup:
document:
rank: 4
index: "my-index"
- match: { result: [ 20, 35, 0, -90, 20 ] }

---
"Execute with keyword field context (single-value)":
- do:
Expand Down Expand Up @@ -308,6 +505,35 @@ setup:
- match: { result.0: "my_keyword" }
- match: { result.1: "my_keyword_test" }

---
"Execute with keyword field context (single-value, fields api)":
- do:
scripts_painless_execute:
body:
script:
source: "emit(field('keyword').getValue(''));"
context: "keyword_field"
context_setup:
document:
keyword: "my_keyword"
index: "my-index"
- match: { result.0: "my_keyword" }

---
"Execute with keyword field context (multi-value, fields api)":
- do:
scripts_painless_execute:
body:
script:
source: "emit(field('keyword').getValue('')); emit(field('keyword').getValue('') + '_test');"
context: "keyword_field"
context_setup:
document:
keyword: "my_keyword"
index: "my-index"
- match: { result.0: "my_keyword" }
- match: { result.1: "my_keyword_test" }

---
"Execute against an empty index with no mappings":
- do:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* Abstract base for scripts to execute to build scripted fields. Inspired by
* {@link AggregationScript} but hopefully with less historical baggage.
*/
public abstract class AbstractFieldScript {
public abstract class AbstractFieldScript extends DocBasedScript {
/**
* The maximum number of values a script should be allowed to emit.
*/
Expand Down Expand Up @@ -73,6 +73,8 @@ static <F> ScriptContext<F> newContext(String name, Class<F> factoryClass) {
protected final LeafSearchLookup leafSearchLookup;

public AbstractFieldScript(String fieldName, Map<String, Object> params, SearchLookup searchLookup, LeafReaderContext ctx) {
super(new DocValuesDocReader(searchLookup, ctx));

this.fieldName = fieldName;
this.leafSearchLookup = searchLookup.getLeafSearchLookup(ctx);
params = new HashMap<>(params);
Expand Down

0 comments on commit a7a9d0e

Please sign in to comment.