gpt4 book ai didi

c# - 在 ElasticSearch 中使用 Bulk.IndexMany 指定 _id 字段

转载 作者:太空宇宙 更新时间:2023-11-03 18:22:57 25 4
gpt4 key购买 nike

我在使用批量 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"}

关于c# - 在 ElasticSearch 中使用 Bulk.IndexMany 指定 _id 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45298044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com