gpt4 book ai didi

python - 通过Python脚本删除Elasticsearch索引时出现TransportError

转载 作者:行者123 更新时间:2023-12-03 01:39:47 30 4
gpt4 key购买 nike

我已经在自己的elasticsearch2.1.1机器中安装了windows,并且我有一个Elasticsearch索引,该索引托管在我的机器中,因为我想更好地理解它。索引的名称是library。以下是它的外观。

enter image description here

我正在尝试使用python库elasticsearch删除现有索引,下面是执行此操作的代码。

from elasticsearch import Elasticsearch
es = Elasticsearch('localhost:9200')
if es.indices.exists(index='library'):
es.delete('library')

当我执行此代码时,我最终遇到错误。
---------------------------------------------------------------------------
TransportError Traceback (most recent call last)
<ipython-input-22-2a4b3642a164> in <module>()
1 from elasticsearch import Elasticsearch
2 es = Elasticsearch('localhost:9200')
----> 3 if es.indices.exists(index='library'):
4 es.delete('library')

c:\python27\lib\site-packages\elasticsearch\client\utils.pyc in _wrapped(*args, **kwargs)
71 if p in kwargs:
72 params[p] = kwargs.pop(p)
---> 73 return func(*args, params=params, **kwargs)
74 return _wrapped
75 return _wrapper

c:\python27\lib\site-packages\elasticsearch\client\indices.pyc in exists(self, index, params)
222 raise ValueError("Empty value passed for a required argument 'index'.")
223 return self.transport.perform_request('HEAD', _make_path(index),
--> 224 params=params)
225
226 @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable',

c:\python27\lib\site-packages\elasticsearch\transport.pyc in perform_request(self, method, url, params, body)
310
311 try:
--> 312 status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
313
314 except TransportError as e:

c:\python27\lib\site-packages\elasticsearch\connection\http_urllib3.pyc in perform_request(self, method, url, params, body, timeout, ignore)
126 if not (200 <= response.status < 300) and response.status not in ignore:
127 self.log_request_fail(method, full_url, url, body, duration, response.status, raw_data)
--> 128 self._raise_error(response.status, raw_data)
129
130 self.log_request_success(method, full_url, url, body, response.status,

c:\python27\lib\site-packages\elasticsearch\connection\base.pyc in _raise_error(self, status_code, raw_data)
123 logger.warning('Undecodable raw error response from server: %s', err)
124
--> 125 raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
126
127

TransportError: TransportError(500, u'')

编辑:我试过按Slam的建议,并在下面的代码中使用
a = elasticsearch.client.IndicesClient('localhost:9200')
a.delete(index = 'library')

但是,我现在收到一个新的错误,如下所示
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-30-dc5154b36ad8> in <module>()
----> 1 a.delete(index = 'library')

c:\python27\lib\site-packages\elasticsearch\client\utils.pyc in _wrapped(*args, **kwargs)
71 if p in kwargs:
72 params[p] = kwargs.pop(p)
---> 73 return func(*args, params=params, **kwargs)
74 return _wrapped
75 return _wrapper

c:\python27\lib\site-packages\elasticsearch\client\indices.pyc in delete(self, index, params)
197 if index in SKIP_IN_PATH:
198 raise ValueError("Empty value passed for a required argument 'index'.")
--> 199 return self.transport.perform_request('DELETE', _make_path(index),
200 params=params)
201

c:\python27\lib\site-packages\elasticsearch\client\utils.pyc in transport(self)
82 @property
83 def transport(self):
---> 84 return self.client.transport
85
86 class AddonClient(NamespacedClient):

AttributeError: 'str' object has no attribute 'transport'

最佳答案

Elasticsearch.delete用于删除文档,而不是索引。

要对idexe进行操作,您需要 IndicesClient.delete 。索引客户端是在基本ES客户端之上以相同传输方式工作的抽象。调用可以

>>> es = Elasticsearch('localhost:9200')
>>> es.indices.delete(index='test')
{'acknowledged': True}

关于python - 通过Python脚本删除Elasticsearch索引时出现TransportError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48708195/

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