gpt4 book ai didi

c# - 如何通过 NEST c# 将列表索引到 elasticsearch

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

我需要从 List<Person> 中输入很多条目通过 NEST 库进入 elasticsearch。我可以使用下面的循环和代码一个一个地放置:

var person = new Person
{
Id = "1",
Firstname = "Martijn",
Lastname = "Laarman"
};

var index = client.Index(person);

但是看起来它运行起来真的很慢。有没有办法通过 NEST 更快地做到这一点?

最佳答案

看看 BulkDescriptor 对象。

然后你可以做如下的事情:

private readonly ElasticClient _client; //needs to be initialized in your code
public void Index(IEnumerable<Person> documents)
{
var bulkIndexer = new BulkDescriptor();

foreach (var document in documents)
{
bulkIndexer.Index<Person>(i => i
.Document(document)
.Id(document.SearchDocumentId)
.Index(_indexName));
}

_client.Bulk(bulkIndexer);
}

Index 函数采用您的类型的 IEnumerable。因此,当您循环遍历要索引的项目时,不要将每个对象单独添加到索引中,而是使用此函数将集合传递给,它会为您批量索引对象。

关于c# - 如何通过 NEST c# 将列表索引到 elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41541362/

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