1

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?

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. Works faster that in the loop more than 100 times. In the loop I spent more that hour for input 144k entities. By using bulk input I've spent 81 second.
Glad I could help :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.