Spring Boot集成Redis Search快速入门Demo

news2024/9/23 21:34:54

1.什么是Redis Search?

RedisSearch 是一个基于 Redis 的搜索引擎模块,它提供了全文搜索、索引和聚合功能。通过 RedisSearch,可以为 Redis 中的数据创建索引,执行复杂的搜索查询,并实现高级功能,如自动完成、分面搜索和排序。利用 Redis 的高性能特点,RedisSearch 可以实现高效的搜索和实时分析。对于微服务架构来说,RedisSearch 可以作为搜索服务的一部分,提供快速、高效的搜索能力,对于提高用户体验和性能具有重要的意义。

2.环境搭建

Docker Compose

version: '3'
services:

  redis:
    image: redis/redis-stack
    container_name: redis
    ports:
      - 6379:6379

  redis-insight:
    image: redislabs/redisinsight
    container_name: redis-insight
    ports:
      - 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 {

    @Id
    private String id;
    @Indexed
    private String name;

    @Searchable
    private 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 {

    @Id
    private String id;
    @Indexed
    private String name;

    @Searchable
    private 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: localhost
    port: 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.coloradmin.cn/o/2158511.html

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

相关文章

简历技能面试问答

变成语言和开发工具 C新特性 自动类型推导 (auto)&#xff1a; 自动推导变量的类型&#xff0c;减少显式类型声明的繁琐 范围 for 循环&#xff1a; 用于遍历容器或数组&#xff0c;简化代码。 nullptr&#xff1a; nullptr 取代 NULL&#xff0c;表示空指针&#xff0c;类…

出海企业如何利用海外云手机开展业务?

随着全球化的持续推进&#xff0c;越来越多的企业将目光投向海外市场。然而&#xff0c;面对不同国家的网络环境、政策限制以及文化和语言的差异&#xff0c;出海企业在拓展海外业务时遇到了许多障碍。海外云手机作为一种创新的解决方案&#xff0c;为企业提供支持和帮助。 海外…

【高阶用法】uniapp的i18n/修复/增强/App无重启更换语言

痛点 在i18n多语言模块使用过程中&#xff0c;发现下面几个问题&#xff0c;需要解决 1&#xff09;uni-best框架下&#xff0c;$t功能函数无法实时的切换语言&#xff0c;可能跟使用有关 2&#xff09;uni-best建议的translate方式在vue块外使用太繁琐&#xff0c;希望不用…

verilog中非阻塞多个if 优先级判断。

always(posedge clk)beginz < 0;if(sel0)z < a;if(sel1)z < b;if(sel2)z < c;if(sel3)z < d;end 比如上述代码&#xff0c;最后一级的优先级最高。

深入理解 JavaScript 三大作用域:全局作用域、函数作用域、块级作用域

一. 作用域 对于多数编程语言&#xff0c;最基本的功能就是能够存储变量当中的值、并且允许我们对这个变量的值进行访问和修改。那么有了变量之后&#xff0c;应该把它放在哪里、程序如何找到它们&#xff1f;是否需要提前约定好一套存储变量、访问变量的规则&#xff1f;答案…

Teams会议侧边栏应用开发-会议转写

Teams应用开发&#xff0c;主要是权限比较麻烦&#xff0c;大量阅读和实践&#xff0c;摸索了几周&#xff0c;才搞明白。现将经验总结如下&#xff1a; 一、目标&#xff1a;开发一个Teams会议的侧边栏应用&#xff0c;实现会议的实时转写。 二、前提&#xff1a; 1&#x…

株洲芦淞大桥事故的深刻反思

株洲芦淞大桥事故的深刻反思 2024年9月23日清晨&#xff0c;株洲芦淞大桥上发生了一起令人痛心的交通事故&#xff0c;一辆白色小汽车被出租车追尾后失控&#xff0c;冲向对向车道&#xff0c;最终酿成6人死亡、多人受伤的惨剧。 这起事故不仅给受害者家庭带来了无法弥补的伤…

【Python机器学习系列】开发Streamlit应用程序并部署机器学习模型(案例+源码)

这是我的第357篇原创文章。 一、引言 近年来&#xff0c;随着机器学习和人工智能技术的迅猛发展&#xff0c;越来越多的研究者选择将他们的模型以应用程序&#xff08;App&#xff09;的形式进行部署&#xff0c;从而使审稿人和其他研究者可以通过简单的界面&#xff0c;输入相…

9月23日

头文件 // My_string.h #ifndef MY_STRING_H #define MY_STRING_H#include <cstring> #include <algorithm>class My_string { private:char* data;size_t length;void resize(size_t new_length) {size_t new_capacity std::max(new_length 1, length);char* n…

一种求解城市场景下无人机三维路径规划的高维多目标优化算法,MATLAB代码

在城市环境下进行无人机三维路径规划时&#xff0c;需要考虑的因素包括高楼、障碍物、飞行安全和效率等。为了解决这些问题&#xff0c;研究者们提出了多种算法&#xff0c;包括基于智能优化算法的方法。 首先&#xff0c;无人机航迹规划问题的数学模型需要考虑无人机的基本约…

用Flowise+OneAPI+Ollama做一个在线翻译工作流

用FlowiseOneAPIOllama做一个在线翻译工作流&#xff0c;输入一种语言&#xff0c;马上翻译成另外一种语言&#xff0c;使用到的结点主要有&#xff0c;ChatLLM、提示词模板还有LLM Chain。 一、设置OneAPI和Ollama 1、Ollama的安装及配置&#xff0c;请参考&#xff1a;在ub…

三种委派 非约束委派 约束委派 基于资源的约束委派 概念

前言 简单记录下委派攻击的概念。具体的攻击演示/复现这里没有。 强烈建议反复通读《域渗透攻防指南》P242开始的4.5&#xff01;&#xff01;&#xff01; 以前看gitbook那个学的&#xff0c;yysy&#xff0c;真的不怎么适合零基础的看。 趁课上认真看了看4.5章&#xff0c…

如何快速免费搭建自己的Docker私有镜像源来解决Docker无法拉取镜像的问题(搭建私有镜像源解决群晖Docker获取注册表失败的问题)

文章目录 📖 介绍 📖🏡 演示环境 🏡📒 Docker无法拉取镜像 📒📒 解决方案 📒🔖 方法一:免费快速搭建自己的Docker镜像源🎈 部署🎈 使用🔖 备用方案⚓️ 相关链接 🚓️📖 介绍 📖 在当前的网络环境下,Docker镜像的拉取问题屡见不鲜(各类Nas查询…

【编程基础知识】MySQL中什么叫做聚簇索引、非聚簇索引、回表、覆盖索引

一、引言 在数据库的奇妙世界里&#xff0c;索引是提升查询速度的超级英雄。就像图书馔的目录帮助我们快速找到书籍一样&#xff0c;MySQL中的索引加速了数据检索的过程。本文将带你深入了解MySQL中的聚簇索引、非聚簇索引、回表操作以及覆盖索引&#xff0c;探索它们如何影响…

机器人顶刊IEEE T-RO发布无人机动态环境高效表征成果:基于粒子的动态环境连续占有地图

摘要&#xff1a;本研究有效提高了动态环境中障碍物建模的精度和效率。NOKOV度量动作捕捉系统助力评估动态占用地图在速度估计方面的性能。 近日&#xff0c;上海交通大学、荷兰代尔夫特理工研究团队在机器人顶刊IEEE T-RO上发表题为Continuous Occupancy Mapping in Dynamic …

Keysight 下载信源 Visa 指令

用于传输原始的IQ数据 file.wiq 或者 file.bin wave_bin:bytes with open("./WaveForm.wfm","rb") as f:wave_bin f.read()log.info("File:WaveForm.wfm Size:%d Bytes"%len(wave_bin)) IMPL.sendCommand(":MEM:DATA \"WFM1:FILE1\&q…

每日OJ题_牛客_杨辉三角(动态规划)

目录 牛客_杨辉三角&#xff08;动态规划&#xff09; 解析代码 牛客_杨辉三角&#xff08;动态规划&#xff09; 杨辉三角_牛客题霸_牛客网 解析代码 最基础的 dp 模型&#xff0c;按照规律模拟出来杨辉三角即可。 #include <iostream> using namespace std;int dp…

企业上云不迷茫,香港电讯助力企业上云全攻略

在全球政策和市场双重驱动下&#xff0c;云计算产业正迎来前所未有的增长浪潮。据中国信通院《云计算白皮书&#xff08;2023年&#xff09;》1显示&#xff0c;2022年全球云计算市场规模已达到4,910亿美元&#xff0c;同比增长率高达百分之十九。而在中国市场&#xff0c;这一…

带线无人机现身俄罗斯抗干扰技术详解

带线无人机在俄罗斯的出现&#xff0c;特别是其光纤制导技术的应用&#xff0c;标志着无人机抗干扰技术的一大进步。以下是对俄罗斯带线无人机抗干扰技术的详细解析&#xff1a; 一、带线无人机抗干扰技术背景 技术突破&#xff1a;俄军成功研发了光纤制导无人机&#xff0c;…

数据链路层协议 —— 以太网协议

目录 1.数据链路层解决的问题 2.局域网通信方式 以太网 令牌环网 无线局域网 3.以太网协议 以太网帧格式 对比理解Mac地址和IP地址 认识MTU MTU对IP协议的影响 MTU对UDP的影响 MTU对TCP的影响 基于以太网协议的报文转发流程 交换机的工作原理 4.ARP协议 ARP协议…