如何修复 Elasticsearch Python elasticsearch.exceptions.NotFoundError: NotFoundError(404, index_not_found_exception)
问题:
你想在 Python 中使用类似这样的代码更新 ElasticSearch 索引设置
es_put_settings.py
es.indices.put_settings(index="node", body={
"index.mapping.total_fields.limit": 100000
})notfound_trace.txt
Traceback (most recent call last):
File "estest.py", line 11, in <module>
es.indices.put_settings(index="node", body={
File "/usr/local/lib/python3.8/dist-packages/elasticsearch/client/utils.py", line 168, in _wrapped
return func(*args, params=params, headers=headers, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/elasticsearch/client/indices.py", line 786, in put_settings
return self.transport.perform_request(
File "/usr/local/lib/python3.8/dist-packages/elasticsearch/transport.py", line 415, in perform_request
raise e
File "/usr/local/lib/python3.8/dist-packages/elasticsearch/transport.py", line 381, in perform_request
status, headers_response, data = connection.perform_request(
File "/usr/local/lib/python3.8/dist-packages/elasticsearch/connection/http_urllib3.py", line 277, in perform_request
self._raise_error(response.status, raw_data)
File "/usr/local/lib/python3.8/dist-packages/elasticsearch/connection/base.py", line 330, in _raise_error
raise HTTP_EXCEPTIONS.get(status_code, TransportError)(
elasticsearch.exceptions.NotFoundError: NotFoundError(404, 'index_not_found_exception', 'no such index [node]', node, index_or_alias)解决方案
需要先创建索引才能设置。**首先,仔细检查索引名称拼写是否正确!**你可以在异常中看到索引名称:no such index [node] 表示索引名为 node
创建索引的直接方法是
es_indices_create_example.txt
es.indices.create("node")但注意这在索引已存在时会失败。为了解决此问题,我推荐使用我们之前关于如何在 Python 中创建不存在的 ElasticSearch 索引的文章中的代码:
es_create_index_if_not_exists.py
def es_create_index_if_not_exists(es, index):
"""创建给定的 ElasticSearch 索引,如果已存在则忽略错误"""
try:
es.indices.create(index)
except elasticsearch.exceptions.RequestError as ex:
if ex.error == 'resource_already_exists_exception':
pass # 索引已存在。忽略。
else: # 其他异常 - 抛出它
raise exnodes并使用 es_create_index_if_not_exists() 函数创建你的索引:
create_node_index.py
es_create_index_if_not_exists(es, "node") # 创建 "node" 索引;如果已存在则不会失败Check out similar posts by category:
Databases, ElasticSearch, Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow