elastic/elasticsearch-py

es部署后索引无法创建

Rmj99 opened this issue · 1 comments

2024-03-26 19:05:07,315 - es_kb_service.py[line:54] - ERROR: 创建索引失败,重新
2024-03-26 19:05:07,316 - es_kb_service.py[line:55] - ERROR: BadRequestError(400, 'resource_already_exists_exception', 'index [cdss/lGW4XIQkT3qLcOq8yuDNPw] already exists')

Hello, issues need more detail and should be in English, so I hope you don't mind if I close it.

However, I'm assuming your issue is that you're trying to create an index that already exists. Let's reproduce the issue by creating an index twice:

>>> from elasticsearch import Elasticsearch
>>> es = Elasticsearch("http://localhost:9200")
>>> es.indices.create(index="test")
ObjectApiResponse({'acknowledged': True, 'shards_acknowledged': True, 'index': 'test'})
>>> es.indices.create(index="test")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/q/src/elasticsearch-py/elasticsearch/_sync/client/utils.py", line 446, in wrapped
    return api(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/q/src/elasticsearch-py/elasticsearch/_sync/client/indices.py", line 553, in create
    return self.perform_request(  # type: ignore[return-value]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/q/src/elasticsearch-py/elasticsearch/_sync/client/_base.py", line 423, in perform_request
    return self._client.perform_request(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/q/src/elasticsearch-py/elasticsearch/_sync/client/_base.py", line 271, in perform_request
    response = self._perform_request(
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/q/src/elasticsearch-py/elasticsearch/_sync/client/_base.py", line 352, in _perform_request
    raise HTTP_EXCEPTIONS.get(meta.status, ApiError)(
elasticsearch.BadRequestError: BadRequestError(400, 'resource_already_exists_exception', 'index [test/9NZJh64dSWSsfaR7gF_EWQ] already exists')

As documented in https://www.elastic.co/guide/en/elasticsearch/client/python-api/current/config.html#_ignoring_status_codes, status codes can be ignored using .options():

>>> es.options(ignore_status=400).indices.create(index="test")
ObjectApiResponse({'error': {'root_cause': [{'type': 'resource_already_exists_exception', 'reason': 'index [test/9NZJh64dSWSsfaR7gF_EWQ] already exists', 'index_uuid': '9NZJh64dSWSsfaR7gF_EWQ', 'index': 'test'}], 'type': 'resource_already_exists_exception', 'reason': 'index [test/9NZJh64dSWSsfaR7gF_EWQ] already exists', 'index_uuid': '9NZJh64dSWSsfaR7gF_EWQ', 'index': 'test'}, 'status': 400})

You can still see the error in the response, but the Elasticsearch client no longer raises an exception.