Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bulk Update on ElasticSearch using NEST

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?

like image 571
Sasi Avatar asked Jan 25 '16 17:01

Sasi


1 Answers

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.

like image 64
Russ Cam Avatar answered Oct 17 '22 08:10

Russ Cam