如何解决Elasticsearch:存储桶排序聚合+按字母顺序排序
我正在尝试汇总字段中的数据,并且应该在另一个字段中进行排序。我也想分页,所以我以为可以使用elasticsearch的BucketSort。我遇到了String(字母)字段的问题。
这是我的虚拟数据。
{
"_index": "testing-aggregation","_type": "employee","_id": "emp001_local000000000000001","_score": 10.0,"_source": {
"name": [
"Person 01"
],"groupbyid": [
"group0001"
],"ranking": [
"2.0"
]
}
},{
"_index": "testing-aggregation","_id": "emp002_local000000000000001","_score": 85146.375,"_source": {
"name": [
"Person 02"
],"ranking": [
"10.0"
]
}
},"_id": "emp003_local000000000000001","_score": 20.0,"_source": {
"name": [
"Person 03"
],"groupbyid": [
"group0002"
],"ranking": [
"-1.0"
]
}
},"_id": "emp004_local000000000000001","_score": 5.0,"_source": {
"name": [
"Person 04"
],"ranking": [
"2.0"
]
}
}
映射以上数据。
{
"name": {
"type": "text","fielddata": true,"fields": {
"lower_case_sort": {
"type": "text","analyzer": "case_insensitive_sort"
}
}
},"ranking": {
"type": "float"
},"groupbyid": {
"type": "text","index": "analyzed","fields": {
"raw": {
"type": "keyword","index": "not_analyzed"
}
}
}
}
ES查询:
{
"from": 0,"size": 0,"query": {
"bool": {
"must": [
{
"query_string": {
"query": "name:XYZ"
}
}
]
}
},"aggregations": {
"groupbyid": {
"terms": {
"field": "groupbyid.raw","size": 100
},"aggs": {
"top_hit_agg": {
"top_hits": {
"size": 100
}
},"ranking_agg": {
"min": {
"field": "ranking"
}
},"test_bucket_sort": {
"bucket_sort": {
"sort": [
{
"ranking_agg": {
"order": "desc"
}
}
],"size": 100,"from": 0
}
}
}
}
}
}
我能够实现“数字”字段。但是不知道我将如何处理名称字段。一种方法是使用脚本,但是我不想使用这种方法,因为这可能是一项昂贵的操作。
有人可以帮我吗?我正在使用ES 7.7.1。
谢谢, Sharvil Shah
解决方法
如果您想按字母顺序对
name
字段进行排序,请使用groupbyid
可以用于name.keyword
的术语聚合和排序 在键上。
您不能在最小聚合中使用name
字段,因为Min aggregation
{
"aggregations": {
"groupbyname": {
"terms": {
"field": "name.keyword","order": { "_key" : "desc" }
}
}
}
}
搜索结果:
"aggregations": {
"groupbyname": {
"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [
{
"key": "Person 04","doc_count": 1
},{
"key": "Person 03",{
"key": "Person 01","doc_count": 1
}
]
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。