简体   繁体   中英

Range aggregation using elasticsearch-dsl-py

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
                        }
                    }
                }
            }
        }
    }
}
}

Using elastic-search-dsl, my python code is

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')

Where do I go form here to define the ranges like in the CURL code? The GIT and docs are currently sparse on this topic. No examples found.

This looks strange, since it seems not to full follow indenting/line continuation rules, but this works.

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)

As a note, the size = 0, just means the query should return all results for that item, as opposed to the default 10. Therefore, it will have a results for all grpId not just 10, and include all email that fall into the range bucket.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM