Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch - How to delete a list of documents?

I have an array of _id.

On this page I found out how to retrieve a list of documents from it :

GET ads/_mget
{
   "ids": [ "586213440e7d2c7f10fe2574",
            "586213440e7d2c7f10fe2575",
            "586213450e7d2c7f10fe2576",
            "586213450e7d2c7f10fe2577" ]
}

This works and returns a list of 4 full documents, as expected.

(sidenote) I find it weird to have to write "ids" in the query, when it actually acts on the "_id" field. (end sidenote)

Now I can't figure out how to DELETE these documents from the same _id list.

I tried DELETE ads/_mget but I get an error : No handler found for uri [/ads/_mget] and method [DELETE]

I tried _mdelete instead of _mget but it doesn't seem to exist.

I also tried

DELETE ads
{
   "ids": [ "586213440e7d2c7f10fe2574",
            "586213440e7d2c7f10fe2575",
            "586213450e7d2c7f10fe2576",
            "586213450e7d2c7f10fe2577" ]
}

...but this... just deletes EVERYTHING and I have to reindex the database.

like image 574
Jeremy Thille Avatar asked Feb 26 '17 08:02

Jeremy Thille


People also ask

Does deleting index delete documents Elasticsearch?

Deleting an index deletes its documents, shards, and metadata.

How do I remove all data from elastic index?

To delete all data from an Elasticsearch index, simply navigate to the "Index Settings" page and click the "Delete Index" button.

How does Elasticsearch deletion work?

Elasticsearch lets you specify time-to-live for each added document, which means after that time has passed, the document is automatically deleted. This is very useful for certain applications, but it will cause heavy deletions over time.


1 Answers

You can always use feature of Delete By Query and supply payload like:

POST ads/_delete_by_query
{
    "query" : {
        "terms" : {
            "_id" : 
              [ "586213440e7d2c7f10fe2574",
              "586213440e7d2c7f10fe2575",
              "586213450e7d2c7f10fe2576",
              "586213450e7d2c7f10fe2577" ]
        }
    }
}

For more infromation about terms query please follow https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html

like image 173
Pavel Vasilev Avatar answered Oct 21 '22 10:10

Pavel Vasilev