Skip to content

Commit e9e6477

Browse files
Update script-fields-api.md (elastic#2209)
Add the MUCH needed example of using the `set` call to manipulate field data. --------- Co-authored-by: István Zoltán Szabó <[email protected]>
1 parent ea3e325 commit e9e6477

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

explore-analyze/scripting/script-fields-api.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ Use the `field` API to access document fields:
2525
field('my_field').get(<default_value>)
2626
```
2727

28+
Alternatively use the shortcut of `$` to get a field.
29+
30+
```painless
31+
$('my_field', <default_value>)
32+
```
33+
2834
This API fundamentally changes how you access documents in Painless. Previously, you had to access the `doc` map with the field name that you wanted to access:
2935

3036
```painless
@@ -77,7 +83,6 @@ ZonedDateTime end = field('end').get(null);
7783
return start == null || end == null ? -1 : ChronoUnit.MILLIS.between(start, end)
7884
```
7985

80-
8186
## Supported mapped field types [_supported_mapped_field_types]
8287

8388
The following table indicates the mapped field types that the `field` API supports. For each supported type, values are listed that are returned by the `field` API (from the `get` and `as<Type>` methods) and the `doc` map (from the `getValue` and `get` methods).
@@ -112,3 +117,20 @@ The `fields` API currently doesn’t support some fields, but you can still acce
112117
| `wildcard` | `String` | - | `String` | `String` |
113118
| `flattened` | `String` | - | `String` | `String` |
114119

120+
## Manipulation of the fields data
121+
122+
The field API provides a `set(<value>)` operation that will take the field name and create the necessary structure. Calling this inside an ingest pipelines script processor context:
123+
124+
```painless
125+
field("foo.bar").set("abc")
126+
```
127+
128+
leads to the generation of this JSON representation.
129+
130+
```json
131+
{
132+
"foo": {
133+
"bar": "abc"
134+
}
135+
}
136+
```

0 commit comments

Comments
 (0)