【问题标题】:elasticsearch-dsl aggregations returns only 10 results. How to change thiselasticsearch-dsl 聚合仅返回 10 个结果。如何改变这个
【发布时间】:2018-04-23 12:13:42
【问题描述】:

我正在使用 elasticsearch-dsl python 库连接到 elasticsearch 并进行聚合。

我正在关注代码

search.aggs.bucket('per_date', 'terms', field='date')\
        .bucket('response_time_percentile', 'percentiles', field='total_time',
                percents=percentiles, hdr={"number_of_significant_value_digits": 1})
response = search.execute()

这工作正常,但在response.aggregations.per_ts.buckets 中只返回 10 个结果

我想要所有的结果

我已经尝试了size=0 的一种解决方案,如this question 中所述

search.aggs.bucket('per_ts', 'terms', field='ts', size=0)\
        .bucket('response_time_percentile', 'percentiles', field='total_time',
                percents=percentiles, hdr={"number_of_significant_value_digits": 1})

response = search.execute()

但这会导致错误

TransportError(400, u'parsing_exception', u'[terms] failed to parse field [size]')

【问题讨论】:

  • 你有什么解决办法吗?我也面临同样的问题
  • 我也遇到了同样的问题

标签: python elasticsearch elasticsearch-dsl


【解决方案1】:

我有同样的问题。我终于找到了这个解决方案:

s = Search(using=client, index="jokes").query("match", jks_content=keywords).extra(size=0)
a = A('terms', field='jks_title.keyword', size=999999)
s.aggs.bucket('by_title', a)
response = s.execute()

2.x 之后,size=0 的所有存储桶结果将不再有效,请参阅此thread。在我的示例中,我只是将大小设置为 999999。您可以根据自己的情况选择一个较大的数字。

建议为数字大小显式设置合理的值 介于 1 到 2147483647 之间。

希望这会有所帮助。

【讨论】:

  • 精湛 .. 非常适合聚合查询。久经考验的弹性搜索 5.X。
【解决方案2】:

这有点旧,但我遇到了同样的问题。我想要的基本上是一个迭代器,我可以用它来遍历我返回的所有聚合(我也有很多独特的结果)。

我发现最好的办法是创建一个像这样的 python 生成器

def scan_aggregation_results():
    i=0
    partitions=20
    while i < partitions:
        s = Search(using=elastic, index='my_index').extra(size=0)
        agg = A('terms', field='my_field.keyword', size=999999,
                include={"partition": i, "num_partitions": partitions})
        s.aggs.bucket('my_agg', agg)
        result = s.execute()

        for item in result.aggregations.my_agg.buckets:
            yield my_field.key
        i = i + 1

# in other parts of the code just do
for item in scan_aggregation_results():
    print(item)  # or do whatever you want with it

这里的神奇之处在于,elastic 会自动将结果数按 20 分区,即我定义的分区数。我只需要将大小设置为足以容纳单个分区的大小,在这种情况下,结果可能高达 2000 万个项目(或 20*999999)。如果您像我一样要返回的项目少得多(例如 20000 个),那么无论您定义了更大的大小,您的存储桶中每个查询都只会有 1000 个结果。

使用上面概述的生成器结构,您甚至可以摆脱它并创建自己的扫描仪,可以说,逐个迭代所有结果,这正是我想要的。

【讨论】:

    【解决方案3】:

    您应该阅读documentation

    所以在你的情况下,应该是这样的:

    search.aggs.bucket('per_date', 'terms', field='date')\
                .bucket('response_time_percentile', 'percentiles', field='total_time',
                        percents=percentiles, hdr={"number_of_significant_value_digits": 1})[0:50]
    response = search.execute()
    

    【讨论】:

    • 我已经厌倦了,但这不起作用错误:percentiles=percentiles, hdr={"number_of_significant_value_digits": 1})[0:20] TypeError: 'Percentiles' object has no attribute 'getitem'
    • 您是否像这样开始搜索:search = Search()
    猜你喜欢
    • 2014-05-20
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多