我为弹性搜索CRUD操作创建了一个Java服务,在下面给出了我的业务逻辑,我在做所有服务器端验证,因为我必须检查弹性搜索部署当前是否可用,或者不使用我的Elastic的RestHighLevelClient在将数据插入索引之前进行搜索。我在下面给出了我的代码。
public String createProfileDocument(EmployeeInformation document, String IndexName) throws Exception {
String Id = document.getId();
if (Strings.isNullOrEmpty(Id)) {
return "ID is null or empty";
}
else if(Strings.isNullOrEmpty(IndexName)) {
return "Index name is null or empty";
}
else if(isTextContainUpperCase(IndexName)){
return "Index name should be in lowercase";
}
else if(logic to check the deployment)
{
return "Elasticsearch deployment is not reachable";
}
IndexRequest indexRequest = new IndexRequest(IndexName);
indexRequest.id(document.getId());
indexRequest.source(convertProfileDocumentToMap(document), XContentType.JSON);
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
return indexResponse.getResult().name();
}
有人可以帮我实现以上代码吗?预先感谢
答案 0 :(得分:1)
答案 1 :(得分:1)
由于您仅询问有关检查状态的信息,我会假设您有权访问RestHighLevelClient
实例
import org.elasticsearch.client.RequestOptions;
...
public boolean checkTempConnection(RestHighLevelClient client) {
try {
return client.ping(RequestOptions.DEFAULT);
} catch (Exception e) {
log.warn("Can't get status : {}", e.getMessage());
return false; // Not available
}
}
祝你好运!