gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:35:33 24 4
gpt4 key购买 nike

我有以下代码:

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'

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

我做错了什么?如何从响应中正确提取匹配项和我的值?

编辑

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

最佳答案

对于“toDict”问题,response.to_dict() 对我有用。不确定这种行为在 lib 版本中是否相同。

关于python - 如何使用 elasticsearch DSL for python 访问响应对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37836490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com