0

How can I get result of an aggregation?

My code is:

s = Items.search() #Items is DSL class
s.aggs.bucket('SP', 'terms', item=item_name).metric('max_amt', 'max', field='amount')
res = s.execute()

When trying this, I'm getting following error:

    ERROR:django.request:Internal Server Error: /items/
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/shipra/codeengine/items/ES/items_views.py", line 38, in sell_items
    res = s.execute()
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch_dsl/search.py", line 573, in execute
    **self._params
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/utils.py", line 69, in _wrapped
    return func(*args, params=params, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/__init__.py", line 530, in search
    doc_type, '_search'), params=params, body=body)
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/transport.py", line 307, in perform_request
    status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/http_urllib3.py", line 93, in perform_request
    self._raise_error(response.status, raw_data)
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/base.py", line 105, in _raise_error
    raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
RequestError: TransportError(400, u'search_phase_execution_exception')

1 Answer 1

4

You're almost there, you don't need to reassign the s reference to the result of the aggs.bucket call (i.e. no need for the a variable), just do it like this:

s = Items.search() #Items is DSL class
s.aggs.bucket('SP', 'terms', field=item_name).metric('max_amt', 'max', field='amount')
res = s.execute()              ^
                               |
            + change this to "field" instead of "item"

# do something with the aggs
for term in response.aggregations.SP.buckets:
    print(term.key, term.max_amt.value)
Sign up to request clarification or add additional context in comments.

5 Comments

I'm getting Transport Error when doing it your way. raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info) RequestError: TransportError(400, u'search_phase_execution_exception')
Interesting, can you update your question with the full error you're getting? The query is now getting generated and sent, so it might be some other issue now.
You have a typo in your query (i.e. field= instead of item=), please check my updated answer.
You're getting more like the max (not avg) for all items. Feel free to post a new question for the filter issue, so we don't cram too much into the same question.
The above mentioned query was working fine, but when I execute following, I am getting Atrribute Error. >>> s = s.aggs.bucket('SP', 'terms', field='colour') >>> res = s.execute() AttributeError: 'Terms' object has no attribute 'execute'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.