Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch Java API asynchronous writing

I'm relatively new to ElasticSearch and I've noticed when you start elasticsearch Java Client, it starts a large number of threads (~50).

I was trying to leverage this, buy couldn't find a way to make an async write (index) to ES.

The official API suggestion is to use:

IndexResponse response = client.prepareIndex(indexName, documentName)
            .setSource(mapper.writeValueAsString(data))
            .get();

Even though I have this running on a new thread it is still blocking as it waits for the response, containing the new created ID and more.

Is it possible to write to ES in an asynchronous manner, without having to create another 50 local threads to match the 50 internal ones of ES?

like image 385
SDekov Avatar asked Nov 28 '25 05:11

SDekov


1 Answers

If anyone stumbles across this, the solution was to use .execute(), which returns a ListenableActionFuture<Response>.

e.g.

client.prepareIndex(indexName, documentName)
        .setSource(mapper.writeValueAsString(data))
        .execute();
like image 177
SDekov Avatar answered Nov 30 '25 20:11

SDekov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!