2

Hello I am new to python and elasticsearch. On my local I have setup Elasticsearch and have added data to it. http://127.0.0.1:9200/index_data/type_data.

I want to delete some _ids from the type_data. suppose the list of _ids are x= ['a','b','c'.'d'] that i want to delete.

curl -XDELETE 'localhost:9200/index_data/type_data/a?pretty'

using this command I was able to delete a particular _id from elasticsearch but how do execute this curl request using python?

Is it possible to delete the entire type_data using python?

why is this code not working?

from elasticsearch import Elasticsearch 
es = Elasticsearch()
request_body = {
    "query": {
        "ids": {
            "values": ['a','b','c','d','e','f']
        }
    }
}
es.delete_by_query(index=es_index, body=request_body)

i am using Elasticsearch version 6.1.0. elasticsearch-py version 5.4.0

Please Help me!

0

1 Answer 1

1

If there are a lot of ids, try parallel_bulk deletion in python: documentaion here: http://elasticsearch-py.readthedocs.io/en/master/helpers.html#elasticsearch.helpers.parallel_bulk

from elasticsearch import Elasticsearch
from elasticsearch import helpers

es = Elasticsearch()
index_name = es_index
doc_type = your_doc_type
ids = ['a','b','c','d','e','f']


def generate_actions(ids):
    for i in ids:
        yield {
            '_op_type': 'delete',
            '_index': index_name,
            '_type': doc_type,
            '_id': i
        }


for success, info in helpers.parallel_bulk(client=es, actions=generate_actions(ids), thread_count=4):
    if not success: 
        print('Doc failed', info)
Sign up to request clarification or add additional context in comments.

1 Comment

however for retrieve all documents with a specific data_type the best practice is to use the scan query - scroll api. In Python: helpers.scan(es, query={"query": { "query": { "match_all": {} } }, index="es_index", doc_type="your_data_type" )

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.