SoFunction
Updated on 2025-05-20

Detailed examples of Spring Boot Integration Solr

Environmental preparation

  • Install Solr: From Solr's official website (Welcome to Apache Solr - Apache Solr) Download and install the latest version, and then use the commandbin/solr startStart the Solr service and usebin/solr create -c mycoreCreate a new Solr core.
  • Install JDK: Make sure that JDK 8 and above are installed.
  • Configure Maven: Create Spring Boot project using Maven as a project building tool.

Add dependencies

In Spring Boot ProjectAdd the following dependencies to the file:

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

This dependency automatically configures Spring Data Solr-related components, including Solr client and Spring Solr support.

Configure Solr Connection

existorAdd Solr's connection configuration to the file, as shown below:

=http://localhost:8983/solr
=mycore

spring:
  data:
    solr:
      host: http://localhost:8983/solr
      core: mycore

Define entity classes

Create an entity class to map documents in Solr, as shown below:

package ;
import ;
import ;
import ;
@SolrDocument(collection = "mycore")
public class Product {
    @Id
    @Field
    private String id;
    @Field
    private String name;
    @Field
    private String description;
    @Field
    private double price;
    // Getters and Setters
}

in,@SolrDocument(collection = "mycore")Specifies the core name of Solr.@FieldAnnotations are used to map fields of an entity class to fields of a Solr document.

Writing Repository Interface

Create an inherited fromSolrCrudRepositoryThe interface to operate the data in Solr, the example is as follows:

package ;
import ;
import ;
import ;
public interface ProductRepository extends SolrCrudRepository<Product, String> {
    List<Product> findByNameContaining(String name);
}

Through inheritanceSolrCrudRepositoryInterface, which can facilitate the addition, deletion, modification and search of documents.findByNameContainingMethods can be used to fuzzy queries by name.

Create Service and Controller

Create Service: Encapsulate business logic, the example is as follows:

package ;
import ;
import ;
import ;
import ;
import ;
@Service
public class ProductService {
    @Autowired
    private ProductRepository productRepository;
    public void saveProduct(Product product) {
        (product);
    }
    public List<Product> searchByName(String name) {
        return (name);
    }
    public void deleteProduct(String id) {
        (id);
    }
}

Create Controller: Process HTTP requests, the examples are as follows:

package ;
import ;
import ;
import ;
import .*;
import ;
@RestController
@RequestMapping("/products")
public class ProductController {
    @Autowired
    private ProductService productService;
    @PostMapping
    public void addProduct(@RequestBody Product product) {
        (product);
    }
    @GetMapping("/search")
    public List<Product> searchProducts(@RequestParam String name) {
        return (name);
    }
    @DeleteMapping("/{id}")
    public void deleteProduct(@PathVariable String id) {
        (id);
    }
}

Sample Run and Test

Add products: After starting the Spring Boot application, send a POST request to add the product, such as using the curl command:

curl -X POST -H "Content-Type: application/json" -d '{"id":"1","name":"Laptop","description":"High performance laptop","price":1000}' http://localhost:8080/products

Search for products: Send GET request to search for products:

curl http://localhost:8080/products/search?name=Laptop

Delete the product: Send DELETE request to delete the product:

curl -X DELETE http://localhost:8080/products/1

This is the end of this article about Spring Boot Integration Solr. For more related Spring Boot Integration Solr content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!