【发布时间】:2021-01-30 19:00:02
【问题描述】:
我正在尝试将数据放在字段中,并且应该在另一个字段上进行排序。我也想要分页,所以我想我可以使用弹性搜索的 BucketSort。我遇到了字符串(按字母顺序)归档的问题。
这是我的虚拟数据。
{
"_index": "testing-aggregation",
"_type": "employee",
"_id": "emp001_local000000000000001",
"_score": 10.0,
"_source": {
"name": [
"Person 01"
],
"groupbyid": [
"group0001"
],
"ranking": [
"2.0"
]
}
},
{
"_index": "testing-aggregation",
"_type": "employee",
"_id": "emp002_local000000000000001",
"_score": 85146.375,
"_source": {
"name": [
"Person 02"
],
"groupbyid": [
"group0001"
],
"ranking": [
"10.0"
]
}
},
{
"_index": "testing-aggregation",
"_type": "employee",
"_id": "emp003_local000000000000001",
"_score": 20.0,
"_source": {
"name": [
"Person 03"
],
"groupbyid": [
"group0002"
],
"ranking": [
"-1.0"
]
}
},
{
"_index": "testing-aggregation",
"_type": "employee",
"_id": "emp004_local000000000000001",
"_score": 5.0,
"_source": {
"name": [
"Person 04"
],
"groupbyid": [
"group0002"
],
"ranking": [
"2.0"
]
}
}
以上数据的映射。
{
"name": {
"type": "text",
"fielddata": true,
"fields": {
"lower_case_sort": {
"type": "text",
"fielddata": true,
"analyzer": "case_insensitive_sort"
}
}
},
"ranking": {
"type": "float"
},
"groupbyid": {
"type": "text",
"fielddata": true,
"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。
谢谢你, 沙维尔沙阿
【问题讨论】:
-
你能分享你的预期输出吗?
-
假设我想对字段 groupbyid 进行俱乐部记录并按 desc 对 name 字段进行排序,输出将如下所示。
group0002(emp003_local000000000000001,emp004_local000000000000001)group0001(emp001_local000000000000001,emp002_local000000000000001)`
标签: elasticsearch elastic-stack elasticsearch-aggregation