1

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);
    }
}

2 Answers 2

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

You can force refresh by RefreshRequestBuilder

Sign up to request clarification or add additional context in comments.

Comments

0

You can refresh you index manually after you insert.

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

1 Comment

ohh nice, this is what i was looking for, but how do i do it in java?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.