Using Elasticsearch with Spring Boot - Technical background
May 27, 2015
This is the third part in a series of four. It explains the technical background.
With Spring Boot it is easy to glue together different components into a complex application. The following is the list of dependencies for this project used by the build tool gradle:
spring-boot-starter-projects make it possible, to glue the components together into
Springs IoC container.
Build and running this projects is a one liner on the command line:
$ gradle bootRun
Importing the emails into Elastiksearch
A class that should be persisted in Elasticsearch has to be marked with the
from the Spring Data Elasticsearch project.
The index and the type that Elasticsearch should use is specified as parameters. In this case an index named
"email" and a type named "email" is used.
In this example an
recipients, a list of
receivedDate and a list of
(email is usually send as multipart message,
so we have to use a list of texts). We ignore attached documents and images:
For simple datatypes nothing has to be specified, see for example the
which is a plain string. For complex datatypes like the lists of
has to know that the data should be stored as an internal document. This is done by using the
annotation and settting the
An email looks like this to Elasticsearch:
The conversion from and to JSON is done by the Jackson JSON library. The
in the class definition specify the date format.
Querying the emails
In the application we want to count the number of emails that contain a specific text in the subject or in the body. This is an aggregation. In SQL you would write something like:
In Spring Boot and Spring Data the class that communicates with the database is called a repository. Spring Data has powerful mechanism to automatically create a repository with many methods to query the repository. If you just want the vanilla functionality then it is sufficient to create an interface that extends a repository class. In this app we need
The user defined methods are provided in the interface
These two methods are implemented in
@Repository annotation tells Spring Boot that this is the implementation of a repository.
@Autowired annotation causes Spring Boot to instantiate the field with a "bean" of type
ElasticsearchTemplate is used in the method
getWeeklyHistogram to execute a
query build with the
Remark: This post was adapted to the new blog format in November 2016.