Bulk Update on ElasticSearch using NEST
Asked Answered
C

1

18

I am trying to replacing the documents on ES using NEST. I am seeing the following options are available.

Option #1:

var documents = new List<dynamic>();

`var blkOperations = documents.Select(doc => new BulkIndexOperation<T>`(doc)).Cast<IBulkOperation>().ToList();   
var blkRequest = new BulkRequest()
{
    Refresh = true,
    Index = indexName,
    Type = typeName,
    Consistency = Consistency.One,
    Operations = blkOperations
};
var response1 = _client.Raw.BulkAsync<T>(blkRequest);

Option #2:

var descriptor = new BulkDescriptor();
foreach (var eachDoc in document)
{
    var doc = eachDoc;
    descriptor.Index<T>(i => i
        .Index(indexName)
        .Type(typeName)
        .Document(doc));
}
var response = await _client.Raw.BulkAsync<T>(descriptor);

So can anyone tell me which one is better or any other option to do bulk updates or deletes using NEST?

Coincidence answered 25/1, 2016 at 17:1 Comment(0)
B
9

You are passing the bulk request to the ElasticsearchClient i.e. ElasticClient.Raw, when you should be passing it to ElasticClient.BulkAsync() or ElasticClient.Bulk() which can accept a bulk request type.

Using BulkRequest or BulkDescriptor are two different approaches that are offered by NEST for writing queries; the former uses an Object Initializer Syntax for building up a request object while the latter is used within the Fluent API to build a request using lambda expressions.

In your example, BulkDescriptor is used outside of the context of the fluent API, but both BulkRequest and BulkDescriptor implement IBulkRequest so can be passed to ElasticClient.Bulk(IBulkRequest).

As for which to use, in this case it doesn't matter so whichever you prefer.

Beanie answered 1/2, 2016 at 22:48 Comment(2)
Thank you Russ Cam for your comment. It make sense I used BulkDescriptor. do you have any idea on how to rollback if any of bulk update fails ?.Coincidence
You mean roll back all of the updates in one bulk request? In short, you can't as each update within the bulk request is independent of others i.e. it is not transactional. You can retry failed operations if necessary but no built in way to roll backBeanie

© 2022 - 2024 — McMap. All rights reserved.