elasticsearch IT save wait

i want to do an integration test using elasticsearch, while trying to do the test to see if the object was successfully saved and deleted i noticed that ES didnt instantly index the document, but had to wait a little bit (like 1 sec), is there a way to tell my save method to index istantly?

here is my test:

@Test
public void SurveySaveDelete(){
    Object lock = new Object();
    synchronized (lock) {
        Survey survey = new Survey();
        survey.setEvaluated("evaluated");
        survey.setEvaluator("evaluator");
        survey.setTimestamp("2017-1-30");
        Survey returnedSaveSurvey = new Survey();
        Survey foundSurvey = new Survey();
        returnedSaveSurvey = elasticsearchSurveyRepository.saveSurvey(survey);

        try {
            lock.wait(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        foundSurvey = elasticsearchSurveyRepository.findSurvey("evaluator", "evaluated", "2017-1-30");
        assertEquals("evaluated", foundSurvey.getEvaluated());
        assertEquals("evaluator", foundSurvey.getEvaluator());
        assertEquals("2017-1-30", foundSurvey.getTimestamp());

        assertEquals("evaluated", returnedSaveSurvey.getEvaluated());
        assertEquals("evaluator", returnedSaveSurvey.getEvaluator());
        assertEquals("2017-1-30", returnedSaveSurvey.getTimestamp());
        boolean wasDeleted;
        wasDeleted = elasticsearchSurveyRepository.deleteSurvey("evaluator", "evaluated", "2017-1-30");
        assertEquals(true, wasDeleted);
    }
}

as you can see i used a lock object to make my test wait for a bit, but I'm unsure if there is another way to do that (i'm learning how to code be patient with me please xD)

and here is my method to save:

 @Override
public Survey saveSurvey(Survey survey) {
    Index index = new Index.Builder(survey).index(surveyIndexName).type(surveyTypeName).build();
    try {
        client.execute(index);
        return survey;
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

Upvotes: 1

Views: 367

Answers (2)

chengpohi
chengpohi

Reputation: 14227

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

You can force refresh by RefreshRequestBuilder

Upvotes: 2

paqash
paqash

Reputation: 2314

You can refresh you index manually after you insert.

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html

Upvotes: 0

Related Questions