var people = new[] { new Person { Id = 1, FirstName = "Martijn", LastName = "Laarman"}, new Person { Id = 2, FirstName = "Stuart" ,LastName = "Cam"}, new Person { Id = 3, FirstName = "Russ", LastName = "Cam"} };
var indexManyResponse = _client.IndexMany(people);
if (indexManyResponse.Errors) //通过检查 Errors,可以让你知道批量操作是否有错误。 { foreach (var itemWithError in indexManyResponse.ItemsWithErrors) { //你可以通过遍历 ItemsWithErrors 来获取错误信息。 Console.WriteLine("Failed to index document {0}: {1}", itemWithError.Id, itemWithError.Error); } }
// Alternatively, documents can be indexed asynchronously var indexManyAsyncResponse = await _client.IndexManyAsync(people);
请求 URL 如下:
1 2 3 4 5 6 7
POST /_bulk {"index":{"_id":"1","_index":"person"}} {"id":1,"firstName":"Martijn","lastName":"Laarman"} {"index":{"_id":"2","_index":"person"}} {"id":2,"firstName":"Stuart","lastName":"Cam"} {"index":{"_id":"3","_index":"person"}} {"id":3,"firstName":"Russ","lastName":"Cam"}
var people = new[] { new Person { Id = 1, FirstName = "Martijn", LastName = "Laarman"}, new Person { Id = 2, FirstName = "Stuart" ,LastName = "Cam"}, new Person { Id = 3, FirstName = "Russ", LastName = "Cam"} };
//同步 var bulkIndexResponse = _client.Bulk(b => b .Index("person") .IndexMany(people));
// 异步 var asyncBulkIndexResponse = await _client.BulkAsync(b => b .Index("person") .IndexMany(people));
请求 URL 为:
1 2 3 4 5 6 7
POST /person/_bulk {"index":{"_id":"1"}} {"id":1,"firstName":"Martijn","lastName":"Laarman"} {"index":{"_id":"2"}} {"id":2,"firstName":"Stuart","lastName":"Cam"} {"index":{"_id":"3"}} {"id":3,"firstName":"Russ","lastName":"Cam"}
var people = new[] { new Person { Id = 1, FirstName = "Martijn", LastName = "Laarman"}, new Person { Id = 2, FirstName = "Stuart" ,LastName = "Cam"}, new Person { Id = 3, FirstName = "Russ", LastName = "Cam"} };
var bulkAllObserver = _client.BulkAll(people, b => b .Index("person") .BackOffTime("30s")//集群繁忙,报429错误码的时候,等待多久进行重试 .BackOffRetries(2) //重试次数 .RefreshOnCompleted() // .MaxDegreeOfParallelism(Environment.ProcessorCount) .Size(1000) //每次 bulk 请求时的文档数量。 ).Wait(TimeSpan.FromMinutes(15), next => {//执行索引并等待 15min,BulkAll 请求虽然时异步的,但是是一个阻塞操作。