通过Python脚本删除elasticsearch索引时的TransportError

时间:2018-02-09 14:37:52

标签: python elasticsearch

我已经在我的elasticsearch2.1.1计算机中安装了windows,我有一个弹性搜索索引,它本地托管在我的计算机上,因为我试图更好地理解它。索引的名称是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'

1 个答案:

答案 0 :(得分:1)

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

要对idex进行操作,您需要IndicesClient.delete。 Indices客户端是在基本ES客户端之上工作的抽象,具有相同的传输。调用可以是

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