我需要通过NEST库将大量来自List<Person>
的条目放入elasticsearch中。
我可以使用下面的循环和代码逐个放置:
var person = new Person
{
Id = "1",
Firstname = "Martijn",
Lastname = "Laarman"
};
var index = client.Index(person);
但似乎它的效果非常慢。有没有办法通过NEST更快地做到这一点?
答案 0 :(得分:5)
查看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。因此,当您遍历项目以进行索引时,不要将每个对象单独添加到索引中,而是使用此函数将集合传递给它,它将为您批量索引对象。