I try to sort my elastic search query based on the size of nested objects that match a condition (here: size of boosters with a specific status)
Here is my mapping :
{
"companies" : {
"mappings" : {
"dynamic" : "false",
"properties" : {
"authorization_level" : { "type" : "keyword" },
"boosters" : {
"type" : "nested",
"properties" : {
"id" : { "type" : "keyword" },
"status" : { "type" : "keyword" } //condition is on this specific field
}
},
/// More properties not relevant here
}
}
}
}
If I simplify my query to keep only the sorting part, I have this :
GET companies/_search
{
"sort": [
{ "authorization_level": { "order": "asc" } },
{ "_script":
{
"script": "params['_source']['boosters'].size()",
"order": "desc",
"type": "number"
}
}
]
}
The script part is working well to sort companies by size of the nested field boosters
. But now I want to sort it by the size of boosters that have a specific status (eg: booster can have 2 different status completed or pending , and I want to count only the pending booster).
Would you know how to write the script part in order to achieve this ? Painless scripting language is not painless at all for me...
Thanks a lot