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.

Response __bool__ gives not reliable results when using aggregations

See original GitHub issue

When performing an aggregation over a large index is suggested to create search object to not return any hits. This however can give inconsistent boolean representation of the response in elasticsearch-dsl.

s = Search()
s = s.extra(size=0)
s.aggs.bucket('articles_per_day', 'date_histogram', field='publish_date', interval='day')\
    .metric('clicks_per_day', 'sum', field='clicks')

r = s.execute()
bool(r)       # false
r.hits.totlal # != 0

Since no hits was returned Response.hits list is empty while Response.hits.total may be a positive number, thus meaning that the search was successful.

We suggest that Response.__bool__ should check hits.total instead then hits.__bool__ to test if the query was succesful.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
sethmlarsoncommented, Dec 3, 2020

Response.__bool__ will continue to reflect the hits length. To check whether there are aggs results should use the bool(resp.aggs)

1reaction
DaanDLcommented, Nov 26, 2020

I think we see what the bool should represent differently, you say:

the boolean check is meant to check if it could have returned any

while the implementation has the meaning (as of now) as:

will I get anything back if I iterate on the object

Both are valid approaches and I see where your expectation is coming from, but I also would find it weird if bool(response) returned True and then iterating over it would bring no results.

I wonder if the best solution is to not implement bool and instead rely on explicit methods. What do you think?

I know this is an old post, but I’m in a similar situation: I perform a query where I’m not interested in any hits, I just want the aggs.

When I perform a truthy check on the Response object, it returns false, even though aggregations are present in it. So wouldn’t it be more logical to do something like:

def __nonzero__(self):

    return bool(self.hits) or bool(self.aggs)

instead of only checking self.hits?

Your previous statement about iterating makes sense, but then again, returning a falsy result doesn’t make sense when there was indeed a correct result…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Elastic Avg. Aggregation in Bool query - Stack Overflow
My Objective : Filter the processName : AddCustomer and messageType : Response documents. Find average response time for the filtered data using ......
Read more >
Boolean Aggregate Functions? - Microsoft Power BI Community
Solved: Is there any way to aggregate a boolean field so that the summary result with be TRUE if any of the detail...
Read more >
Terms aggregation | Elasticsearch Guide [8.5] | Elastic
You'll know you've gone too large if the request fails with a message about max_buckets . Shard sizeedit. To get more accurate results,...
Read more >
Bucket aggregations - OpenSearch documentation
While the filter aggregation results in a single bucket, the filters aggregation returns multiple buckets, one for each of the defined filters.
Read more >
Fixing Tableau Errors: Cannot mix Aggregate and Non ...
In short, the reason for this error is that Tableau cannot mix an aggregate argument e.g. sum(Sales), avg(Profit), Profit Ratio with non- ...
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