如何在 elasticsearch-dsl-py 中获取 aggs
How to get aggs in elasticsearch-dsl-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')
就快完成了,您不需要将 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)
如何获得聚合结果?
我的代码是:
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')
就快完成了,您不需要将 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)