Get in touch

Awesome Image Awesome Image

ElasticSearch Engine April 8, 2024

Elasticsearch Integration with Spring Boot: A Developer’s Guide

Writen by Mahipalsinh Rana


Elasticsearch is a search tool that can also analyze data as well, And it is free to use by everyone. Developers appreciated it as it impressively handles many searches at once, therefore assisting in increasing the speed of their applications and making them work more efficiently.

Now, when you mix Elasticsearch with Spring Boot, which is another tool that helps build big, serious apps easily, things get even better. Your designed app can quickly scroll through a tremendous amount of data, accomplish a better understanding of data, and flourish without any nerve-wracking moments. This guide is for all of those who would like to use the capabilities of Elasticsearch to make their Spring Boot app more intelligent. We will do it step by step from the very beginning to the very end to show you how you can assemble it to get it going.

You can either enhance your app or come up with a completely new one in which case we will provide you with all the resources you need to ensure the best results and get your app to the top of Elasticsearch thus making sure that your app excels.

What is Elasticsearch?

Elasticsearch is a real-time distributed and open-source full-text search and analytics engine. It is used in Single Page Application (SPA) projects. Elasticsearch is an open-source developed in Java and used by many big organizations around the world.

Recommendation: Explore Elasticsearch and Why It’s Worth Using

Benefits of Using Elasticsearch with Spring Boot

#1. Imagine a library where you have every book instantly, as they can be sorted and found in any type of configuration. The application function becomes a lot better when you use the Elasticsearch tool. The search result of lots of information, even if you have tons of data, is fast with it.

#2. As your app gains more users as it gets more popular, the need to expand its storage capacity becomes more apparent because large data will cause your app to slow down. Elasticsearch together with Spring Boot in case combination is a good selection. They help you to increase the size of your app smoothly with no difficulties handling all the data.

#3. Elasticsearch doesn’t just seek to locate information; it magnificently handles the tasks of the analysis. Understanding your data then becomes easier. You can spot trends, for example, and find out exactly what your users prefer most. To make the right choice, that is to make a smart decision for your app, this kind of data is crucial.

#4. Application web development in Spring Boot is ideal since it means that developers set up and bootstrap things quickly and effortlessly. Employing it in conjunction with Elasticsearch enables you to start building the saucy sections of your app, without hogging your time with the nitty-gritty of setting it up.

#5. Searches look up quickly and provide matching results that stick your app to the user’s mind. People make decisions instantly when you provide what they’re exactly looking for. This momentary action will make them stick to your app more.

#6. Elasticsearch gets fresh and relevant data, and updates search results as things change. This is the perfect fit for apps that consume the most recent or most up-to-date information, especially for apps like news apps or social media.

Read | Spring Boot with Microsoft Azure Integration

Key Components

  1. Documents: These are the basic units of information stored in Elasticsearch. Documents are indexed in JSON format and can contain various types of data like numbers, strings, and dates. Each document has a unique ID and a specific data type.
  2. Indices: Collections of documents with similar characteristics are grouped into indices. Think of indices as databases in relational databases where related documents are stored together. Each index has a unique name and can be queried against.
  3. Inverted Index: An index in Elasticsearch is also referred to as an inverted index. This data structure is fundamental to search engines and maps content like words or numbers to their locations in documents.
  4. Shards: Shards are individual Lucene indexes that make up Elasticsearch’s scalability. They allow for distributing data across nodes to enhance performance and prevent issues like index size limitations.
  5. Nodes: Nodes are essential components of an Elasticsearch cluster. Different types of nodes exist, such as data nodes for storing data, master nodes for cluster management, client nodes for handling requests, and more. Nodes are assigned specific roles within the cluster.
  6. Cluster: An Elasticsearch cluster comprises one or more nodes and has a unique identifier. Clusters are formed by nodes joining together and are crucial for the distributed nature of Elasticsearch.

Primary Use Cases

  1. Application search: For applications that access, retrieve, and report data mainly via a search platform.
  2. Web Search: Elasticsearch serves as a powerful tool for accurate and effective searches on websites that have massive content repositories. It comes as no surprise that Elasticsearch is becoming more and more popular in the site search industry.
  3. Business exploration: Enterprise-wide search is made possible by Elasticsearch and can contain every type of search you can imagine, including document, blog, people, and E-commerce product searches. It has gradually supplanted the search functions on most of the well-known websites that we visit regularly. From an enterprise-specific standpoint, corporate intranets make extensive use of Elasticsearch.
  4. Logging and log analytics: Elasticsearch is a prominent tool for scalable, near-real-time ingesting and analyzing log data. To motivate activities, it additionally provides substantial operational information on log metrics.
  5. Monitoring of containers and infrastructure metrics: A lot of companies utilize the ELK stack for analyzing various metrics. This could entail collecting information on several performance metrics that change depending on the use case.
  6. Security analytics: Elasticsearch is also an effective analytics application for security analysis. The ELK stack may be utilized to analyze access logs and other logs relevant to system security, offering you a broader understanding of what’s going on in real time across all your systems.

Setting Up Elasticsearch with Spring Boot

Step 1: Include Required Dependencies

To start, you need to include the necessary dependencies in your Spring Boot project. Add the following dependencies to your pom.xml file:

Step 2: Configure Elasticsearch Connection with Java Configuration Class

  • Establishes a connection to the Elasticsearch cluster using the RestClient builder.
  • Configures security (HTTPS) with hostname, port, and credentials (username and password).
  • Creates RestClient, ElasticsearchTransport, and finally the ElasticsearchClient bean for interaction.

Note: You will get http_ca_fingerprint, username, and password when you start the Elasticsearch service for the first time.

  1. The default username is elastic.
  2. If somehow you forgot the password, then you can reset the password by using the following command:

bin/elasticsearch-reset-password -u elastic –url https://localhost:9200

  • You can get http_ca_fingerprint using the following command:

openssl x509 -in config/certs/http_ca.crt -noout -fingerprint | sed ‘s/://g’

Step 3: Create One Service Class to Create Index, Ingest and Search Document Using Elasticsearch Client

Simple Product class:

Service class: inject ElasticsearchClient dependency

Provides methods for various operations:

  • createIndex: Creates an Elasticsearch index with the provided name.
  • ingestDocument: Inserts a document into a specific index with a unique ID.
  • getProductById: Retrieves a document from an index by its ID.
  • searchDocuments: Searches for documents in an index based on a field and search term.

getAllDocuments: Retrieves all documents from a specific index.

Step 4: Create One Simple Rest Controller and Inject ElasticsearchService Dependency

Start elasticsearch service before running the project:

Run the following command:


GitHub Reference URL:


Bringing Software Development Expertise to Every
Corner of the World

United States



United Kingdom




New Zealand










South Africa