当前位置: 首页 > news >正文

Spring Boot 集成 Solr 的详细步骤及示例

环境准备

  • 安装 Solr :从 Solr 官网(Welcome to Apache Solr - Apache Solr)下载并安装最新版本,然后通过命令 bin/solr start 启动 Solr 服务,使用 bin/solr create -c mycore 创建一个新的 Solr 核心。

  • 安装 JDK :确保安装了 JDK 8 及以上版本。

  • 配置 Maven :以 Maven 作为项目构建工具,创建 Spring Boot 项目。

添加依赖

在 Spring Boot 项目的 pom.xml 文件中添加以下依赖:

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

此依赖会自动配置 Spring Data Solr 的相关组件,包括 Solr 客户端和 Spring Solr 支持。

配置 Solr 连接

application.propertiesapplication.yml 文件中添加 Solr 的连接配置,示例如下:

  • application.properties

    spring.data.solr.host=http://localhost:8983/solr
    spring.data.solr.core=mycore
  • application.yml

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

    定义实体类

    创建一个实体类用于映射 Solr 中的文档,示例如下:

    package cn.juwatech.model;import org.apache.solr.client.solrj.beans.Field;
    import org.springframework.data.annotation.Id;
    import org.springframework.data.solr.core.mapping.SolrDocument;@SolrDocument(collection = "mycore")
    public class Product {@Id@Fieldprivate String id;@Fieldprivate String name;@Fieldprivate String description;@Fieldprivate double price;// Getters and Setters
    }

    其中,@SolrDocument(collection = "mycore") 指定了 Solr 的核心名称,@Field 注解用于将实体类的字段映射到 Solr 文档的字段。

    编写 Repository 接口

    创建一个继承自 SolrCrudRepository 的接口来操作 Solr 中的数据,示例如下:

    package cn.juwatech.repository;import cn.juwatech.model.Product;
    import org.springframework.data.solr.repository.SolrCrudRepository;import java.util.List;public interface ProductRepository extends SolrCrudRepository<Product, String> {List<Product> findByNameContaining(String name);
    }

    通过继承 SolrCrudRepository 接口,可方便地进行文档的增删改查操作,findByNameContaining 方法可用于按名称模糊查询。

    创建 Service 与 Controller

  • 创建 Service :封装业务逻辑,示例如下:

    package cn.juwatech.service;import cn.juwatech.model.Product;
    import cn.juwatech.repository.ProductRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;import java.util.List;@Service
    public class ProductService {@Autowiredprivate ProductRepository productRepository;public void saveProduct(Product product) {productRepository.save(product);}public List<Product> searchByName(String name) {return productRepository.findByNameContaining(name);}public void deleteProduct(String id) {productRepository.deleteById(id);}
    }
  • 创建 Controller :处理 HTTP 请求,示例如下:

    package cn.juwatech.controller;import cn.juwatech.model.Product;
    import cn.juwatech.service.ProductService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
    @RequestMapping("/products")
    public class ProductController {@Autowiredprivate ProductService productService;@PostMappingpublic void addProduct(@RequestBody Product product) {productService.saveProduct(product);}@GetMapping("/search")public List<Product> searchProducts(@RequestParam String name) {return productService.searchByName(name);}@DeleteMapping("/{id}")public void deleteProduct(@PathVariable String id) {productService.deleteProduct(id);}
    }

    示例运行与测试

  • 添加产品 :启动 Spring Boot 应用后,发送 POST 请求添加产品,如使用 curl 命令:

    curl -X POST -H "Content-Type: application/json" -d '{"id":"1","name":"Laptop","description":"High performance laptop","price":1000}' http://localhost:8080/products
  • 搜索产品 :发送 GET 请求搜索产品:

    curl http://localhost:8080/products/search?name=Laptop
  • 删除产品 :发送 DELETE 请求删除产品:

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

相关文章:

  • 36.金属壳体材料的选择与工艺处理对EMC的影响
  • C++ STL 容器详解:vector、string 和 map 的完全指南
  • kotlin中枚举带参数和不带参数的区别
  • C# 方法(局部函数和参数)
  • DDR在PCB布局布线时的注意事项及设计要点
  • SpringMVC框架详解与实践指南
  • 字符串,数组,指针之间的关系
  • 【NLP】 26. 语言模型原理与概率建模方法详解(Language Models)
  • FreeRTOS学习系列·二值信号量
  • TCP 与 UDP报文
  • 《数字图像处理(面向新工科的电工电子信息基础课程系列教材)》封面颜色空间一图的选图历程
  • linux系统基本操作命令
  • asyncio:Python的异步编程
  • Lua 元表和元方法
  • 【ArcGIS Pro微课1000例】0066:多边形要素添加折点,将曲线线段(贝塞尔、圆弧和椭圆弧)替换为线段?
  • Webug4.0靶场通关笔记16- 第20关文件上传(截断上传)
  • 【NLP】 31. Retrieval-Augmented Generation(RAG):KNN-LM, RAG、REALM、RETRO、FLARE
  • # 从零构建一个简单的卷积神经网络:手写数字识别
  • 【Unity】AssetBundle热更新
  • HTML 元素
  • 有乘客被高铁车门夹住?铁路回应:系突感不适下车,未受伤,列车正点发车
  • 发表“男性患子宫肌瘤”论文的杂志一年发文三千余篇,中介称可提供代写
  • 沙发上躺赢又如何?告别冠军绝缘体的凯恩,要开始收割荣誉了
  • 马克思主义理论研究教学名师系列访谈|王公龙:做好马克思主义研究,既要“钻进去”又要“跳出来”
  • 一周观展|上海浦东美术馆透纳展还剩最后5天
  • 美国加州州长:加州继续对中国“敞开贸易大门”