Using Spring Boot with Apache Solr for Search Functionality

As applications accumulate data, the need for efficient search functionality becomes critical. Apache Solr is a powerful open-source search platform built on Apache Lucene, designed for indexing and searching large volumes of text-centric data. Integrating Solr with Spring Boot allows developers to create applications that include robust search functionality easily. In this post, we will guide you through the steps to set up and use Apache Solr with Spring Boot.

What is Apache Solr?

Apache Solr is an enterprise search platform that provides distributed indexing and search capabilities. Its main features include:

  • Full-text Search: Supports complex queries and search functionalities, including faceting, hit highlighting, and spelling correction.
  • Scalability: Designed for high scalability and can handle large amounts of data.
  • REST API: Exposes a RESTful API for interacting with the search indexes.

Setting Up Spring Boot with Apache Solr

Let’s walk through the integration process:

1. Install Apache Solr

Download Apache Solr from the official website (solr.apache.org) and follow the installation instructions. Start Solr using:

bin/solr start

Open the Solr Admin UI by navigating to http://localhost:8983/solr.

2. Create a Spring Boot Project

Create a new Spring Boot project using Spring Initializr and include the following dependencies:

  • Spring Web
  • Spring Data Solr

3. Adding Dependencies

Add Spring Data Solr to your pom.xml:

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

Configuring Solr in Spring Boot

Add the Solr configuration in your application.properties file:

spring.data.solr.host=http://localhost:8983/solr

Creating an Entity Class

Define a domain model that will represent the data stored in Solr. For example, let’s create a Product class:

import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.SolrDocument;

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

    // Getters and Setters
}

Creating a Repository Interface

Now create a repository interface for your Product entity:

import org.springframework.data.solr.repository.SolrCrudRepository;
import org.springframework.stereotype.Repository;

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

Creating a Service Layer

Create a service class to handle business logic for managing 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> findProductsByName(String name) {
        return productRepository.findByName(name);
    }
}

Building the REST Controller

Create a REST controller that exposes endpoints for product management:

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.findProductsByName(name);
    }
}

Running Your Application

Run your Spring Boot application and ensure your Solr instance is running. You can now test the API by adding products and searching:

curl -X POST -H "Content-Type: application/json" -d '{"id": "1", "name": "Product A", "description": "A great product!"}' http://localhost:8080/api/products
curl "http://localhost:8080/api/products/search?name=Product A"

Conclusion

Integrating Spring Boot with Apache Solr enables the development of high-performance search capabilities in your applications. The combination of Spring Data and Solr simplifies the management and querying of search data, leading to a more efficient data handling model.

For deeper insight into working with Spring Boot and Apache Solr as well as best practices for building searchable applications, explore the rich offerings at ITER Academy to enhance your development skills.

To learn more about ITER Academy, visit our website.

Scroll to Top