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及其基本使用(含语法介绍)

相关推荐
开心就好20252 分钟前
全面介绍iOS开发工具:Xcode、AppCode、CocoaPods、Fastlane和Git
后端·ios
一 乐8 分钟前
交通感知与车路协同系统|基于springboot + vue交通感知与车路协同系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·交通感知与车路协同系统
uElY ITER9 分钟前
基于Spring Boot 3 + Spring Security6 + JWT + Redis实现登录、token身份认证
spring boot·redis·spring
Rust研习社41 分钟前
Rust Pin 解析:核心原理与异步编程实践
开发语言·后端·rust
book123_0_9941 分钟前
Spring Boot 条件注解:@ConditionalOnProperty 完全解析
java·spring boot·后端
NCIN EXPE43 分钟前
使用Springboot + netty 打造聊天服务(一)
java·spring boot·后端
涡能增压发动积43 分钟前
日志里全是 $ref,但客户端居然没事?
后端
我叫黑大帅1 小时前
受保护的海报图片读取方案 - 在不公开静态资源目录下如何获取静态资源
后端·python·面试
我母鸡啊1 小时前
软考架构师故事系列-嵌入式技术
后端
MXN_小南学前端1 小时前
Vue3 + Spring Boot 工单系统实战:用户反馈和客服处理的完整闭环(提供gitHub仓库地址)
前端·javascript·spring boot·后端·开源·github