question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scripting a range aggregation

See original GitHub issue

Hi. I have this multi-level aggregation, that I would like to convert. Problem is, I don’t know how to code the ranges part is Python.

CURL code

POST /_search
{
    "size": 0,
    "aggs": {
        "by_property" : {
            "terms": {
                "field": "propertyId",
                "size": 0
            },
            "aggs": {
                "twitter_count": {
                    "range": {
                        "field": "twitterAccount.followers",
                        "ranges": [
                            { "to" : 5001},
                            { "from" : 5001, "to" : 10001},
                            { "from" : 10001, "to" : 50001},
                            { "from" : 50001}
                        ]
                    },
                    "aggs" : {
                        "email_addy": {
                            "terms" : {
                                "field": "emails.value",
                                "size": 0
                            }
                        }
                    }
                }
            }
        }
    }
}

Python DSL code

s.aggs.bucket('by_property', 'terms', field='propertyId', size=0) \
    .bucket('twitter_count', 'range', field='twitterAccount.followers')

How do I continue the aggregation and say what ranges, the range uses? .ranges( )? body=range{ } ?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
honzakralcommented, Apr 27, 2015

Hi,

the mechanism is always the same, whatever you would put inside of the json object, just pass in as kwargs, in this case:

s.aggs.bucket('by_property', 'terms', field='propertyId', size=0)\
    .bucket('twitter_count', 'range',
        field='twitterAccount.followers',
        ranges=[
            {'to': 5001},
            {'from': 5001, 'to': 10001},
            {'from': 10001, 'to': 50001},
            {'from': 50001}
        ]
    )

Note that you can always use Search.from_dict to just pass it the json you would send by curl and then inspect the resulting object and it’s repr:

s = Search.from_dict({...})
print(repr(s.aggs['by_property']['twitter_count']))

Hope this helps

1reaction
vbaiicommented, Apr 27, 2015

Thank so much Honza. I’m new to both Python and ElasticSearch, which conflates my syntax issues. I will also try Search.from_dict. That will come in handy for some other queries down the line. This definitely helped. Thanks again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Range aggregation | Elasticsearch Guide [8.5] | Elastic
Range aggregation is a bucket aggregation, which partitions documents into buckets rather than calculating metrics over fields like metrics aggregations do.
Read more >
Can I pass result of an aggregation to a range aggregation?
Another thing you can do is use scripts in the pipeline, in order to filter only the relevant results.
Read more >
Range Aggregation - 《Elasticsearch v7.9 Reference》 - 书栈网
Range aggregation accepts a script parameter. This parameter allows to defined an inline script that will be executed during aggregation ...
Read more >
ScriptedResult - how to solve related issues - Opster
Example of bucket aggregation is Histogram Aggregation, Range Aggregation, ... in scripts also and “avg” is the type of aggregation applied on fees...
Read more >
Bucket aggregations - OpenSearch documentation
The range aggregation lets you define the range for each bucket. For example, you can find the number of bytes between 1000 and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found