在 ElasticSearch 中使用 Bulk.IndexMany 指定 _id 字段
Specify the _id field using Bulk.IndexMany in ElasticSearch
我在使用批量 API (C# NEST v5.4) 插入文档时遇到问题。我有一个文件数组,数组里面有我的 ID。
我的代码是:
documents = documents .ToArray();
Client.Bulk(bd =>
bd.IndexMany(documents,
(descriptor, s) => descriptor.Index(indexName)));
如何使用描述符手动插入_id?
提前致谢!
您可以像在 BulkDescriptor
上设置索引名称一样设置 _id
。鉴于以下 POCO
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"}
我在使用批量 API (C# NEST v5.4) 插入文档时遇到问题。我有一个文件数组,数组里面有我的 ID。
我的代码是:
documents = documents .ToArray();
Client.Bulk(bd =>
bd.IndexMany(documents,
(descriptor, s) => descriptor.Index(indexName)));
如何使用描述符手动插入_id?
提前致谢!
您可以像在 BulkDescriptor
上设置索引名称一样设置 _id
。鉴于以下 POCO
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"}