I
I
Igor Che2017-12-14 13:02:59
Python
Igor Che, 2017-12-14 13:02:59

How to delete documents matching a condition in Elasticsearch?

I used to use elasticutils in my Python script.
Now I am rewriting it to use the elasticsearch-dsl module.
Here is the old code:

from elasticutils import get_es

es = get_es(urls=config.ELASTIC_SEARCH_HOSTS, timeout=timeout)
es.delete_by_query(
            'posts',
            'post',
            {'query': {'term': {'user_id': user.id}}})

There is nothing in elasticsearch-dsl for such an operation.
How to delete documents by condition using elasticsearch module?
Used by Elasticsearch 2.4.
The delete_by_query module is cut out of the second elastic, it is not desirable to cut it back.
As far as I understand, you need to delete via bulk. But I don't understand how to build a query.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Cheremisin, 2017-12-14
@chewarer

Try it through a regular client (didn't check the code, pulled it from the source, but it should work)

from elasticsearch import Elasticsearch, helpers

es = Elasticsearch()

def mybulk_delete(q, index, doc_type):
   res = helpers.scan(es,
                       query={"query": q},
                       index=index, doc_type= doc_type)
    for r in res:
       yield {"delete" : { "_index" : r["_index"], "_type" : r["_type"], "_id" : r["_id"] } }

q = {"query":{"term":{"user":"kimchy"}}}
k = (i for i in mybulk_delete(q,"myindex", "mytype"))
helpers.bulk(es, k, refresh = True)

E
Evgeny Panin, 2018-02-22
@varenich

For example, like this:

POST finrep/securities/_delete_by_query
{
  "query": { 
    "match": {
      "company_name": "ТрансКонтейнер"
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question