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

redis-insights-connection

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方式

插入一个实体

person-create

查询

person-index

    模糊查询redis数据(*rab*)

redis-search

查看redis数据库数据

redis-browser

redis数据库模糊查询

redis-insight-search-index

测试json document方式

同样的方式插入json文档,然后在你redis数据库里面查看

redis-insight-redis-json

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™

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1543508.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

【第十二周】李宏毅机器学习笔记10:生成式对抗网络2

目录 摘要Abstract1.GAN is Still Challenging2.Evaluation of Generation2.1 Mode Collapse2.2.Mode Dropping2.3.Diversity 3.Conditional GAN4.Learning from Unpaired Data总结 摘要 本周主要学习了上周关于生成式对抗网络的剩余知识&#xff0c;了解了为什么 GAN 难以训练…

【数字】flexnoc Qos配置

对于Qos_generator 的regulator模式来说可以配置的寄存器如下表&#xff1a; 因为我们没有使用external的clk来做count&#xff0c;也没有使用外部的threshold&#xff0c;所以都是使用的internal的时钟。 根据文档讲解的概念Bandwidth设置为下&#xff1a; Urgency使用来源如…

Nat Med.作者提供全文的绘图代码,对于学习作图很有帮助(一)

本期教程 获得本期教程全文代码&#xff1a;在订阅号后台回复关键词&#xff1a;20240923 2022年教程总汇 2023年教程总汇 引言 今天分享的文章是2024发表在Nat Med.期刊中&#xff0c;来自上海交通大学医学院的文章&#xff0c;作者提供了全文的绘图代码&#xff0c;确实勇&a…

S32K3 工具篇8:如何移植RTD MCAL现有demo到其他K3芯片

S32K3 工具篇8&#xff1a;如何移植RTD MCAL现有demo到其他K3芯片 一&#xff0c;文档简介二 &#xff0c;平台以及移植步骤2.1 平台说明2.2 移植步骤2.2.1 拷贝工程并配置2.2.1.1 拷贝工程2.2.1.2 配置工程 2.2.2 EB 工程配置 三&#xff0c; 命令行编译及其结果测试四&#x…

在idea里运行swing程序正常,但是在外部运行jar包却报错,可能是jdk版本问题

在idea里运行swing程序异常&#xff0c;报Caused by: java.awt.HeadlessException错误 System.setProperty("java.awt.headless","false");加上这句话

Spring Data Rest 远程命令执行命令(CVE-2017-8046)

&#xff08;1&#xff09;访问 http://your-ip:8080/customers/1&#xff0c;然后抓取数据包&#xff0c;使用PATCH请求来修改 PATCH /customers/1 HTTP/1.1 Host: Accept-Encoding: gzip, deflate Accept: */* Accept-Language: en User-Agent: Mozilla/5.0 (compatible; MS…

iOS OC 底层原理之 category、load、initialize

文章目录 category底层结构runtime 执行 category 底层原理添加成员变量 load调用形式系统调用形式的内部原理源码实现逻辑 initialize调用形式源码核心函数&#xff08;由上到下依次调用&#xff09;如果分类实现了 initialize category 底层结构 本质是结构体。struct _cat…

AlmaLinux 安裝JDK8

在 AlmaLinux 上安装 JDK 8 可以通过包管理器 dnf 来完成。AlmaLinux 是基于 RHEL 的一个开源发行版&#xff0c;因此其包管理系统和 RHEL 类似。以下是详细的步骤来安装 OpenJDK 8 1. 更新系统包列表 sudo dnf update -y 2. 安装 OpenJDK 8 使用 dnf 安装 OpenJDK 8。你可…

召回的在线评估与离线评估

在现代信息检索、推荐系统等应用场景中&#xff0c;召回阶段扮演着至关重要的角色。召回系统负责从海量候选项中筛选出潜在相关的内容&#xff0c;因此其效果直接影响用户的满意度和系统的效率。为了确保召回系统的性能&#xff0c;我们需要对其进行评估&#xff0c;而评估方法…

C++中set和map的使用

1.关联式容器 序列式容器里存储的是元素本身&#xff0c;如vector、list、deque 关联式容器即&#xff0c;容器中存储<key&#xff0c;value>的键值对&#xff0c;树型结 构的关联式容器主要有四种&#xff1a;map、set、multimap、multiset。他们都使用平衡搜索树(即红…

医学数据分析实训 项目四 回归分析--预测帕金森病病情的严重程度

文章目录 项目四&#xff1a;回归分析实践目的实践平台实践内容 预测帕金森病病情的严重程度作业&#xff08;一&#xff09;数据读入及理解&#xff08;二&#xff09;数据准备&#xff08;三&#xff09;模型建立&#xff08;四&#xff09;模型预测&#xff08;五&#xff0…

Web端云剪辑解决方案,可实现移动端、PC、云平台无缝兼容

美摄科技作为业界领先的视频技术解决方案提供商&#xff0c;再次以科技创新为驱动&#xff0c;隆重推出其Web端云剪辑解决方案&#xff0c;彻底颠覆传统剪辑模式的界限&#xff0c;为视频创作者、影视制作人及广告从业者带来前所未有的高效与便捷。 【跨平台无缝协作&#xff…

消息队列:如何确保消息不会丢失?

引言 对业务系统来说&#xff0c;丢消息意味着数据丢失&#xff0c;这是无法接受的。 主流的消息队列产品都提供了非常完善的消息可靠性保证机制&#xff0c;完全可以做到在消息传递过程中&#xff0c;即使发生网络中断或者硬件故障&#xff0c;也能确保消息的可靠传递&#…

智能优化算法-多目标灰狼优化算法(MOGWO)(附源码)

目录 1.内容介绍 2.部分代码 3.实验结果 4.内容获取 1.内容介绍 多目标灰狼优化算法 (Multi-Objective Grey Wolf Optimizer, MOGWO) 是一种基于群体智能的元启发式优化算法&#xff0c;它扩展了经典的灰狼优化算法 (GWO)&#xff0c;专门用于解决多目标优化问题。MOGWO通过模…

IT监控管理工具 WGCLOUD - 使用公共告警消息推送接口

WGCLOUD的公共告警接口 用于外部业务系统调用的告警接口&#xff0c;需要升级到v3.4.5或以上版本 只要调用这个接口&#xff0c;就可以将消息同步推送到我们的告警平台&#xff0c;比如邮件&#xff0c;钉钉&#xff0c;企业微信等 此接口主要给有告警需求的第三方系统使用&…

软件功能测试需进行哪些测试?第三方软件测评机构有哪些测试方法?

在信息化社会迅速发展的今天&#xff0c;软件功能测试在软件开发生命周期中占据着不可或缺的地位。软件功能测试是评估软件系统是否符合预期功能和用户需求的过程。其重要性体现在提升软件质量、确保用户满意度以及降低维护成本等方面。 软件功能测试是对软件应用程序进行的一…

软件测试实验室如何利用GB/T25000标准建立测试技术体系

《系统与软件工程 系统与软件质量要求和评价&#xff08;SQuaRE&#xff09;》是国际标准化组织ISO/IEC为统一软件质量评判标准而指定的软件质量度量和评价的标准。该标准是开展中国合格评定国家认可委员会&#xff08;CNAS&#xff09;实验室认可软件测评实验室过程中需要参照…

开源模型应用落地-Qwen2.5-Coder模型小试-码无止境(一)

一、前言 代码专家模型是一种基于人工智能的先进技术&#xff0c;旨在自动分析和理解大量代码库&#xff0c;并从中学习常见的编码模式和最佳实践。这种模型通过深度学习和自然语言处理&#xff0c;能够提供准确而高效的代码建议&#xff0c;帮助开发人员在编写代码时有效地避免…

[ComfyUI]Flux:太美啦!绮梦流光-水湄凝香,写实与虚拟混合,极致细节和质感

大家好我是安琪&#xff01;&#xff01;&#xff01; 在数字艺术和创意领域&#xff0c;[ComfyUI]Flux已经成为艺术家和设计师们手中的利器。今天&#xff0c;我们激动地宣布&#xff0c;[ComfyUI]Flux带来了一款令人瞩目的创新作品——绮梦流光-水湄凝香。这款作品将写实与虚…

怎么把kgm转换成mp3?5个kgm转mp3的方法,亲测管用!

很多小伙伴不难发现kgm格式只能在固定的平台或设备上播放&#xff0c;如果想要打破这一限制&#xff0c;我们可以将kgm格式转换为兼容性较强的mp3格式。 下面&#xff0c;就来给大家分享5个好用的kgm转mp3方法&#xff0c;操作简单&#xff0c;小白也能分分钟学会哦~ kgm转mp3…