0

I have installed elasticsearch2.1.1 in my windows machine and i have an elasticsearch index which is hosted in my machine locally as i am trying to understand it better. Name of the index is library. Below is how it looks like.

enter image description here

I am trying to delete the existing index using python library elasticsearch and below is the code to do so.

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

When i execute this code, i end up getting below error.

---------------------------------------------------------------------------
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'')

Edit: I tried as per suggestion by Slam and used below code

a = elasticsearch.client.IndicesClient('localhost:9200')
a.delete(index = 'library')

However, i am now getting a new error as follows

---------------------------------------------------------------------------
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 Answer 1

1

Elasticsearch.delete is made for deleting documents, not indexes.

To operate over idexes, you need IndicesClient.delete. Indices client is abstraction that works on top of basic ES client, with same transport. Invocation can be

>>> es = Elasticsearch('localhost:9200')
>>> es.indices.delete(index='test')
{'acknowledged': True}
Sign up to request clarification or add additional context in comments.

2 Comments

Its not working either. I have added the new error and code
@Enthusiast I've updated answer with example signature. Pls check if it works for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.