Using Spring Boot with Elasticsearch for Fast Search Capabilities

Elasticsearch is a highly scalable open-source full-text search and analytics engine that allows you to perform fast searches across large volumes of data. When integrated with Spring Boot, it provides a powerful combination for building applications that require efficient searching and data analysis. In this post, we’ll explore how to integrate Elasticsearch into your Spring Boot application and implement a simple search feature.

What is Elasticsearch?

Elasticsearch is a distributed search and analytics engine that is built on top of Apache Lucene. It is used for searching, analyzing, and visualizing large amounts of data in real-time. Key features of Elasticsearch include:

  • Full-Text Search: Elasticsearch provides powerful full-text search capabilities, allowing for complex queries.
  • Real-Time Indexing: Data can be indexed and made searchable within seconds.
  • Scalability: Designed to handle large amounts of data and can scale horizontally.

Setting Up Elasticsearch with Spring Boot

To integrate Elasticsearch into your Spring Boot application, follow these steps:

1. Create a Spring Boot Project

Use Spring Initializr to generate a new Spring Boot application with the following dependencies:

  • Spring Web
  • Spring Data Elasticsearch

2. Adding Dependencies

Add the following dependency for Spring Data Elasticsearch in your pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

3. Configuring Elasticsearch

Specify the configuration details for Elasticsearch in your application.properties file:

spring.elasticsearch.rest.uris=http://localhost:9200

4. Creating an Elasticsearch Entity

Define an entity to represent the data that you will index. For example, let’s create a simple Product class:

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "products")
public class Product {
    @Id
    private String id;
    private String name;
    private String description;

    // Getters and Setters
}

5. Creating a Repository Interface

Next, create a repository interface that extends ElasticsearchRepository, allowing you to perform CRUD operations:

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepository extends ElasticsearchRepository<Product, String> {
    List<Product> findByName(String name);
}

Building the Service Layer

Create a service class to handle the business logic related to products:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ProductService {

    @Autowired
    private ProductRepository productRepository;

    public void saveProduct(Product product) {
        productRepository.save(product);
    }

    public List<Product> searchByName(String name) {
        return productRepository.findByName(name);
    }
}

Creating REST Endpoints

Finally, create a REST controller to expose the API for searching products:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping
    public void createProduct(@RequestBody Product product) {
        productService.saveProduct(product);
    }

    @GetMapping("/search")
    public List<Product> searchProducts(@RequestParam String name) {
        return productService.searchByName(name);
    }
}

Running Your Application

Run your Spring Boot application, and start Elasticsearch on your local machine. You can index new products using:

curl -X POST http://localhost:8080/api/products -H "Content-Type: application/json" -d '{"id":"1","name":"Sample Product","description":"This is a sample product."}'

Then, search for products by name using:

curl -X GET "http://localhost:8080/api/products/search?name=Sample Product"

Conclusion

Integrating Elasticsearch with Spring Boot allows you to build powerful search capabilities for your application that can handle a large amount of data efficiently. By following the steps outlined in this post, you can create a working example of a searchable product store with minimal effort.

To delve deeper into advanced topics like indexing strategies and performance tuning for Elasticsearch and Spring Boot, check out the extensive resources available at ITER Academy.

To learn more about ITER Academy, visit our website.

Scroll to Top