How to deal with elasticsearch delay when doing unit test?

There is some delay in the elasticsearch search from requesting indexing of a document so that the document becomes searchable. I want to know how to handle this in unit test, for example, indexing a document first, and then checking if it is actually indexed. Now I just use the Thread.sleep () call to delay some time (I use a simple junit class), say 2 seconds. This is normal? Or are there better solutions?

+8
unit-testing elasticsearch delay
source share
1 answer

You can update the spaces after you click on elasticsearch.

By doing this

$ curl -XPOST 'http://localhost:9200/index1,index2/_refresh' 

The update API allows you to explicitly update one or more indexes, making all operations performed since the last update available for search. The possibilities (nearby) in real time depend on the index mechanism used. For example, the internal one requires an update to be called, but by default an update is scheduled periodically.

See @Elasticsearch website

using java api,

  client.admin().indices().prepareRefresh().execute().actionGet() 

using the JavaScript client:

 client.indices.refresh({index : 'myIndex'}); 
+8
source share

All Articles