我目前正在尝试根据elasticsearch中的索引生成排行榜。由于我的初始分组依赖于索引上的2个字段,我有:
{
"size" : 0,
"query" : {
"bool": {
// conditions and filters
}
},
"aggs" : {
"rank" : {
"terms" : {
"script" : {
"source": "doc['fieldA'].value + '-' + doc['fieldB'].value" // i needed 2 fields for grouping
},
"size" : 10000,
"min_doc_count" : 10 // requirement for minimum records
}
}
}
}
结果是:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1134,
"max_score": 0,
"hits": []
},
"aggregations": {
"rank": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "groupA-user1",
"doc_count": 758
},
{
"key": "groupA-user2",
"doc_count": 289
},
{
"key": "groupB-user3",
"doc_count": 51
},
{
"key": "groupA-user4",
"doc_count": 30
},
{
"key": "groupA-user1",
"doc_count": 30
},
{
"key": "groupB-user10",
"doc_count": 10
}
]
}
}
}
由于它按降序doc_count排序,我可以从中生成某种排行榜groupA-user1
是第一位,groupA-user2
是第二位,因为我提取了客户端...但是在groupA-user4
和groupA-user1
的情况下,两者都具有相同的doc_count,并且应该排在第4位。有没有办法在弹性搜索中单独执行此操作?比如通过doc_count对生成的桶进行分组?或者可能在第一个脚本中立即分组?顺便说一句,我使用elasticsearch 5.6