Search code examples
c#.netelasticsearchnest

How could I bulk delete docs using a custom type field with NEST for Elastic?


How would I go upon using BulkDeleteDescriptor to delete documents that match a certain criteria using Elastic's NEST?

Specifically, I have a custom type field and I want to delete all docs that have a specific type and any other criteria such as ID, a document would look like this:

{
  "_index": "testIndex",
  "_id": "someId",
  "_score": 1.0,
  "_source": {
    "bookName": "A Book",
    "type": "action"
  }
}

When Elastic/NEST had types, I would do this:

descriptor.Delete<object>(i => i
    .Index("testIndex") 
    .Type(type)
    .Id(id));

Can't seem to find much on NEST documentation..


Solution

  • Bulk delete is a great way to delete a bunch of documents when you have ids of these documents upfront. For a case when you don't know ids, I would suggest delete by query API which will delete documents matching query criteria.

    With this API you will be able to specify both delete criteria (id and type) at the same time

    POST your_index/_delete_by_query
    {
      "query": {
        "bool": {
            "must": [
            {
                "term": {
                    "_id": x
                }
            },
            {
                "term": {
                    "type": y
                }
            }]
        }
      }
    }