Skip to content

Commit 9498933

Browse files
committed
miny fixes
1 parent a10e333 commit 9498933

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

manage-data/ingest/transform-enrich/general-tips.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ But it will error if object is `null`. To be a 100% on the safe side you need to
6161

6262
## Accessing fields in a script
6363

64-
Within a script there are the same two possibilities to access fields as above. As well as the new `getter`. This only works in the painless scripts in an ingest pipeline\! Take the following input:
64+
Within a script there are the same two possibilities to access fields as above. As well as the new `getter`. This only works in the painless scripts in an ingest pipeline. Take the following input:
6565

6666
```json
6767
{
@@ -79,7 +79,7 @@ This works as long as `user_name` is populated. If it is null, you get null as v
7979

8080
This is one of the alternatives to get it working when you only want to set it, if it is not null
8181

82-
```
82+
```painless
8383
if (ctx.user_name != null) {
8484
ctx.user.name = ctx.user_name
8585
}
@@ -106,16 +106,18 @@ One common thing I use it for is when dealing with numbers and casting. The fiel
106106

107107
This allows me to always set the `cpu.usage` field and not to worry about it, have an always working division. One other way to leverage this, in a simpler script is like this, but most scripts are rather complex so this is not that often applicable.
108108

109-
```
110-
script: {
111-
source: "ctx.abc = ctx.def"
112-
if: "ctx.def != null"
109+
```json
110+
{
111+
"script": {
112+
"source": "ctx.abc = ctx.def"
113+
"if": "ctx.def != null"
114+
}
113115
}
114116
```
115117

116118
## Check if a value exists and is not null
117119

118-
In simplest case the `ignore_empty_value` parameter is available in most processors to handle fields without values. Or the `ignore_failure` parameter to let the processor fail without impacting the pipeline you but sometime you will need to use the [null safe operator `?.`](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-operators-reference.html#null-safe-operator) to check if a field exists and is not `null`.
120+
In simplest case the `ignore_empty_value` parameter is available in most processors to handle fields without values. Or the `ignore_failure` parameter to let the processor fail without impacting the pipeline you but sometime you will need to use the [null safe operator `?.`](elasticsearch::/references/painless/current/painless-operators-reference.html#null-safe-operator) to check if a field exists and is not `null`.
119121

120122
```json
121123
POST _ingest/pipeline/_simulate
@@ -159,7 +161,7 @@ Imagine you write this:
159161

160162
Then the ? will transform this simple if statement to this:
161163

162-
```
164+
```painless
163165
ctx.windows != null &&
164166
ctx.windows.event != null &&
165167
ctx.windows.event.data != null &&
@@ -390,22 +392,22 @@ Sometimes it is needed to write to a field and this field does not exist yet. Wh
390392

391393
Creating something like `ctx.abc.def = “cool”` does not work unless you create the `abc` object beforehand or it already exists. There are multiple ways to do it. What we always or usually want to create is a Map. We can do it in a couple of ways:
392394

393-
```
395+
```painless
394396
ctx.abc = new HashMap();
395397
ctx.abc = [:];
396398
```
397399

398400
Both options are valid and do the same thing. However there is a big caveat and that is, that if `abc` already exists, it will be overwritten and empty. Validating if `abc` already exists can be done by:
399401

400-
```
402+
```painless
401403
if(ctx.abc == null) {
402404
ctx.abc = [:];
403405
}
404406
```
405407

406408
With a simple `if ctx.abc == null` we know that `abc` does not exist and we can create it. Alternatively you can use the shorthand which is super helpful when you need to go 2,3,4 levels deep. You can use either version with the `HashMap()` or with the `[:]`.
407409

408-
```
410+
```painless
409411
ctx.putIfAbsent("abc", new HashMap());
410412
ctx.putIfAbsent("abc", [:]);
411413
```
@@ -424,7 +426,7 @@ Now assuming you want to create this structure:
424426

425427
The `putIfAbsent` will help a ton here:
426428

427-
```
429+
```painless
428430
ctx.putIfAbsent("user", [:]);
429431
ctx.user.putIfAbsent("geo", [:]);
430432
ctx.user.geo = "Amsterdam"

0 commit comments

Comments
 (0)