gpt4 book ai didi

elasticsearch - elasticsearch_dsl响应多个存储桶聚合

转载 作者:行者123 更新时间:2023-12-03 01:36:00 26 4
gpt4 key购买 nike

找到了关于如何使用elasticsearch_dsl Generate multiple buckets in aggregation构建嵌套聚合的线程

有人可以显示如何遍历响应以获取第二个存储桶结果吗?

for i in s.aggregations.clients.buckets.num_servers.buckets:

无法正常工作,如何获取num_servers或server_list中的内容?

最佳答案

如果要循环进行第二级聚合,则需要两个循环。这是一个假设您的索引中包含“标签”和“数字”字段的示例:

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, A

client = Elasticsearch()

# Build a two level aggregation
my_agg = A('terms', field='label')
my_agg.bucket('number', A('terms', field='number'))

# Build and submit the query
s = Search(using=client, index="stackoverflow")
s.aggs.bucket('label', my_agg)

response = s.execute()

# Loop through the first level of the aggregation
for label_bucket in response.aggregations.label.buckets:
print "Label: {}, {}".format(label_bucket.key, label_bucket.doc_count)

# Loop through the 2nd level of the aggregation
for number_bucket in label_bucket.number.buckets:
print " Number: {}, {}".format(number_bucket.key, number_bucket.doc_count)

哪个会打印如下:
Label: A, 3
Number: 2, 2
Number: 1, 1
Label: B, 3
Number: 3, 2
Number: 1, 1

关于elasticsearch - elasticsearch_dsl响应多个存储桶聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52609972/

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