In this article we’ll explore the basics of Spring Data Elasticsearch in a code-focused, practical manner.
We’ll show how to index, search, and query Elasticsearch in a Spring application using Spring Data – a Spring module for interaction with a popular open-source, Lucene-based search engine.
While Elasticsearch is schemaless, it can use mappings in order to tell the type of a field. When a document is indexed, its fields are processed according to their types. For example, a text field will be tokenized and filtered according to mapping rules. You could also create filters and tokenizers of your own.
2. Spring Data
Spring Data helps avoid boilerplate code. For example, if we define a repository interface that extends the ElasticsearchRepository interface provided by Spring Data Elasticsearch, CRUD operations for the corresponding document class will be made available by default.
Additionally, simply by declaring methods with names in a prescribed format, method implementations are generated for you – there is no need to write an implementation of the repository interface.
Notice that we added two custom methods. With the findByAuthorsNamemethod, the repository proxy will create an implementation based on the method name. The resolution algorithm will determine that it needs to access the authors property and then search the name property of each item.
The second method, findByAuthorsNameUsingCustomQuery, uses an Elasticsearch boolean query, defined using the @Query annotation, which requires strict matching between the author’s name and the provided nameargument.
2.3. Java Configuration
Let’s now explore the Spring configuration of our persistence layer here:
Notice that we’re using a standard Spring enable-style annotation – @EnableElasticsearchRepositories – to scan the provided package for Spring Data repositories.
We are also:
Starting the Elasticsearch node without HTTP transport support
Setting the location of the data files of each index allocated on the node
Starting from Elasticsearch 2.x, we need to set “path.home” with Elasticsearch home directory.
Finally – we’re also setting up an ElasticsearchOperations bean – elasticsearchTemplate – as our client to work against the Elasticsearch server.
Let’s now define our first entity – a document called Article with a String id:
@Document(indexName = "blog", type = "article")
@Field(type = FieldType.Nested)
// standard getters and setters
Note that in the @Document annotation, we indicate that instances of this class should be stored in Elasticsearch in an index called “blog“, and with a document type of “article“. Documents with many different types can be stored in the same index.
Also notice that the authors field is marked as FieldType.Nested. This allows us to define the Author class separately, but have the individual instances of author embedded in an Article document when it is indexed in Elasticsearch.
4. Indexing Documents
Spring Data Elasticsearch generally auto-creates indexes based on the entities in the project.
However, you can also create an index programmatically, via the client template:
After the index is available, we can add a document to the index.
Let’s have a quick look at an example – indexing an article with two authors:
Article article = newArticle("Spring Data Elasticsearch");