gpt4 book ai didi

Python: elasticsearch.exceptions.NotFoundError: NotFoundError(404, '{"code":404 ,"message":"HTTP 404 Not Found"}')

转载 作者:行者123 更新时间:2023-12-03 02:19:17 29 4
gpt4 key购买 nike

我想在 Python 中使用 ElasticSearch 从给定的 URL(带有前缀)获取数据。这是我的代码:

if __name__ == '__main__':
username = "xxxx"
password = "xxxx"
url = "http://xxxx.xxx:80/xxxx/xxxx/"

_es = Elasticsearch([url], http_auth=(username, password))
if _es.ping():
print('Connect')
print(_es)
res = _es.search(index='index1', body={"query": {"match_all": {}}})
print(res)
else:
print('It could not connect!')

实际上,我可以 ping _e,而 _es 将是一个弹性对象:

<Elasticsearch([{'host': 'xxxx.xxx', 'url_prefix': 'xxxx/xxxx/', 'port': 80}])>

此外,为了验证我的 URL、端口和前缀,我在 Postman 中进行了检查,我可以正确获取 Json 格式的数据。但是当我在 Python 中运行代码时,我收到以下错误:

Connect
<Elasticsearch([{'host': 'xxxx.xxx', 'url_prefix': 'xxxx/xxxx/', 'port': 80}])>
Traceback (most recent call last):
File "/----.py", line 29, in <module>
res = _es.search(index='index1', body={"query": {"match_all": {}}})
File "/usr/local/lib/python3.5/dist-packages/elasticsearch/client/utils.py", line 139, in _wrapped
return func(*args, params=params, headers=headers, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/elasticsearch/client/__init__.py", line 1484, in search
body=body,
File "/usr/local/lib/python3.5/dist-packages/elasticsearch/transport.py", line 352, in perform_request
timeout=timeout,
File "/usr/local/lib/python3.5/dist-packages/elasticsearch/connection/http_urllib3.py", line 256, in perform_request
self._raise_error(response.status, raw_data)
File "/usr/local/lib/python3.5/dist-packages/elasticsearch/connection/base.py", line 288, in _raise_error
status_code, error_message, additional_info
elasticsearch.exceptions.NotFoundError: NotFoundError(404, '{"code":404,"message":"HTTP 404 Not Found"}')

有什么想法吗?

最佳答案

请使用 try except 和 elasticsearch 异常,如下所示:

from elasticsearch import Elasticsearch, exceptions

es = Elasticsearch()

try:
res2 = es.delete(index='my_index', id='not exist id')
except exceptions.NotFoundError:
pass

关于Python: elasticsearch.exceptions.NotFoundError: NotFoundError(404, '{"code":404 ,"message":"HTTP 404 Not Found"}'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62820622/

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