Spring Boot集成Redis Search快速入门Demo
1.什么是Redis Search?
RedisSearch 是一个基于 Redis 的搜索引擎模块,它提供了全文搜索、索引和聚合功能。通过 RedisSearch,可以为 Redis 中的数据创建索引,执行复杂的搜索查询,并实现高级功能,如自动完成、分面搜索和排序。利用 Redis 的高性能特点,RedisSearch 可以实现高效的搜索和实时分析。对于微服务架构来说,RedisSearch 可以作为搜索服务的一部分,提供快速、高效的搜索能力,对于提高用户体验和性能具有重要的意义。
2.环境搭建
Docker Compose
version: '3' services:redis:image: redis/redis-stackcontainer_name: redisports:- 6379:6379redis-insight:image: redislabs/redisinsightcontainer_name: redis-insightports:- 8001:8001
Run following command:
docker-compose up -d
3.代码工程
实验目的
利用redis search 实现文本搜索功能
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.1</version></parent><modelVersion>4.0.0</modelVersion><artifactId>RedisSearch</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.redis.om</groupId><artifactId>redis-om-spring</artifactId><version>0.8.2</version></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies>
</project>
controller
package com.et.controller;import com.et.redis.document.Student;
import com.et.redis.document.StudentRepository;
import com.et.redis.hash.Person;
import com.et.redis.hash.PersonRepository;
import jakarta.websocket.server.PathParam;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;@RestController
public class WebController {private PersonRepository personRepository;private StudentRepository studentRepository;public WebController(PersonRepository personRepository, StudentRepository studentRepository) {this.personRepository = personRepository;this.studentRepository = studentRepository;}@PostMapping("/person")public Person save(@RequestBody Person person) {return personRepository.save(person);}@GetMapping("/person")public Person get(@PathParam("name") String name, @PathParam("searchLastName") String searchLastName) {if (name != null)return this.personRepository.findByName(name).orElseThrow(() -> new RuntimeException("person not found"));if (searchLastName != null)return this.personRepository.searchByLastName(searchLastName).orElseThrow(() -> new RuntimeException("person not found"));return null;}// ---- Student Endpoints@PostMapping("/student")public Student saveStudent(@RequestBody Student student) {return studentRepository.save(student);}@GetMapping("/student")public Student getStudent(@PathParam("name") String name, @PathParam("searchLastName") String searchLastName) {if (name != null)return this.studentRepository.findByName(name).orElseThrow(() -> new RuntimeException("Student not found"));if (searchLastName != null)return this.studentRepository.searchByLastName(searchLastName).orElseThrow(() -> new RuntimeException("Student not found"));return null;}@ExceptionHandler(value = RuntimeException.class)public ResponseEntity handleError(RuntimeException e) {return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());}}
@RedisHash 方式
package com.et.redis.hash;import com.redis.om.spring.annotations.Indexed;
import com.redis.om.spring.annotations.Searchable;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;@RedisHash
public class Person {@Idprivate String id;@Indexedprivate String name;@Searchableprivate String lastName;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}
}
package com.et.redis.hash;import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;import java.util.Optional;@Repository
public interface PersonRepository extends CrudRepository<Person, String> {Optional<Person> findByName(String name);Optional<Person> searchByLastName(String name);
}
@Document 方式
package com.et.redis.document;import com.redis.om.spring.annotations.Document;
import com.redis.om.spring.annotations.Indexed;
import com.redis.om.spring.annotations.Searchable;
import org.springframework.data.annotation.Id;@Document
public class Student {@Idprivate String id;@Indexedprivate String name;@Searchableprivate String lastName;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}
}
package com.et.redis.document;import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;import java.util.Optional;@Repository
public interface StudentRepository extends CrudRepository<Student, String> {Optional<Student> findByName(String name);Optional<Student> searchByLastName(String name);
}
DemoApplication
package com.et;import com.redis.om.spring.annotations.EnableRedisDocumentRepositories;
import com.redis.om.spring.annotations.EnableRedisEnhancedRepositories;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableRedisDocumentRepositories(basePackages = "com.et.redis.document")
@EnableRedisEnhancedRepositories(basePackages = "com.et.redis.hash")
@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}
application.yaml
server:port: 8088
spring:redis:host: localhostport: 6379
只是一些关键代码,所有代码请参见下面代码仓库
代码仓库
- https://github.com/Harries/springboot-demo(redis search)
4.测试
启动Spring boot应用
测试hash方式
插入一个实体
查询
模糊查询redis数据(*rab*)
查看redis数据库数据
redis数据库模糊查询
测试json document方式
同样的方式插入json文档,然后在你redis数据库里面查看
5.引用
- https://blog.devgenius.io/redis-search-with-spring-boot-and-redis-om-searchable-indexed-ttl-ccf2fb027d96
- How to Search & Query Redis with A Spring Boot Application | RefactorFirst
- Spring Boot集成Redis Search快速入门Demo | Harries Blog™
相关文章:

Spring Boot集成Redis Search快速入门Demo
1.什么是Redis Search? RedisSearch 是一个基于 Redis 的搜索引擎模块,它提供了全文搜索、索引和聚合功能。通过 RedisSearch,可以为 Redis 中的数据创建索引,执行复杂的搜索查询,并实现高级功能,如自动完…...

提升工作效率神器
这五款软件让你事半功倍 在当今快节奏的社会中,提高工作效率成为了每个人追求的目标。而在这个数字化时代,选择对的软件工具无疑是提高效率的关键。今天,我为大家推荐五款优秀的工作效率软件,帮助你在工作中事半功倍。 1、亿可达…...

统信服务器操作系统【targetcli部署】
targetcli部署方案 文章目录 功能概述功能介绍1.安装targetcli2.targetcli语法及参数说明3.示例1. 配置2.访问功能概述 SCSI 即小型计算机系统接口(Small Computer System Interface;简写:SCSI) iSCSI,internet SCSI 网络磁盘 ,提供一对一的网络存储, 主机A 提供xx存储设…...

I2C中继器TCA9517A(TI)
一、芯片介绍 本芯片是一款具有电平转换功能的双向缓冲器,适用于I2C和SMBus系统,同时支持各种拓扑结构的扩展使用。芯片支持SCL和SDA缓冲,因此允许两条总线的负载电容达到400pF。 TCA9517A的A和B侧驱动器是不同的,但是均可耐受5…...
基于单片机的智能电话控制系统设计
摘要: 为了能够使用电话实现电器设备的控制,文中通过单片机及双音多频解码集成电路,使用用 户通过电话输入相应的指令就能够实现远程设备的智能化控制。文章主要对系统的构成、软件及 硬件设计进行了简单的介绍,并且对其中的电路进行了简单的说明,比如语音提示、双音频解…...

Go 综合题面试题
1. Golang 中 make 和 new 的区别? #make 和 new 都用于内存分配1:接收参数个数不一样: new() 只接收一个参数,而 make() 可以接收3个参数2:返回类型不一样: new() 返回一个指针,而 make() 返回…...

【Python报错已解决】AttributeError: ‘Tensor‘ object has no attribute ‘kernel_size‘
🎬 鸽芷咕:个人主页 🔥 个人专栏: 《C干货基地》《粉丝福利》 ⛺️生活的理想,就是为了理想的生活! 专栏介绍 在软件开发和日常使用中,BUG是不可避免的。本专栏致力于为广大开发者和技术爱好者提供一个关于BUG解决的经…...

Spring MVC 参数校验 总结
1. 简介 Sping MVC提供了参数校验的方便注解。 2.代码 在pom.xml中添加依赖: <dependency><groupId>org.hibernate.validator</groupId><artifactId>hibernate-validator</artifactId><version>8.0.0.Final</version&g…...

【图像压缩与重构】基于BP神经网络
课题名称:基于BP神经网络的图像压缩与重构(带GUI) 相关资料: 1. 代码注释 2.BP神经网络原理文档资料 3.图像压缩原理文档资料 程序实例截图:...

数据结构--单链表创建、增删改查功能以及与结构体合用
一、作业要求 单链表操作,要求节点是结构体类型,实现以下功能: 1.尾插学生 2.任意位置插入学生 3.任意位置删除学生 4.逆置单链表 5.学生按学号排序 6.销毁单链表 二、实现过程 1.代码如下: (1)头…...

开源ids snort (windows版)
Snort-IPS-on-Windows-main资源-CSDN文库 GitHub - eldoktor1/Snort-IPS-on-Windows: A comprehensive guide to installing and configuring Snort IPS on Windows, ensuring robust network security 手动打造Snortbarnyard2BASE可视化告警平台 - FreeBuf网络安全行业门户 …...
关于 vue3 axios的封装,并发请求相关
简介 Axios 是一个基于 promise 网络请求库,作用于node.js 和浏览器中。 它是 isomorphic 的(即同一套代码可以运行在浏览器和node.js中)。在服务端它使用原生 node.js http 模块, 而在客户端 (浏览端) 则使用 XMLHttpRequests。 请求方法别名 为了方便起见&#x…...
cpp中的namespace详解
namespace的作用主要是为了避免名字冲突和组织代码。 命名空间在C中是一个非常重要的特性,它帮助开发者更好地管理代码和避免潜在的冲突。 具体来说,它有以下几个主要用途 避免名字冲突 在大型项目中可能会有很多个类、函数或变量使用相同的名称。使用…...
request库的使用 | get请求
requests 库的 get 方法用于发送 HTTP GET 请求。GET 请求通常用于请求服务器发送数据。 1、导入 requests 库: import requests 2、发送 GET 请求: 使用 requests.get() 方法发送请求。 response requests.get(urlhttp://www.jd.com) 3、查看响应…...
理想低通信道和理想带通信道的区别
一、定义与特性 理想低通信道(可通过<MAX): 定义:理想低通信道允许信号的所有低频分量,只要其频率不超过某个上限值,都能够不失真地通过此信道。而频率超过该上限值的所有高频分量都不能通过该信道。特…...

LAMP架构搭建
目录 LAMP架构搭建 编译安装Apache httpd服务 1、需要的安装包 2、关闭防火墙和核心防护 3、安装环境依赖包 4、配置软件模块 5、编译及安装 6、优化配置文件路径(可不做) 7、添加httpd系统服务 8、修改httpd 服务配置文件 9、浏览器访问验证…...

RT-DETR
SSE represents the single-scale Transformer encoder,CSF represents cross-scale fusion. AIFI and CCFF are the two modules designed into 作者的 hybrid encoder 截止到发文时间的issue数,多吓人呐,不建议复现...
【算法——KMP】
1理解next数组定义:最长相等前后缀(不含当前字符并且不能是整体) 算法讲解100【扩展】 KMP算法原理和代码详解_哔哩哔哩_bilibili next数组的值:假设这个i出现了不匹配就从next[i]的位置开始在再匹配 2next数组生成 看一下是怎…...

视频监控相关笔记
一、QT 之 QTreeWidget 树形控件 Qt编程指南,Qt新手教程,Qt Programming Guide 一个树形结构的节点中的图表文本 、附带数据的添加: QTreeWidgetItem* TourTreeWnd::InsertNode(NetNodeInfo node, QTreeWidgetItem* parent_item) { // …...
React 中,构建组件的方式
1. 函数组件(Function Components) 函数组件是最简单的组件形式,通常用于展示性的组件,不涉及复杂的生命周期方法。 import React from react;function Welcome(props) {return <h1>Hello, {props.name}</h1>; }exp…...

XML Group端口详解
在XML数据映射过程中,经常需要对数据进行分组聚合操作。例如,当处理包含多个物料明细的XML文件时,可能需要将相同物料号的明细归为一组,或对相同物料号的数量进行求和计算。传统实现方式通常需要编写脚本代码,增加了开…...
【杂谈】-递归进化:人工智能的自我改进与监管挑战
递归进化:人工智能的自我改进与监管挑战 文章目录 递归进化:人工智能的自我改进与监管挑战1、自我改进型人工智能的崛起2、人工智能如何挑战人类监管?3、确保人工智能受控的策略4、人类在人工智能发展中的角色5、平衡自主性与控制力6、总结与…...
Java 8 Stream API 入门到实践详解
一、告别 for 循环! 传统痛点: Java 8 之前,集合操作离不开冗长的 for 循环和匿名类。例如,过滤列表中的偶数: List<Integer> list Arrays.asList(1, 2, 3, 4, 5); List<Integer> evens new ArrayList…...

【单片机期末】单片机系统设计
主要内容:系统状态机,系统时基,系统需求分析,系统构建,系统状态流图 一、题目要求 二、绘制系统状态流图 题目:根据上述描述绘制系统状态流图,注明状态转移条件及方向。 三、利用定时器产生时…...

HBuilderX安装(uni-app和小程序开发)
下载HBuilderX 访问官方网站:https://www.dcloud.io/hbuilderx.html 根据您的操作系统选择合适版本: Windows版(推荐下载标准版) Windows系统安装步骤 运行安装程序: 双击下载的.exe安装文件 如果出现安全提示&…...
css的定位(position)详解:相对定位 绝对定位 固定定位
在 CSS 中,元素的定位通过 position 属性控制,共有 5 种定位模式:static(静态定位)、relative(相对定位)、absolute(绝对定位)、fixed(固定定位)和…...
基于matlab策略迭代和值迭代法的动态规划
经典的基于策略迭代和值迭代法的动态规划matlab代码,实现机器人的最优运输 Dynamic-Programming-master/Environment.pdf , 104724 Dynamic-Programming-master/README.md , 506 Dynamic-Programming-master/generalizedPolicyIteration.m , 1970 Dynamic-Programm…...
Typeerror: cannot read properties of undefined (reading ‘XXX‘)
最近需要在离线机器上运行软件,所以得把软件用docker打包起来,大部分功能都没问题,出了一个奇怪的事情。同样的代码,在本机上用vscode可以运行起来,但是打包之后在docker里出现了问题。使用的是dialog组件,…...

以光量子为例,详解量子获取方式
光量子技术获取量子比特可在室温下进行。该方式有望通过与名为硅光子学(silicon photonics)的光波导(optical waveguide)芯片制造技术和光纤等光通信技术相结合来实现量子计算机。量子力学中,光既是波又是粒子。光子本…...

算法岗面试经验分享-大模型篇
文章目录 A 基础语言模型A.1 TransformerA.2 Bert B 大语言模型结构B.1 GPTB.2 LLamaB.3 ChatGLMB.4 Qwen C 大语言模型微调C.1 Fine-tuningC.2 Adapter-tuningC.3 Prefix-tuningC.4 P-tuningC.5 LoRA A 基础语言模型 A.1 Transformer (1)资源 论文&a…...