I have this 2 methods which will index products. What is the difference between these 2 options, IndexManyAsync and BulkAsync in NEST?
public async Task SaveManyAsync(Product[] products)
{
var result = await _elasticClient.IndexManyAsync(products);
}
public async Task SaveBulkAsync(Product[] products)
{
var result = await _elasticClient.BulkAsync(b => b.Index("products").IndexMany(products));
}
IndexManyAsync
is just a convenient method in NEST client, that will use the Bulk
internally.
From docs:
These methods are specific to the NEST client to provide a convenient shortcut to indexing multiple documents using the _bulk endpoint.
If in doubt, you can check the source code:
/// <summary>
/// Shortcut into the Bulk call that indexes the specified objects
/// <para> </para>
/// https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
/// </summary>
/// <param name="client"></param>
/// <typeparam name="T">The type used to infer the default index and typename</typeparam>
/// <param name="objects">List of objects to index, Id will be inferred (Id property or IdProperty attribute on type)</param>
/// <param name="index">Override the inferred indexname for T</param>
/// <param name="type">Override the inferred typename for T</param>
public static Task<BulkResponse> IndexManyAsync<T>(this IElasticClient client, IEnumerable<T> objects, IndexName index = null,
CancellationToken cancellationToken = default
)
where T : class
{
var bulkRequest = CreateIndexBulkRequest(objects, index);
return client.BulkAsync(bulkRequest, cancellationToken);
}
private static BulkRequest CreateIndexBulkRequest<T>(IEnumerable<T> objects, IndexName index) where T : class
{
var bulkRequest = new BulkRequest(index);
var indexOps = @objects
.NotEmpty(nameof(objects))
.Select(o => new BulkIndexOperation<T>(o))
.Cast<IBulkOperation>()
.ToList();
bulkRequest.Operations = new BulkOperationsCollection<IBulkOperation>(indexOps);
return bulkRequest;
}