如何使用Elasticsearch DSL for python访问响应对象 [英] How to access the response object using elasticsearch DSL for python
本文介绍了如何使用Elasticsearch DSL for python访问响应对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
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)
我得到了错误:
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'
但是,这与文档所述内容直接矛盾:文档
However that is in direct contradiction to what the docs state: Docs
我做错了什么?如何从响应中正确提取匹配和我的值?
What am I doing wrong? How can I correctly extract the hits and my values from the response?
EDIT
EDIT
响应对象也应该具有方法"toDict",但是当我尝试调用它时,我再次得到AttributeError.
Also the response object ist supposed to have a method "toDict" but when I try to call it I again get an AttributeError.
推荐答案
对于"toDict"问题, response.to_dict()
对我有用.不知道此行为在lib版本之间是否相同.
For the "toDict" question, response.to_dict()
works for me. Not sure if this behaviour is the same across lib versions.
这篇关于如何使用Elasticsearch DSL for python访问响应对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文