Sae*_*HAH 4 python search get http elasticsearch
我想使用 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!')
Run Code Online (Sandbox Code Playgroud)
实际上,我可以 ping _e,并且 _es 将是一个弹性对象,如下所示:
<Elasticsearch([{'主机': 'xxxx.xxx', 'url_prefix': 'xxxx/xxxx/', '端口': 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"}')
Run Code Online (Sandbox Code Playgroud)
任何想法?
EMA*_*MAI 10
请使用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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12263 次 |
最近记录: |