Search code examples
elasticsearchelastic4s

Why ElasticSearch Java Client index Future completes before the record is searchable?


I'm using elastic4s client that returns a Future in response to index request and when that future completes I still have to do Thread.sleep(1000) before I can query for that indexed record. Mostly it is exactly 1 second. Is there an elasticsearch setting that I can change so that when the Future completes the record will be available?

I tried to use the java client directly client.prepareIndex....execute().actionGet(); and it ends up exactly the same way, I have to call Thread.sleep(1000)

Is there any settings I can change for the record to be ready after the future completes?

execute(index into(foo, bar) id uuid fields baz).await
Thread.sleep(1000) // This is mandatory for search to find it
execute {search in foo}.await // returns empty without Thread.sleep(1000)

Solution

  • It sounds like you may be having to wait for the default index refresh interval to come into play before you can query the newly indexed data. The refresh interval is 1 second by default and can be changed with the following

    curl -XPUT localhost:9200/test/_settings -d '{
        "index" : {
            "refresh_interval" : "1s"
        } }'
    

    Alternatively, you can refresh the shard after the indexing operation by including the refresh parameter in the query string of the index operation. This may be more useful than changing the refresh interval globally

    curl -XPUT 'http://localhost:9200/{index}/{type}/{id}?refresh=true' -d '{
      "property" : "value"
    }'