gpt4 book ai didi

java - elasticsearch IT 节省等待

转载 作者:行者123 更新时间:2023-12-01 08:48:49 25 4
gpt4 key购买 nike

我想使用elasticsearch进行集成测试,在尝试进行测试以查看对象是否成功保存和删除时,我注意到ES没有立即索引文档,而是必须等待一点(比如1秒) ),有没有办法告诉我的保存方法立即建立索引?

这是我的测试:

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

正如你所看到的,我使用了一个锁对象让我的测试等待一段时间,但我不确定是否还有其他方法可以做到这一点(我正在学习如何编码,请耐心等待 xD)

这是我的保存方法:

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

最佳答案

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

您可以通过RefreshRequestBuilder强制刷新

关于java - elasticsearch IT 节省等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42519437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com