What is the correct way to insert document in elastic search using multiple documents using multiple threads in c#

Hi All,

I'm using multiple threads in c# to isert documents in c# as below but some times document is not inserted into elstic search search.

var indexResponse = elasticClient.IndexDocument(testReport);

But if I try the following code which iterates through all documents in mongo db and then inserts into elastic all documents are getting inserted successfully. Please help to resolve this issue

await testcoll.Find(new BsonDocument()).ForEachAsync(async document =>
{
var indexResponse = elasticClient.IndexDocument(testReport);
})

Welcome!

How do you know that it's not inserted?
Are you running a GET by ID request? A Search? Are you getting an error?

A common mistake is to forget to run a refresh call after the document has been inserted. It's happening most of the time in integration tests.

For example, have a look at this (Java) code:

Note the refresh part:

client.indices().refresh(rr -> rr.index("test"));

Will it not delay the indexing process if we refresh the index for thousands of documents

It will add some delay, yes. But you did not answer any of my questions...

  • How do you know that it's not inserted?
  • Are you running a GET by ID request?
  • A Search?
  • Are you getting an error?

Yes to know if it is not inserted I'm getting by ID.

GET by id does not need a refresh. See Get API | Elasticsearch Guide [8.10] | Elastic

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.