Hello.
I have the following property in my mapping:
"attempts": {
"type": "nested",
"properties": {
"type": {
"type": "keyword"
},
"reason": {
"type": "keyword"
},
"status": {
"type": "keyword"
},
"number": {
"type": "integer"
},
"date": {
"type": "date"
}
}
}
attempts is an array of objects as you may see.
The functionality I need is the following:
- When a user sends me an attribute
type_A
- I need to sort all my documents the following way
- doc.attempts.type === 'type_A' -> sort by doc.attempts.date asc | desc
So basically I need to enter the attempts array of each document, check if the attempt is of type 'type_A' and return the date, if it's not of 'type_A' I might need to ignore it somehow...not sure..
What I tried is the following:
_script: {
nested: {
path: 'attempts',
},
script: {
lang : 'painless',
source: `
if (
doc['attempts.number'].value == params.number
&& doc['attempts.type'].value == params.type
) {
return doc['attempts.date'].value.millis
} else {
return 0
}
`,
params: { type, number },
},
order : sortType,
type : 'number',
}
the problem I faced is the following:
Imagine I have 10 documents, each of which have attempts array with 10 elements inside. Each of these 10 elements have type 'type_A' and their date are like these: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
When I do sorting 'type_A asc', with size: 5, it returns me the docs with these attempts dates: 6, 7, 8, 9, 10
As I understand from the output, attempts with date from 1 to 5 returned their dates. The rest returned 0, so since I requested sorting by asc, elastic returned 5 elements with lowest dates, in my case it's 0.
What I need is to return the date when attempt has the required type and return something other for the attempts with other types, that would work for asc and desc sorting.
Can anyone help, please?