Hi there.
I want to limit the search result JSON, so that for a particular (nested) field — that is an array — only the first element (if one exists) is returned. I guess the _source
filtering doesn't support this. I tried painless, but cannot figure out how to handle the case when the array is empty:
{
"query": {...},
"script_fields": {
"firstElement": {
"script": {
"lang": "painless",
"source": "params['_source'].parent.arrayThatMightBeEmpty[0]"
}
}
}
}
So this will work, as long as the arrayThatMightBeEmpty
contains elements, but will fail at runtime, if any search result contains an empty array.
How can this be solved?
Thanks!