我的文档如下索引,
1
{
"name": "Gilly",
"hobbyName" : "coin collection",
"countries": ["US","France","Georgia"]
}
2
{
"name": "Billy",
"hobbyName":"coin collection",
"countries":["UK","Ghana","China","France"]
}
现在我需要根据字段“countries”的数组长度对这些文档进行排序,这样排序后的结果将是order2,document1的顺序。如何使用elasticsearch实现这一目标?
答案 0 :(得分:3)
您可以使用script based sorting来实现此目标。
{
"query": {
"match_all": {}
},
"sort": {
"_script": {
"type": "number",
"script": "doc['countries'].values.size()",
"order": "desc"
}
}
}
答案 1 :(得分:2)