我在使用本机Elasticsearch Java API时遇到问题。我想创建一种通过其名称属性搜索对象的方法。到目前为止,到目前为止非常容易,之后,我想为此方法创建一个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);
}
有时,此测试可以正常工作,而其他时候,即使我创建了一些文档以确保可以找到某些内容,搜索结果也不包含任何内容。但是,如果我在Elasticsearch数据库中查找文档,则该文档存在,因此我想我的想法不正确,或者搜索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,有人在确认问题的位置吗?如果有人需要进一步的信息,请随时询问。
Elasticsearch接近实时,这意味着在您为文档建立索引与可搜索之间存在一定的延迟(默认为1s)。您可以通过在运行查询之前简单地刷新索引来克服此问题。
所以我要么在您索引示例文档后就执行此操作...
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);
...
如果您在样本文档上运行多个测试用例,则我会选择第一个选项,否则任何选项都可以。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句