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

I am creating some abstraction functions for my application to call, which will remove elasticsearch via Nest. One such feature is a call Delete(string id)that is easy to complete. I did it as follows:

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

Now let me say that I want to do the same, but work with several documents at the same time. My initial hunch was to do something like this:

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

As stated in my comment, this will not compile. What is the correct way to delete documents by ID in Nest?

+4
source share
2 answers

esClient.DeleteMany(..), .

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

, :

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

, :

var bulkResponse = client.Bulk(new BulkRequest
{
    Operations = ids.Select(x => new BulkDeleteOperation<YourType>(x)).Cast<IBulkOperation>().ToList()
});
+12

.NET- ElasticSearch 5.x, ( ) :

  //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);
0

All Articles