3

I want to delete a particular '_id' in elasticsearch but its not working using elasticsearch python api client.

doc = {'system_caused': 'office', 'division': 'National', 'addt_notes': '', 'pg': '100', 'date': '2016/02/15 08:56',
               'duration': '15 minutes', 'outage_caused': 'Scheduled', 'ticket_num': '1234', 'ticket_type': 'JIRA', 'error_count': '1,000 - 5,000'}
self.es = Elasticsearch(['localhost'], verify_certs=True)

result = self.es.index(index='tickets', doc_type='tickets', body=doc)


doc = {'_id': result['_id']}
result = self.es.delete(
        index='tickets', doc_type='tickets', **doc)

The inserts are working fine but the delete is failing.

Here is the error I get -

TypeError: delete() got an unexpected keyword argument '_id'
3
  • It shouldn't be id? github.com/elastic/elasticsearch-py/blob/master/elasticsearch/… Commented Feb 15, 2016 at 20:20
  • You should pass only doc id instead of doc. See documentation for allowed arguments Commented Feb 15, 2016 at 20:26
  • I don't know your use-case, but I want to mention that delete operations are pretty slow. It is, depending on the case, better to update a document by setting a bool which you filter afterwards in queries. Commented Feb 16, 2016 at 17:59

3 Answers 3

1

According to the source code the input parameters for delete include id, but not _id. Check out the code here.

Sign up to request clarification or add additional context in comments.

Comments

0

When you use the ** operator in front of a dict, it expands to keyword arguments. So we can rewrite it as:

result = self.es.delete(
    index='tickets', doc_type='tickets',
    _id=result['_id'])

Thus you are passing _id as one of the keyword parameters. As @Forge said, I assume you meant to use id in the delete call.

Comments

0
from elasticsearch import Elasticsearch
from elasticsearch import helpers
from elasticsearch_dsl import connections
import pandas as pd

# initialize list of lists
data = [['tom', 10, 'NY'], ['nick', 15, 'NY'], ['juli', 14, 'NY'], ['akshay', 30, 'IND'], ['Amit', 14, 'IND']]

# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Country'])

from elasticsearch import Elasticsearch
from elasticsearch import helpers

##connection to elastic search cluster

es_client = connections.create_connection(hosts=['http://localhost:9200/'])
def doc_generator(df):
    df_iter = df.iterrows()
    for index, document in df_iter:
        yield {
                "_index": 'age_sample',
                "_type": "_doc",
                "_source": document,
            }

helpers.bulk(es_client, doc_generator(df))

from elasticsearch_dsl import Search
s = Search(index='age_sample').query("match", _id='V5A3Y34Bq-Uo6Q7qyfBJ')
response = s.delete()

1 Comment

Hi and thanks for the answer. It would be great if you could explain to us how and why your code solves the OP's problem as code itself is not always easy to read.

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.