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大神给出的解决方案
这看起来很奇怪,因为它似乎没有完全遵循缩进/行连续规则,但这是可行的。
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)
请注意,size = 0,仅表示查询应返回该项目的所有结果,而不是默认值10。因此,它将为所有grpId提供结果,而不仅仅是10,并且包括所有落入范围桶。