Spring Data ElasticSearch

Elasticsearch Spring Data is part of the larger Spring Data project, which attempts to provide a familiar and consistent Spring-based programming model for new datastores while keeping store-specific features and capabilities.

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

1. Introduction to Spring Data ElasticSearch

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:

2. Spring Data

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.

2.1. Maven Dependency

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:

Maven Dependency

2.2. Defining Repository Interfaces

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:

paging feature

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!

ElasticSearch Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

2.3. Java Configuration

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:

Java Configuration

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.

3. Mappings

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.

Mappings

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

Subscribe to our youtube channel to get new updates..!

4. Indexing Documents

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:

Indexing Documents

After that, we can start adding documents to the index:

documents to the index

5. Querying

5.1. Name-Based Query Method 

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:

Name-Based Query Method

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.

5.2. A Custom Query

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:

 A Custom Query

6. Updating and Deleting

First, we must retrieve the document in order to change it:

Updating and Deleting

Then, by altering the content of the object using its assessors, we may make changes to the document:

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:

delete method

When the id is known, we can also delete it by id:

delete it

It's also feasible to write custom deleteBy queries and use Elasticsearch's bulk delete feature:

Elasticsearch's bulk delete feature

ElasticSearch Training

Weekday / Weekend Batches

Conclusion

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.

Other Blogs:

Find our upcoming ElasticSearch Training Online Classes

  • 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

Global Promotional Image
 

Categories

Request for more information

Gayathri
Gayathri
Research Analyst
As a senior Technical Content Writer for HKR Trainings, Gayathri has a good comprehension of the present technical innovations, which incorporates perspectives like Business Intelligence and Analytics. She conveys advanced technical ideas precisely and vividly, as conceivable to the target group, guaranteeing that the content is available to clients. She writes qualitative content in the field of Data Warehousing & ETL, Big Data Analytics, and ERP Tools. Connect me on LinkedIn.