Versions (relevant - OpenSearch/Dashboard/Server OS/Browser):
1.2.4
Describe the issue:
Hello!
I got a question on how can I iterate over elements of nested object in a query.
I have index of the following structure:
{
"elements":{
"type":"nested",
"properties":{
"action_code":{
"type":"keyword"
},
"created_at":{
"type":"date"
}
}
}
}
I’d like to filter documents so that documents for which most recent item in elements
array has action_code= my_action
are filtered out.
I tried to use the following query:
{
"query": {
"bool": {
"filter": {
"script": {
"script": "String latestAction = ''; Date latestActionDate = new Date(Long.MIN_VALUE); for(nextAction in doc['elements'].values){if(nextAction['created_at'].value > latestActionDate){latestAction = nextAction['action_code'].value; latestActionDate = nextAction['created_at'].value }}return latestAction != 'my_action'"
}
}
}
}
}
However, I keep getting "No field found for [elements] in mapping with types []"
For update scripts I can access nested field and iterate over it with ctx._source.elements
. However, for script inside the query it seems there is no context available, since replacing doc['elements']
with ctx._source.elements
throws an error that ctx
is unknown field.
Is there any way to iterate over nested field in the query in Opensearch? How can I make query script be aware of context?
Configuration:
Relevant Logs or Screenshots: