You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: manage-data/ingest/transform-enrich/general-tips.md
+14-12Lines changed: 14 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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
61
61
62
62
## Accessing fields in a script
63
63
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:
65
65
66
66
```json
67
67
{
@@ -79,7 +79,7 @@ This works as long as `user_name` is populated. If it is null, you get null as v
79
79
80
80
This is one of the alternatives to get it working when you only want to set it, if it is not null
81
81
82
-
```
82
+
```painless
83
83
if (ctx.user_name != null) {
84
84
ctx.user.name = ctx.user_name
85
85
}
@@ -106,16 +106,18 @@ One common thing I use it for is when dealing with numbers and casting. The fiel
106
106
107
107
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.
108
108
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
+
}
113
115
}
114
116
```
115
117
116
118
## Check if a value exists and is not null
117
119
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`.
119
121
120
122
```json
121
123
POST _ingest/pipeline/_simulate
@@ -159,7 +161,7 @@ Imagine you write this:
159
161
160
162
Then the ? will transform this simple if statement to this:
161
163
162
-
```
164
+
```painless
163
165
ctx.windows != null &&
164
166
ctx.windows.event != null &&
165
167
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
390
392
391
393
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:
392
394
393
-
```
395
+
```painless
394
396
ctx.abc = new HashMap();
395
397
ctx.abc = [:];
396
398
```
397
399
398
400
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:
399
401
400
-
```
402
+
```painless
401
403
if(ctx.abc == null) {
402
404
ctx.abc = [:];
403
405
}
404
406
```
405
407
406
408
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 `[:]`.
407
409
408
-
```
410
+
```painless
409
411
ctx.putIfAbsent("abc", new HashMap());
410
412
ctx.putIfAbsent("abc", [:]);
411
413
```
@@ -424,7 +426,7 @@ Now assuming you want to create this structure:
0 commit comments