Elasticsearch is an open, free and distributed search engine and an analytical tool built on top of Apache Lucene.
It first stores documents in an index. It uses powerful queries to search the index and runs its analytical functions on the data. Elasticsearch uses REST APIs to perform operations.
To use Elasticsearch in spring-boot, an interface is implemented as an alternative to its REST APIs. In this blog, we'll learn how to use Elasticsearch in Spring-boot.
Let's take a look using a step by step guide with an example.
1. Add Maven dependency
2. Connection with Elasticsearch Instance
To connect with Elasticsearch server, Spring data Elasticsearch uses JHLC(Java High Level REST Client)
3. Create a document
Here, Studentindex is the index name for Elasticsearch, provided using @Document annotation.
1. ElasticSearchRestTemplate: It is an implementation of the ElasticSearchOperations interface. Here, you can write your own queries.
2. ElasticSearchRepository Interface: ElasticSearch queries can be generated using repository's methods. It is easier and fast but problematic for writing complex queries.
1. ElasticSearchRepository for indexing and searching
We need to create a repository interface by extending ElasticSearchRepository interface.
Now, StudentRepository inherits ElasticsearchRepository's methods. So, Whenever a save() or saveAll() methods invoke, data gets also inserted in index.
We can perform our searching using methods we defined in our StudentRepository.
2. ElasticSearchRestTemplate for indexing and searching
ElasticsearchRestTemplate implements the ElasticsearchOperations interface, which does the heavy lifting for low-level search and cluster actions.
This interface has the method index() for adding a single document and bulkIndex() for adding multiple documents to the index. Here i'm using index() method in this example.
now, to search documents, ElasticsearchRestTemplate has a search() method