我在使用批量API(C#NEST v5.4)插入文档时遇到问题。我有一个文档数组,在数组中有我的ID。
我的代码是:
documents = documents .ToArray(); Client.Bulk(bd => bd.IndexMany(documents, (descriptor, s) => descriptor.Index(indexName)));
如何 使用描述符手动 插入 _id ?
提前致谢!
您可以使用_id类似的方法在上设置索引名称BulkDescriptor。鉴于以下POCO
_id
BulkDescriptor
public class Message { public string Content { get; set; } }
例如,使用递增计数器设置ID
var documents = new[] { new Message { Content = "message 1" }, new Message { Content = "another message" }, new Message { Content = "yet another one" } }; var indexName = "index-name"; var id = 0; client.Bulk(bd => bd .IndexMany(documents, (descriptor, s) => descriptor.Index(indexName).Id(++id)));
产生以下请求
POST http://localhost:9200/_bulk {"index":{"_index":"index-name","_type":"message","_id":1}} {"content":"message 1"} {"index":{"_index":"index-name","_type":"message","_id":2}} {"content":"another message"} {"index":{"_index":"index-name","_type":"message","_id":3}} {"content":"yet another one"}