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?


You are passing the bulk request to the ElasticsearchClient ie 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.

链接地址: http://www.djcxy.com/p/31616.html

上一篇: SonarLint超级慢

下一篇: 使用NEST批量更新ElasticSearch