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.

Return aggregation and subaggregation values only.

See original GitHub issue

I’d really like to be able to perform a arbitrarily nested aggregation with mixed agg types and be able to return only the values associated with each named aggregation (and any nested sub-aggregation). I know the returned model mirrors elasticseach defaults, but the default model is deeply hostile to [simply] parsing anything beyond a first or second order aggregation.

Example:

match_all = s.query()
match_all.aggs.bucket("example1", "terms", field="field")
match_all.aggs.bucket("example2", "avg", field="field2")
match_all.aggs["example1"].bucket("example3", "cardinality", field="field3")

This is fantastic, I have a simple query dsl where I can easily pull aggregate stats on my data (I use elasticsearch for text analysis in a data science context).

The problem arises when I get the result.

>> results.aggregations.to_dict()
{u'example1': {u'buckets': [{u'doc_count': 53586,
    u'example3': {u'value': 1},
    u'key': u'example 1 agg value'},
   {u'doc_count': 18278, u'example3': {u'value': 1}, u'key': u'1'},
   {u'doc_count': 17309, u'example3': {u'value': 1}, u'key': u'2'},
   {u'doc_count': 10230, u'example3': {u'value': 1}, u'key': u'3'},
   {u'doc_count': 9328, u'example3': {u'value': 1}, u'key': u'4'},
   {u'doc_count': 9090, u'example3': {u'value': 1}, u'key': u'5'},
   {u'doc_count': 7528, u'example3': {u'value': 1}, u'key': u'6'},
   {u'doc_count': 6409, u'example3': {u'value': 1}, u'key': u'7'},
   {u'doc_count': 5757, u'example3': {u'value': 1}, u'key': u'8'},
   {u'doc_count': 4896, u'example3': {u'value': 1}, u'key': u'9'}],
  u'doc_count_error_upper_bound': 0,
  u'sum_other_doc_count': 38543},
 u'example2': {u'value': 45}}

What I really want to be able to get using an option of some sort is this.

{
example1: [{example3: 1, ...} ...],
example2: 45
}

Thoughts? Exists? Possible? SOL (i.e. write a parser)?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
honzakralcommented, Nov 28, 2016

I improved the aggregation response in the aggs branch where each response is wrapped in an object similar to hits. This also allowed us to deserialize the bucket keys (so key for a date histogram bucket is an actual datetime object now) and should allow for easier extendability - simply create your own class with your methods etc.

It also contains other improvements like being able to iterate over the results, yielding you buckets.

please let me know what you think! Thank you!

0reactions
OAuthBringercommented, Dec 30, 2016

This is great and timely. I fell off of doing elasticsearch things for a while, but have orbited back to it. Arbitrary nested aggregations are again a business need. Thanks for the update.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Aggregations | Elasticsearch Guide [8.5] | Elastic
To return only aggregation results, set size to 0 : ... aggregation with an avg sub-aggregation calculates an average value for each bucket...
Read more >
ElasticSearch aggregation return entire sub object, not just the ...
Is there a way to get back the entire Category object, not only the Id ? Or serialize the category object into string...
Read more >
A Basic Guide To Elasticsearch Aggregations - Logz.io
Elasticsearch Aggregations enable you to group and perform calculations and statistics on your data. Learn how to run and apply various aggregations here....
Read more >
Bucket Aggregations - Open Distro for Elasticsearch
The values are returned with the key key . doc_count specifies the number of documents in each bucket. By default, the buckets are...
Read more >
Elasticsearch Aggregations - Bucket, Metric & Pipeline ...
With aggregations you can not only search your data, ... more useful as a sub aggregation to calculate values for a bucket aggregation....
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