Issue
i have created a Java service for elastic search CRUD operation i have given my business logic below there i am doing all my server side validation in that i have to check the elastic search deployment is currently available or not using my RestHighLevelClient of elastic search before inserting data into my index. i have given my code below.
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();
}
Could some one help me to achieve this in above code? Thanks in advance
Solution
Since you asked only about checking the status i willl suppose that you have access to the RestHighLevelClient
instance
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
}
}
Good Luck !
Answered By - Rayen Rejeb
Answer Checked By - Clifford M. (JavaFixing Volunteer)