Thumbnail image

Spring Boot: Talking to Elasticsearch through JEST (REST api)

Thu, May 14, 2020 2-minute read

If you use Spring Boot with some database you are probably quite familiar with Spring Data project. This way we can use many database systems very standardized way. It includes Elasticsearch using Spring Data Elasticsearch project. Spring Data Elasticsearch talks to ES server using native port and there are situations when we may prefer using REST api here. And if we are in such situation, Jest library comes to rescue. Jest is a Java HTTP Rest client for ElasticSearch and its API is very nice and straightforward. To use it in our Spring Boot application (assuming we have Gradle based app) we simply include …


… into build.gradle file as an additional dependency. To use Jest we need JestClient as an entry point. From SpringBoot application perspective, we can define @Service bean that will create and hold connection object. Such service can be injected in our code later using @Autowire annotation. Implementation of minimal JestClient service can look like the following one.

public class JestClientService implements Serializable {
private static final long serialVersionUID = 1L;
JestClient client=null;

String host;

String port;

String indexName;
public JestClient getClient() {
if (this.client==null){
GsonFireBuilder fireBuilder = new GsonFireBuilder();

GsonBuilder builder = fireBuilder.createGsonBuilder();

final Gson gson = builder.setDateFormat(AbstractJestClient.ELASTIC_SEARCH_DATE_FORMAT).create();
System.out.println("Establishing JEST Connection to Elasticsearch over HTTP: "+"http://"":"+this.port);
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
this.client = factory.getObject();


return this.client;

Jest library contains decent documentation and internal tests come as nice source of examples that are very easy to learn from.