Image for post
Image for post

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

David Marko
Feb 2, 2017 · 2 min 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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store