The Elasticsearch search engine is integrated with the Spring Data Elasticsearch project. Spring Data's main functional area Elasticsearch is a POJO-centric approach for dealing with Elasticsearch Documents and developing a Repository-style data access layer quickly and efficiently.
Let’s begin this blog with an introduction to Spring Data ElasticSearch, and then will explore the concepts like Spring Data, Mappings, Indexing Documents, Querying, updating and deleting.
Take your career to next level in Elasticsearch with HKR. Enroll now to get Elasticsearch certification course training
In this blog, we'll look at the fundamentals of Spring Data Elasticsearch in a practical and code-focused manner.
We'll demonstrate how to use Spring Data Elasticsearch to index, search, and query Elasticsearch within a Spring application. Elasticsearch is a Spring module that implements Spring Data, allowing you to connect with the popular open-source Lucene-based search engine.
While Elasticsearch can run without a schema, it's customary to construct one and establish mappings that identify the type of data to expect in specific fields.When a document is indexed, the fields are classified and processed accordingly. A text field, for instance, will be tokenized and filtered using mapping rules. We could also make our own filters and tokenizers.
We'll use a docker image for our Elasticsearch instance for simplicity's sake, but any Elasticsearch instance listening on port 9200 would do.
First, we'll begin our Elasticsearch instance:
Spring Data makes it possible to avoid writing boilerplate code. CRUD operations for the corresponding document class will be made available by default if we define a repository interface that extends the ElasticsearchRepository interface provided by Spring Data Elasticsearch.
Furthermore, method implementations are generated for us simply by declaring methods with names in a predefined format – there is no need to write a repository interface implementation.
The Baeldung Spring Data guides cover everything you need to know to get started with the topic.
For the search engine, Spring Data Elasticsearch offers a Java API. To use it, we must add a new dependency to the pom.xml file:
We extend one of the available repository interfaces to define new repositories by substituting the generic types with our own document and primary key types.
ElasticsearchRepository is extended from
PagingAndSortingRepository, which is vital to remember.
This enables pagination and sorting to be built-in.
We'll use the paging feature in our custom search methods in this example:
The repository proxy will construct an implementation based on the method name when using the findByAuthorsName method. The resolution algorithm would determine that it requires access to the authors property, followed by a search of each item's name property.
The second method, findByAuthorsNameUsingCustomQuery, makes use of a custom Elasticsearch boolean query written with the @Query annotation, which requires a rigorous match between the author's name and the name argument provided.
We have the perfect professional ElasticSearch Tutorial for you. Enroll now!
We must define how we connect to the Elasticsearch instance while configuring Elasticsearch in our Java application. We use a RestHighLevelClient provided by the Elasticsearch requirement for this:
A typical Spring-enabled style annotation is being used. @EnableElasticsearchRepositories will enable Spring Data Elasticsearch to search for Spring Data repositories in the specified package.
We utilise a basic RestHighLevelClient to interface with our Elasticsearch server. While Elasticsearch has a variety of clients, using the RestHighLevelClient is an excellent method to ensure that your communication with the server is future-proof.
Finally, we created an ElasticsearchOperations bean to do server operations. In this scenario, an ElasticsearchRestTemplate is instantiated.
Mappings are used to define a document's schema. We can safeguard our documents from undesirable results like mapping to a type we don't want by specifying a schema for them.
The entity we're working with is a simple document called Article, with an id of String type. We further state that such documents must be saved in the article type's blog index.
There are various types of indexes. That feature can be used to create hierarchies.
The authors field is marked as FieldType.Nested. This enables us to declare the Author class separately, yet have individual author instances embedded in an Article document when it is indexed in Elasticsearch.
Top 30+ frequently asked Elasticsearch interview questions & answers for freshers & experienced professionals
In most cases, Spring Data Elasticsearch produces indexes depending on the entities in the project. However, using the client template, we can generate an index programmatically:
After that, we can start adding documents to the index:
We build methods that define the query we wish to run when we use the method name-based query. Spring Data will analyse the method signature and make queries based on it during the setup:
We get the first page of results (page numbering is zero-based) by executing findByAuthorsName with a PageRequest object, with that page comprising at most 10 articles.The page object also contains information on the total number of hits for the query, as well as pagination information.
Custom queries for Spring Data Elasticsearch repositories can be defined in a few different ways. The @Query annotation is one approach.
Another alternative is to write our custom query using the query builder.
We could simply create a NativeSearchQueryBuilder with a Filter on the title if we needed to search for articles with the word "data" in the title:
First, we must retrieve the document in order to change it:
Then, by altering the content of the object using its assessors, we may make changes to the document:
When it comes to deleting, you have many options. Using the delete method, we can obtain the document and delete it:
When the id is known, we can also delete it by id:
It's also feasible to write custom deleteBy queries and use Elasticsearch's bulk delete feature:
We looked at how to connect to and use Spring Data Elasticsearch in this post. We have explained how to query, update, and delete documents..We also have explained about how to develop custom queries that don't fit with what Spring Data Elastic has to give.
Batch starts on 30th Sep 2021, Weekday batch
Batch starts on 4th Oct 2021, Weekday batch
Batch starts on 8th Oct 2021, Fast Track batch