Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 30, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 30, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 30, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 30, 2025

      Does Elden Ring Nightreign have crossplay or cross-platform play?

      May 30, 2025

      Cyberpunk 2077 sequel enters pre-production as Phantom Liberty crosses 10 million copies sold

      May 30, 2025

      EA has canceled yet another game, shuttered its developer, and started more layoffs

      May 30, 2025

      The Witcher 3: Wild Hunt reaches 60 million copies sold as work continues on The Witcher 4

      May 30, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      How Remix is shaking things up

      May 30, 2025
      Recent

      How Remix is shaking things up

      May 30, 2025

      Perficient at Kscope25: Let’s Meet in Texas!

      May 30, 2025

      Salesforce + Informatica: What It Means for Data Cloud and Our Customers

      May 30, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Does Elden Ring Nightreign have crossplay or cross-platform play?

      May 30, 2025
      Recent

      Does Elden Ring Nightreign have crossplay or cross-platform play?

      May 30, 2025

      Cyberpunk 2077 sequel enters pre-production as Phantom Liberty crosses 10 million copies sold

      May 30, 2025

      EA has canceled yet another game, shuttered its developer, and started more layoffs

      May 30, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Elasticsearch Integration with Spring Boot: A Developer’s Guide

    Elasticsearch Integration with Spring Boot: A Developer’s Guide

    April 8, 2024

    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

    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.
    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.
    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.
    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.
    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.
    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

    Application search: For applications that access, retrieve, and report data mainly via a search platform.
    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.
    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.
    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.
    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.
    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:

    <dependency>

               <groupId>co.elastic.clients</groupId>

              <artifactId>elasticsearch-java</artifactId>

    </dependency>

    <dependency>

             <groupId>org.projectlombok</groupId>

            <artifactId>lombok</artifactId>

    </dependency>

    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.

    @Configuration
    public class ElasticsearchConfig {

        private static String HTTP_CA_FINGERPRINT = “<<HTTP_CA_FINGERPRINT>>”;

        @Bean
        public RestClient restClient() {
            SSLContext sslContext = TransportUtils.sslContextFromCaFingerprint(HTTP_CA_FINGERPRINT);
            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(“<<USERNAME>>”, “<<PASSWORD>>”));

            return RestClient.builder(new HttpHost(“<<HOSTNAME>>”, 9200, “https”))
                    .setHttpClientConfigCallback(
                            hc -> hc
                                    .setSSLContext(sslContext)
                                    .setDefaultCredentialsProvider(credentialsProvider)
                    ).build();
        }

        @Bean
        public ElasticsearchTransport elasticsearchTransport() {
            return new RestClientTransport(restClient(), new JacksonJsonpMapper());
        }

        @Bean
        public ElasticsearchClient elasticsearchClient() {
            return new ElasticsearchClient(elasticsearchTransport());
        }
    }

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

    The default username is elastic.
    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:

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Product {
        private String name;
        private String description;
        private Double price;
        private List<String> category;
    }

    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.

    @Service
    @RequiredArgsConstructor
    public class ElasticsearchService {

      private final ElasticsearchClient elasticsearchClient;

        public String createIndex(String indexName) throws IOException {
            return elasticsearchClient.indices()
                    .create(c -> c.index(indexName)).index();
        }

        public Result ingestDocument(String indexName, String id, Product product) throws IOException {
            IndexRequest<Object> indexRequest = IndexRequest.of(
                    request -> request
                            .index(indexName)
                            .id(id)
                            .document(product)
            );
            return elasticsearchClient.index(indexRequest).result();
        }

        public Object getProductById(String indexName, String id) throws IOException {
            GetResponse<Product> response = elasticsearchClient.get(
                    g -> g.index(indexName).id(id),
                    Product.class
            );
            if (response.found()) {
                return response.source();
            } else {
                return “Product Not Found”;
            }
        }

        public List<Product> searchDocuments(String indexName, String fieldName, String text) throws IOException {
            List<Product> responseList = new ArrayList<>();
            SearchResponse<Product> searchResponse = elasticsearchClient.search(s -> s
                            .index(indexName)
                            .query(q -> q
                                    .term(t -> t
                                            .field(fieldName)
                                            .value(v -> v.stringValue(text))
                                    )),
                    Product.class);
            for (Hit<Product> hit : searchResponse.hits().hits()) {
                responseList.add(hit.source());
            }
            return responseList;
        }

        public List<Product> getAllDocuments(String indexName) throws IOException {
            List<Product> responseList = new ArrayList<>();
            SearchResponse<Product> searchResponse = elasticsearchClient.search(s -> s
                    .index(indexName).query(q -> q.matchAll(MatchAllQuery.of(m -> m.queryName(“match_all”)))), Product.class);
            for (Hit<Product> hit : searchResponse.hits().hits()) {
                responseList.add(hit.source());
            }
            return responseList;
        }
    }

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

    @RestController
    @RequestMapping(“/app”)
    @RequiredArgsConstructor
    public class SampleController {

        private final ElasticsearchService elasticsearchService;

        @PostMapping(“/index/create/{indexName}”)
        public String createIndex(@PathVariable String indexName) throws IOException {
            return elasticsearchService.createIndex(indexName);
        }

        @PostMapping(“/document/ingest/{indexName}/{id}”)
        public Result ingestDocument(@PathVariable String indexName, @PathVariable String id, @RequestBody Product product) throws IOException {
            return elasticsearchService.ingestDocument(indexName, id, product);
        }

        @GetMapping(“/document/get-by-id/{indexName}/{id}”)
        public Object getProductById(@PathVariable String indexName, @PathVariable String id) throws IOException {
            return elasticsearchService.getProductById(indexName, id);
        }

        @GetMapping(“/documents/find/{indexName}/{fieldName}/{text}”)
        public List<Product> searchByName(@PathVariable String indexName, @PathVariable String fieldName, @PathVariable String text) throws IOException {
            return elasticsearchService.searchDocuments(indexName, fieldName, text);
        }

        @GetMapping(“/documents/{indexName}”)
        public List<Product> getAllDocuments(@PathVariable String indexName) throws IOException {
            return elasticsearchService.getAllDocuments(indexName);
        }
    }

    Start elasticsearch service before running the project:

    Run the following command:

    ./bin/elasticsearch

    GitHub Reference URL:

    https://github.com/jay-rudani/elasticsearch-spring-boot

    The post Elasticsearch Integration with Spring Boot: A Developer’s Guide appeared first on Inexture.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleTop AI Tools to Build Your Large Language Models (LLMs) Apps
    Next Article Cybercriminals Targeting Latin America with Sophisticated Phishing Scheme

    Related Posts

    Security

    China-Linked Hackers Exploit SAP and SQL Server Flaws in Attacks Across Asia and Brazil

    May 31, 2025
    Security

    New Apache InLong Vulnerability (CVE-2025-27522) Exposes Systems to Remote Code Execution Risks

    May 31, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Microsoft reduces Nvidia’ GB200 orders as result of production delays

    Development

    Human Metapneumovirus (HMPV) – Testing Management System Using PHP and MySQL

    Development

    Obsidian Entertainment CEO says the developer has grown significantly under Xbox Game Studios

    News & Updates

    We tried Windows 11’s new Start menu design, and it’s now really good

    Operating Systems
    Hostinger

    Highlights

    Artificial Intelligence

    Node.js Speech-to-Text with Punctuation, Casing, and Formatting

    May 29, 2024

    Automatically-generated transcripts from audio and video files are a lot more useful and readable when…

    Teams “Sorry We Couldn’t Connect You” Error – Fix-it Guide

    January 8, 2025

    Three ways Figma explored horizontal scrolling

    April 17, 2025

    So is FileHippo now a malware site? [closed]

    November 25, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.