如何在elasticsearch-dsl-py

时间:2015-12-21 19:43:17

标签: python elasticsearch aggregation elasticsearch-dsl elasticsearch-py

如何获得聚合结果?

我的代码是:

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

尝试此操作时,我遇到以下错误:

    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 个答案:

答案 0 :(得分:2)

你几乎就在那里,你不需要将s引用重新分配给aggs.bucket调用的结果(即不需要a变量),只需要它是这样的:

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)