gpt4 book ai didi

elasticsearch - 如何在结果中按字段对一组存储桶进行排序

转载 作者:行者123 更新时间:2023-12-02 23:53:10 25 4
gpt4 key购买 nike

我需要按定义为文本的“优先级”字段对存储分区进行排序,但我不知道该如何进行。

您介意帮我吗?

我尝试了bucket_sort,但是ES给出了关于类型的错误,与sort和order相同。

这是聚合查询

{
"query": {
[...]
},
"sort": [
{
"priority.keyword": {
"order": "asc"
}
}
],
"aggregations": {
"by_family": {
"terms": {
"field": "familyId",
"size": 25,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"same_family": {
"top_hits": {
"from": 0,
"size": 1,
"version": false,
"explain": false,
"highlight": {
"pre_tags": [
"<search>"
],
"post_tags": [
"</search>"
],
"fields": {
"title*": {
"type": "unified"
}
}
}
}
}
}
}
}
}

结果的示例是:
{
"responses" : [
{
"took" : 13117,
"timed_out" : false,
"_shards" : {
"total" : 10,
"successful" : 10,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1754299,
"max_score" : null,
"hits" : [...]
},
"aggregations" : {
"by_family" : {
"doc_count_error_upper_bound" : 40,
"sum_other_doc_count" : 1753462,
"buckets" : [
{
"key" : 39031576,
"doc_count" : 92,
"same_family" : {
"hits" : {
"total" : 92,
"max_score" : 10.636923,
"hits" : [
{
"_index" : "idx5-1554993721115",
"_type" : "_doc",
"_id" : "589403A-333506350",
"_score" : 10.636923,
"_source" : {
"number" : "589403A",
"suggest" : {
"input" : [
"589403A"
]
},
"id" : "589403A-333506350",
"familyRepresentative" : 1,
"familyId" : 39031576,
"countryCode" : "NZ",
"number" : "589403",
"kind" : "A",
"family" : [ ],
"priority" : "20070425", <-------------
"created" : "2019-04-14",
"modified" : null,
"title" : [...],

我想按定义为索引中文本的字段“优先级”对存储桶聚合(asc / desc)进行排序

最佳答案

您需要定义另一个子聚合(例如max或min,具体取决于您要排序的方式),然后按该指标对父terms聚合进行排序。请记住,在familyId的存储桶中,文档的priority字段可能都具有不同的值,因此仅根据给定字段的合计值对给定的文档字段对存储桶进行排序是没有意义的。

{
"query": {
[...]
},
"sort": [
{
"priority.keyword": {
"order": "asc"
}
}
],
"aggregations": {
"by_family": {
"terms": {
"field": "familyId",
"size": 25,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"max_priority": "desc"
}
]
},
"aggregations": {
"max_priority": {
"max": {
"script": "Long.parseLong(doc['priority.keyword'].value)"
}
}
}
}
}

关于elasticsearch - 如何在结果中按字段对一组存储桶进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56151928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com