2

I am new to ElasticSearch. I am trying to delete a group of data from http://localhost:9200/es_index/es_type using python.

suppose x=['a','b','c','d','e','f'] is a list of _ids that I want to delete

Code:

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)

But when I check http://localhost:9200/es_index/es_type the data is still there.

3
  • Which version of ES are you running? Commented Dec 30, 2017 at 11:01
  • the version is 6.1.0 Commented Dec 30, 2017 at 11:04
  • 1
    And which version of elasticsearch-py? Commented Dec 30, 2017 at 11:31

3 Answers 3

2

Please specify your ES and elasticsearch-py version in comments. Also you forgot to mention the value of es_index in your code, so make sure to pass the right one.

If the index is properly set, issue with delete_by_query usually comes from the query itself. Try to run a simple search with the same body and check that you get the desired results. It is always wise to do so to prevent mistakes.

In your case you know the id, so it is more appropriate to use es.delete and pass the id value :

for id in ids:
    es.delete(es_index=index, doc_type=_type, id=id)

With many documents you should consider using bulk helper to improve performance. See the answer here.

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

Comments

1

Another option would be delete_by_query, where you can directly pass a list of ids.

es = Elasticsearch()

def delete_by_id(index, ids):
    query = {"query": {"terms": {"Id":ids}}}
    es.delete_by_query(index=index, body=query)

Comments

0

We can follow the Delete by Query section on documentation and make a simple POST request to your index:

let's say that your index is named 'index_test'. So, you can send a POST request to your index_url + '_delete_by_query', so the content (document) of your index will be deleted but the index itself will be preserved:

curl -X POST http://elasticAddress:9200/index_test/_delete_by_query

With this, you will not lose your visualization content as the index is preserved, but the document (content) is updated.

Comments

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.