如何使用 python 的 elasticsearch DSL 访问响应对象

Tin*_* A. 2 python elasticsearch elasticsearch-dsl

我有以下代码:

s = Search(using=Elasticsearch('http://user:passwd@ipaddress'), index="myindex")
q = Q("multi_match", query='some query', fields=['_all'])
s = s.query(q)

response = s.execute()
print('Total %d hits found.' % response.hits.total)
for hit in response:
    print(hit.title)
Run Code Online (Sandbox Code Playgroud)

我收到错误:

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/elasticsearch_dsl/utils.py", line 102, in __getattr__
return _wrap(self._d_[attr_name])
KeyError: 'title'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "FindImage.py", line 89, in <module>
main(args.image_file)
File "FindImage.py", line 82, in main
query_db([1], [2])
File "FindImage.py", line 77, in query_db
print(hit.title)
File "/usr/local/lib/python3.5/dist-packages/elasticsearch_dsl/utils.py", line 105, in __getattr__
'%r object has no attribute %r' % (self.__class__.__name__, attr_name))
AttributeError: 'Result' object has no attribute 'title'
Run Code Online (Sandbox Code Playgroud)

然而,这与文档所述的内容直接矛盾: Docs

我究竟做错了什么?如何从响应中正确提取命中和我的值?

编辑

另外,响应对象应该有一个方法“toDict”,但是当我尝试调用它时,我再次得到一个 AttributeError。

shi*_*ing 5

对于“toDict”问题,response.to_dict()对我有用。不确定此行为在库版本之间是否相同。