SpringBoot整合Solr进行搜索(简单)

SpringBoot整合Solr进行搜索

创建SpringBoot项目

这里基于aliyun提供的快速构建一个项目。我们这主要是整合Solr。

pom中加入Solr依赖

maven下载地址

pom中加入以下内容:

bash 复制代码
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-solr -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-solr</artifactId>
    <version>2.4.13</version>
</dependency>

配置 Solr

在 application.yml 或 application.properties 文件中配置 Solr 的连接信息:

yml 复制代码
# application.yml
spring:
  data:
    solr:
      host: http://localhost:8983/solr/my-core

或者

bash 复制代码
# application.properties
spring.data.solr.host=http://localhost:8983/solr/my-core

my-core就是我们之前安装的集合。我们写一个测试代码测试一下

创建实体

java 复制代码
import lombok.*;
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.SolrDocument;

import java.io.Serializable;

/**
 * @author by Guoshun
 * @version 1.0.0
 * @description 对应的实体
 * @date 2024/6/28 10:46
 */
@Setter
@Getter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@SolrDocument(collection = "test_core")
public class AppUser implements Serializable {

    @Id
    private String id;

    @Field("title")
    private String name;

    @Field("school_name")
    private String schoolName;

}

编写一个简单的ID查询

bash 复制代码
    @Autowired
    private SolrClient solrClient;

    @Test
    public void getAppUser() throws SolrServerException, IOException {
        SolrDocument byId = solrClient.getById("0006ed0ad3c14727849bf0a77257d86e");
        System.out.println(byId);
    }

打印结果

bash 复制代码
SolrDocument{name=李四, school_name=金山小学云翠校区, id=0006ed0ad3c14727849bf0a77257d86e, _version_=1803003388015673344}

参考文章

SpringBoot整合Solr及其基本使用(含语法介绍)

相关推荐
武子康1 小时前
Java-07 深入浅出 MyBatis数据库一对多关系模型实战:表结构设计与查询实现
java·后端
花椒技术2 小时前
企业内部 Agent 落地复盘:Gateway、Skill 和二次确认如何串起受控业务执行
后端·agent·ai编程
我是一颗柠檬4 小时前
【MySQL全面教学】MySQL事务与ACID Day9(2026年)
数据库·后端·mysql
枕星而眠4 小时前
数据结构八大排序详解(一):四大简单排序
c语言·数据结构·c++·后端
IT_陈寒4 小时前
React useEffect闭包陷阱差点把我整失业了
前端·人工智能·后端
苍何5 小时前
爆肝两周,我把 Codex 最全实战指南开源了
后端
苏渡苇5 小时前
服务容错的必要性与Spring Cloud Alibaba Sentinel 限流配置实战
spring boot·spring cloud·sentinel
bug菌5 小时前
【SpringBoot 3.x 第254节】夯爆了,数据库访问性能优化实战详解!
数据库·spring boot·后端
Rust研习社5 小时前
从碎片化到标准化:cargo-bp 如何重构 Rust 开发逻辑
后端·rust·编程语言
锋行天下5 小时前
一句mysql复杂查询搞崩一个壮汉
后端·mysql·go