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:
The 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 @Document
annotation
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 Email
has a list of recipients
, a list of senders
a subject
, a sentDate
, a receivedDate
and a list of texts
(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 subject
,
which is a plain string. For complex datatypes like the lists of EmailAddress
es, Elasticsearch
has to know that the data should be stored as an internal document. This is done by using the @Field
annotation and settting the type
to FieldType.Object
.
An email looks like this to Elasticsearch:
The conversion from and to JSON is done by the Jackson JSON library. The @JsonFormat
annotations
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 EmailRepositoryCustom
.
These two methods are implemented in EmailRepositoryCustom
.
The @Repository
annotation tells Spring Boot that this is the implementation of a repository.
The @Autowired
annotation causes Spring Boot to instantiate the field with a "bean" of type
ElasticsearchTemplate
.
The ElasticsearchTemplate
is used in the method getWeeklyHistogram
to execute a
query build with the NativeSearchQueryBuilder
.