[英]How to write script to sort two buckets individually in elasticsearch
I have two fields (ID and Name) in elastic search. 我在弹性搜索中有两个字段(ID和名称)。
I have a requirement to search and sort data by below conditions : 我需要按以下条件搜索和排序数据:
Bucket 1 : Contains all records having Name value empty (Sort by ID field in ascending order) 值区1:包含名称为空的所有记录(按ID字段升序排列)
Bucket 2 : Contains all records having Name value Present (Sort by Name field in ascending order) 值区2:包含名称值为Present的所有记录(按名称升序排列)
then return all the records of Bucket 1 followed by Bucket 2 in one response. 然后在一个响应中返回存储区1的所有记录,然后返回存储区2。
It sounds like you could state this a bit differently: Get all documents, and Sort first by Name (empty first!), then by ID. 听起来您可能会对此有所不同:获取所有文档,然后先按名称(按空)排序,然后按ID排序。
Stated that way, the solution falls out pretty easily. 如此说来,解决方案很容易解决。 Empty fields should be sorted first when sorting in ascending order anyway, so: 无论如何,在按升序排序时,应该首先对空字段进行排序,因此:
{
"query" : {
"match_all": {}
},
"sort" : [
{ "Name" : "asc" },
{ "ID" : "asc" }
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.