【问题标题】:Range aggregation using elasticsearch-dsl-py使用 elasticsearch-dsl-py 进行范围聚合
【发布时间】:2015-04-27 19:24:38
【问题描述】:
POST /_search
{
"size": 0,
"aggs": {
    "by_grp" : {
        "terms": {
            "field": "grpId",
            "size": 0
        },
        "aggs": {
            "twitter_count": {
                "range": {
                    "field": "twitter.followers",
                    "ranges": [
                        { "to" : 501},
                        { "from" : 501, "to" : 1001},
                        { "from" : 1001, "to" : 5001},
                        { "from" : 5001}
                    ]
                },
                "aggs" : {
                    "email_addy": {
                        "terms" : {
                            "field": "email.value",
                            "size": 0
                        }
                    }
                }
            }
        }
    }
}
}

使用elastic-search-dsl,我的python代码是

from datetime import datetime
from elasticsearch_dsl import DocType, String, Date, Integer, Search, Q
from elasticsearch_dsl.connections import connections
from elasticsearch import Elasticsearch

client = connections.create_connection(hosts=['http://somehost:9200'])
s = Search(using=client, index="dexy", doc_type="grp")

s.aggs.bucket('by_grp', 'terms', field='grpId', size=0) \
.bucket('twitter_count', 'range', field='twitter.followers')

我该去哪里定义 CURL 代码中的范围? GIT 和文档目前在这个主题上很少。未找到示例。

【问题讨论】:

标签: python python-2.7 curl elasticsearch-dsl


【解决方案1】:

这看起来很奇怪,因为它似乎没有完全遵循缩进/行继续规则,但这是可行的。

s.aggs.bucket('by_grp', 'terms', field='grpId', size=0) \
.bucket('twitter_count', 'range', field='twitter.followers',
    ranges=[
        {'to': 5001},
        {'from': 5001, 'to': 10001},
        {'from': 10001, 'to': 50001},
        {'from': 50001}
    ]
) \
.bucket('email_addy', 'terms', field='email.value', size=0)

请注意,大小 = 0 仅表示查询应返回该项目的所有结果,而不是默认的 10。因此,它将有所有 grpId 的结果,而不仅仅是 10,并包括所有电子邮件落入范围桶。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 2020-12-19
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多