i tried to check if an index exists or note before i do a search, i found a solution by performing requests, but it's not working, it results on a NullPointerException
@Autowired
private RestHighLevelClient restHighLevelClient;
private List<E> convertToObjectList(SearchRequest searchRequest) {
SearchResponse searchResponse = null;
try {
if (searchRequest != null) {
RestClient restClient = null;
Response response = restClient.performRequest("HEAD", "/" + index()); //java.lang.NullPointerException: null
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 404) {
searchResponse = restHighLevelClient.search(searchRequest);
}
}
} catch (IOException e) {
logger.error("Error" + e);
}
....
}
@Configuration
public class ElasticsearchConfiguration extends AbstractFactoryBean<RestHighLevelClient> {
private RestHighLevelClient restHighLevelClient;
@Override
public RestHighLevelClient createInstance() {
return buildClient();
}
@Override
public Class<RestHighLevelClient> getObjectType() {
return RestHighLevelClient.class;
}
@Override
public void destroy() {
try {
if (restClient() != null) {
restClient().close();
}
} catch (final Exception e) {
logger.error("Error closing ElasticSearch client: ", e);
}
}
@Override
public boolean isSingleton() {
return false;
}
public RestClient restClient() {
return RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
}
private RestHighLevelClient buildClient() {
try {
restHighLevelClient = new RestHighLevelClient(restClient());
} catch (Exception e) {
logger.error("Error" + e);
}
return restHighLevelClient;
}
}