gpt4 book ai didi

elasticsearch - 使用基数,但尝试查找总长度

转载 作者:行者123 更新时间:2023-12-02 23:28:19 26 4
gpt4 key购买 nike

我一直在使用基数来查找一些唯一字段,例如作者

    "aggs": {
"author_count" : {
"cardinality" : {
"field" : "author"
}
}
}

这有效并计算其中具有唯一作者的所有作者字段。

现在,我想找到这些独特作者的总人数。对于其他查询,我只是通过添加
  "aggs":{
"sum":{
"field" : "length" }}}

但是,当我尝试过此方法后,它不仅可以为独特的作者提供全部内容,而且可以为我提供全部内容。

因此,例如,如果字段作者仅包含一个“Kim”,则应返回该字段。
我希望每个只写一本书的作者,也将他们的所有页面长度加在一起。

例如
"author" : "kim",
"length": 100

"author" : "lolo",
"length": 100

输出应为 author_count 2total_length 200

但对于
"author" : "kim",
"length": 100

"author" : "lolo",
"length": 100

"author" : "lolo",
"length": 100

输出应为 author_count 1total_length 100。因为kim是 唯一的唯一作者(仅写过一本书的作者)

有任何想法吗?

最佳答案

了解问题之后,可以使用bucket selector aggregationsum bucket aggregation来实现。首先,在“作者”字段上进行汇总将赋予所有唯一作者,然后value count aggregation将提供这些唯一作者所撰写的书籍。
total_sum对页面的长度求和。

现在存储桶选择器将只保留那些只写过一本书的作者的存储桶,最后sum_bucket将所有作者的总和相加

{
"size": 0,
"aggs": {
"unique_author": {
"terms": {
"field": "author",
"size": 100
},
"aggs": {
"total_book_count": {
"value_count": {
"field": "author"
}
},
"total_sum": {
"sum": {
"field": "length"
}
},
"only_single_book_author": {
"bucket_selector": {
"buckets_path": {
"total_books": "total_book_count"
},
"script": "total_books==1"
}
}
}
},
"page_length": {
"sum_bucket": {
"buckets_path": "unique_author>total_sum"
}
}
}
}

关于elasticsearch - 使用基数,但尝试查找总长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40073912/

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