I am trying to count JSON objects based on a flag. To do so, I created two foreach pipelines iterating over my objects.
I want to count all the objects in the documents
array for which the field "count"
is set to true
.
POST _ingest/pipeline/_simulate
{
"pipeline":{
"description":"...",
"processors":[
{
"set":{
"field":"specific.docCount",
"value":0
}
},
{
"foreach":{
"field":"data.status.transactions",
"processor":{
"foreach":{
"field":"_ingest._value.documents",
"processor":{
"script":{
"lang":"painless",
"inline":"if (ctx.count) ctx.specific.docCount += 1"
}
}
}
}
}
}
]
},
"docs":[
{
"_source":{
"data":{
"status":{
"transactions":[
{
"id":"123",
"documents":[
{
"count": true
},
{
"count": false
}
]
}
]
}
}
}
}
]
}
I'm getting the following error :
{
"docs": [
{
"error": {
"root_cause": [
{
"type": "exception",
"reason": "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: NullPointerException;",
"header": {
"processor_type": "foreach"
}
}
],
"type": "exception",
"reason": "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: NullPointerException;",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "ScriptException[runtime error]; nested: NullPointerException;",
"caused_by": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"if (ctx.count) ",
" ^---- HERE"
],
"script": "if (ctx.count) ctx.stats.docCount += 1",
"lang": "painless",
"caused_by": {
"type": "null_pointer_exception",
"reason": null
}
}
},
"header": {
"processor_type": "foreach"
}
}
}
]
}
This foreach pipeline doc suggests to use ctx
to refer to the ingest document, but I am not sure how to use this in my case.
How can I retrieve the current "foreach entry" in my painless script ?