IndexManyAsync 和 BulkAsync 有什么区别?

What is the difference between IndexManyAsync and BulkAsync?

我有这两种方法可以索引产品。 NEST 中的 IndexManyAsync 和 BulkAsync 这两个选项有什么区别?

    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 只是 NEST 客户端中的一种方便方法,它将在内部使用 Bulk

来自docs

These methods are specific to the NEST client to provide a convenient shortcut to indexing multiple documents using the _bulk endpoint.

如有疑问,可以查看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;
        }