Elasticsearch Python elasticsearch.exceptions.NotFoundError: NotFoundError(404, 'index_not_found_exception', 'no such index [node]', node, index_or_alias) beheben
English
Deutsch
Problem:
ElasticSearch-Index-Einstellungen sollen in Python mit Code wie
es_put_settings.py
es.indices.put_settings(index="node", body={
"index.mapping.total_fields.limit": 100000
})aktualisiert werden.
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)Lösung
Der Index muss zuerst erstellt werden, bevor Einstellungen gesetzt werden können. Zuerst überprüfen, ob der Index-Name richtig geschrieben ist! Der Index-Name ist in der Ausnahme sichtbar: no such index [node] bedeutet, dass der Index node heißt.
Der direkte Weg, einen Index zu erstellen, ist
es_indices_create_example.txt
es.indices.create("node")Aber beachten, dass dies fehlschlägt, wenn der Index bereits existiert. Um dieses Problem zu umgehen, empfehle ich, den Code aus unserem vorherigen Beitrag ElasticSearch-Index erstellen, wenn er noch nicht existiert, in Python zu verwenden:
es_create_index_if_not_exists.py
def es_create_index_if_not_exists(es, index):
"""Create the given ElasticSearch index and ignore error if it already exists"""
try:
es.indices.create(index)
except elasticsearch.exceptions.RequestError as ex:
if ex.error == 'resource_already_exists_exception':
pass # Index already exists. Ignore.
else: # Other exception - raise it
raise exnodesund die Funktion es_create_index_if_not_exists() verwenden, um den Index zu erstellen:
create_node_index.py
es_create_index_if_not_exists(es, "node") # Creates the "node" index ; doesn't fail if it already existsCheck 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