Insert in nodejs is slow

This is my code:

for (let i = 0; i < 500; i++) {
    await client.index({
      index: 'user',
      body: {
        name: 'mohammad',
        type: 'mobile'
      }
    })
  }

This code take time about 3 second to finish!
I tested this code via mongodb and take only 1 second.
I use docker in local for run elasticsearch

Hello,

It is not clear what is your issue here, can you provide any context?

From what you shared you are basically doing 500 requests to index the same document, this is not the correct way to evaluate indexing speed.

In a real use case you should the bulk api if you want to index multiple documents.

1 Like

This is only for test.
I have 10000 records.
And I want to save this records to user table.
And it's important for me use await, because next line I want to avoid duplication.

Yeah, but as a I said, this is not the correct way to test indexing speed. When indexing multiple documents you should use bulk to increase the indexing speed, if you do not use bulk you will have one API request for every document and this can be slow depending on the case/size of document.

If you want to avoid duplicate documents you need to use a custom _id value.

1 Like

Thank you
I have a realtime service and per second I receive more than 2000 message
So I want to check if not exists, insert this record
I can't use bulk, because maybe in this message have duplicated data
My structure is:

get new message
check if exists
if exists => update it
then => insert it

How is best solution in nodejs for it?

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