Reputation: 115
I have the elastic search cluster of the form:
{
"_id":"xxxx"
"contents":[
{
"name":"abc",
"age":"24"
}
]
},
{
"_id":"yyyy"
"contents":[
{
"name":"xyz",
"age":"25"
},
{
"name":"pqr",
"age":"29"
},
]
}
I have to perform a sort based query on the size of the object array field 'contents'.
I have tried:
{
"size": 10,
"from": 0,
"query": {
"match_all": {}
},
"sort" : {
"_script" : {
"script": "doc['contents.name'].values.size()",
"order": "desc",
"type" : "string",
}
}
}
The above query sometime gives me the correct result, but most of the times it fails to give the results in sorted order.
Upvotes: 1
Views: 2423
Reputation: 1069
Your contents
field is an array, not an object. So when you try and access contents.name in your script it's undefined. You should get the size of the contents
array:
{
"size": 10,
"from": 0,
"query": {
"match_all": {}
},
"sort" : {
"_script" : {
"script": "doc['contents'].values.size()",
"order": "desc",
"type" : "string",
}
}
}
Upvotes: 1