I have the following field in my mapping
"attempts": {
"type": "nested",
"properties": {
"type": {
"type": "keyword"
},
"reason": {
"type": "keyword"
},
"status": {
"type": "keyword"
},
"number": {
"type": "integer"
},
"date": {
"type": "date"
}
}
}
I need to do the following:
when a certain type and number is provided from ui, I have to sort by the field attempts.date
I suppose it can be done with the script but I can't make it work.
This is what I tried:
sort: [
{
_script: {
nested: {
path: 'attempts',
},
script: {
lang : 'painless',
source: `if (
doc['attempts.number'].value === params.number
&& doc['attempts.type'].value === params.attemptType
) {
return doc['attempts.date'].date.getMillis()
}`,
params: { attemptType, number },
},
order : sortType,
type : 'number',
},
},
{
_id: 'desc'
}
]
It does not give any error, it just returns the records in different order every time.
Can anybody help?