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
ElasticsearchClienti.e.ElasticClient.Raw, when you should be passing it toElasticClient.BulkAsync()orElasticClient.Bulk()which can accept a bulk request type.Using
BulkRequestorBulkDescriptorare 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,
BulkDescriptoris used outside of the context of the fluent API, but bothBulkRequestandBulkDescriptorimplementIBulkRequestso can be passed toElasticClient.Bulk(IBulkRequest).As for which to use, in this case it doesn't matter so whichever you prefer.