我在使用原生elasticsearch java api时遇到问题。 我想创建一个方法来通过其name属性搜索Object。 到目前为止很容易,之后我想为这个方法创建一个JUnit测试,这里就开始了问题。
@Test
public void nameSearchTest() throws ElasticSearchUnavailableException, IOException{
String nameToSearch = "fuzzyText";
TrainingToCreate t = new TrainingToCreate();
t.setName(nameToSearch);
//Create two Trainings to find sth
String id1 = ElasticIndexer.index(t);
String id2 = ElasticIndexer.index(t);
//For creating delay, throws Exception if id doesn't exist
ElasticGetter.getTrainingById(id1);
ElasticGetter.getTrainingById(id2);
int hits = 0;
ArrayList<Training> trainings = ElasticSearch.fuzzySearchTrainingByName(nameToSearch, Integer.MAX_VALUE, 0);
System.out.println("First id: " + id1);
System.out.println("Second id: " + id2);
String idOfTraining;
if(trainings.size() == 0){
System.out.println("Zero hits could be found.");
}
//just for printing id's of results
//-------------------------------------------------
for (int i = 0; i < trainings.size(); i++) {
idOfTraining = trainings.get(i).getId();
System.out.println("Training: "+i+" id: "+ idOfTraining);
}
//-------------------------------------------------
for (Training training : trainings) {
if(training.getId().equals(id1)||training.getId().equals(id2)){
hits++;
}
}
assertTrue(hits>=2);
ElasticDelete.deleteTrainingById(id1);
ElasticDelete.deleteTrainingById(id2);
}
有时这个测试没有问题,有时候搜索结果什么都没有,即使我已经创建了一些文件以确保可以找到某些东西。但是如果我在弹性搜索数据库中查看文档存在,那么我想我的实现是不对的,或者搜索api有严重的延迟。
这里是正在测试的代码:
public static ArrayList<Training> fuzzySearchTrainingByName(String name, int size, int offset) throws ElasticSearchUnavailableException, JsonParseException, JsonMappingException, IOException {
Client client = clientFactory.getClient(configService.getConfig().getElasticSearchIp(), configService
.getConfig().getElasticSearchPort());
return ElasticSearch.fuzzySearchDocument(client, "trainings", "training", "name", name, size, offset);
}
private static ArrayList<Training> fuzzySearchDocument(Client client, String index, String type, String field, String value, int size, int offset) throws JsonParseException, JsonMappingException, IOException {
QueryBuilder query = fuzzyQuery(field, value);
SearchResponse response = client.prepareSearch(index).setTypes(type)
.setQuery(query).setSize(size).setFrom(offset).execute().actionGet();
SearchHits hits = response.getHits();
TrainingToCreate source = null;
ObjectMapper mapper = new ObjectMapper();
ArrayList<Training> trainings = new ArrayList<Training>();
for (SearchHit searchHit : hits) {
source = mapper.readValue(searchHit.getSourceAsString(), TrainingToCreate.class);
trainings.add(TrainingFactory.getTraining(searchHit.getId(), source));
}
return trainings;
}
我正在使用Elastic 1.7.0在Java 8上工作 有没有人重新解决问题的立场? 如果有人需要进一步的信息,请随时询问。
答案 0 :(得分:0)
Elasticsearch为near real time,这意味着您在索引文档的那一刻与可搜索文档之间存在一些延迟(默认为1秒)。您可以通过在运行查询之前简单刷新索引来解决此问题。
所以我会在你为你的样本文件编制索引之后这样做......
public void nameSearchTest() throws ElasticSearchUnavailableException, IOException{
String nameToSearch = "fuzzyText";
TrainingToCreate t = new TrainingToCreate();
t.setName(nameToSearch);
//Create two Trainings to find sth
String id1 = ElasticIndexer.index(t);
String id2 = ElasticIndexer.index(t);
// REFRESH YOUR INDICES (just after indexing)
client().admin().indices().prepareRefresh().execute().actionGet();
......或者只是在fuzzySearchDocument
private static ArrayList<Training> fuzzySearchDocument(Client client, String index, String type, String field, String value, int size, int offset) throws JsonParseException, JsonMappingException, IOException {
// REFRESH YOUR INDICES (just before searching)
client().admin().indices().prepareRefresh().execute().actionGet();
QueryBuilder query = fuzzyQuery(field, value);
...
如果您在示例文档上运行多个测试用例,我会选择第一个选项,否则任何选项都可以。