Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete several documents by ID in one operation using Elasticsearch Nest

I am building some abstraction functions for my application to call, which will hit elasticsearch through Nest. One of such functions is a Delete(string id) call, which is easy to accomplish. I have done this as follows:

public void Delete(string id)
{
    esClient.Delete(id);
}

Now let's say I want to do the same thing, but operate on several documents simultaneously. My original hunch was to do something like this:

public void Delete(IEnumerable<string> ids)
{
    esClient.DeleteMany(ids); // won't compile
}

As my comment states, doing this won't compile. What is the proper way of batch deleting documents by ID in Nest?

like image 969
Jim Avatar asked Jun 24 '15 14:06

Jim


2 Answers

To use esClient.DeleteMany(..) you have to pass collection of objects to delete.

var objectsToDelete = new List<YourType> {.. };
var bulkResponse = client.DeleteMany<YourType>(objectsToDelete);

You can get around this by using following code:

var ids = new List<string> {"1", "2", "3"};
var bulkResponse = client.DeleteMany<YourType>(ids.Select(x => new YourType { Id = x }));

Third option, use bulk delete:

var bulkResponse = client.Bulk(new BulkRequest
{
    Operations = ids.Select(x => new BulkDeleteOperation<YourType>(x)).Cast<IBulkOperation>().ToList()
});
like image 54
Rob Avatar answered Oct 06 '22 01:10

Rob


I was working on a .NET client for ElasticSearch 5.x, and I was fortunate to have the following code running (as well as succeeding all unit tests) for bulk deletion using ID's:

  //IList<string> ids = ...

  var descriptor = new BulkDescriptor();

  foreach (var id in ids.Where(x => !string.IsNullOrWhiteSpace(x)))
    descriptor.Delete<T>(x => x
        .Id(id))
      .Refresh(Refresh.WaitFor);

  var response = await _client.BulkAsync(descriptor);
like image 36
Burak Tasci Avatar answered Oct 05 '22 23:10

Burak Tasci