Elastic Search Using Spring Boot

Search the pages with the words rather than the word in the Pages

Ever have imaged how the things would change when we will get to look for the Pages with the words rather than the words in the Pages of a Dictionary…Well, not in this Scenario, but when we will take Spring Data into our consideration, this will surely make life easier.

We usually implement the functionality like Pagination and Sorting independently in our Backend Application, but Spring Boot’s Elastic Search brings all of them together and serves us with the Response we always look for in our Query Requests.

Consider we have data of n number of Employee in an organization, which are mapped together into Entities in the Database. For every Use Case where we need to get any specific data about one Employee, we usually do a JPA Interface request from our Repository and once we get that response, we start using the basic libraries of Pagination and Sorting at our backend.

But with Elastic Search, this all comes inside one wrapped response JSON and it provides all the convenient information needed to make life easier.

So, how does this works. Don’t you think Elastic Search has a Pre Defined Framework of its own and how does it basically arranges things.

All the entities are considered as the Fields in this context and they are wrapped together inside a Document (based on the similar attributes ). These Documents are further contained inside a Shard and these ‘k’ number of shards are wrapped together inside an Index. Shard is nothing but a container of divided Index.

So how does the searching works ?

Any search request goes with a keyword. The framework looks for all the Shards and searches the one which contains that Key. It returns all the related documents containing the fields as the response. So, this makes things faster in a way that it does not have to go to every Entity or leverage a confusing Sub — Sub — Sub Query to get the data.

In short,

Documents : Combination of one or more fields.

Index : Combination of one or more documents.

Shards : Combination of divided Indexes

Whenever a search request comes, the request goes to every Shard and only that Shard is selected whichever has that Search Keyword, and then the further Investigation with the Documents inside that Shard goes on.

To make a basic setup of this, all you need to so is to add a dependency of Elastic Search in the POM file and defined all the entities with the “@Document” Annotation. Further we have to replace the JPA or Cassandra or whatever DB Interface with the Elastic Search Interface in the Repository and that all.. a basic Elastic Search Setup is done.

I will share an Installation and POC like Project soon here for Elastic Search. But before starting this foundation and basic is all we need.

Happy Dev..

hariom sinha :)

Frontend Developer from India. Focus on Learning and writing helpful contents for us fellow Developers.