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')
Shipra
  • 1,259
  • 2
  • 14
  • 26

1 Answers1

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)
Val
  • 207,596
  • 13
  • 358
  • 360
  • 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') – Shipra Dec 22 '15 at 04:27
  • 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. – Val Dec 22 '15 at 04:32
  • You have a typo in your query (i.e. `field=` instead of `item=`), please check my updated answer. – Val Dec 22 '15 at 06:10
  • 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. – Val Dec 22 '15 at 07:54
  • 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' – Shipra Jan 08 '16 at 10:54