Reputation: 3740
I need to put a lot of entries from List<Person>
into elasticsearch via NEST library.
I can put one by one using loop and code below:
var person = new Person
{
Id = "1",
Firstname = "Martijn",
Lastname = "Laarman"
};
var index = client.Index(person);
But seems like it works really slow. Is there any way for doing that more quickly via NEST?
Upvotes: 1
Views: 1822
Reputation: 7803
Take a look at the BulkDescriptor
object.
Then you can do something as follow:
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);
}
The function Index
, takes an IEnumerable of your type. So when you loop through your items to index, instead of adding each object to the index individually, use this function to pass the collection to, and it will bulk index the objects for you.
Upvotes: 6