RedisStack之RedisSearch使用

安装RedisStack

这里我们选择通过Docker来安装, 具体细节可以参考: Run Redis Stack on Docker

docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest

RedisStack中自动就包含了RedisSearch模块

如果使用的是Redis,Redis版本需要在v6.x或以上,并且需要安装和开启RediSearch(v2.2 or later)模块。

RedisJson模块的使用和RedisSearch同理

连接RedisStack

可以通过redis-cli客户端来连接, 如果发现redis-cli命令不存在,可以选择本地安装一下或者直接进入上面运行的容器内部使用redis-cli命令

// 进入容器里面
docker exec -it 28c3e594f5aa /bin/sh
// 连接redis
#redis-cli

创建一个 Index

127.0.0.1:6379> FT.CREATE myIdx ON HASH PREFIX 1 doc: SCHEMA title TEXT WEIGHT 5.0 body TEXT url TEXT

通过上面的命令,创建了一个支持hash数据类型的索引,同时需要满足key前缀为 doc: 。支持索引的字段说明如下:

字段

类型

权重(默认权重为1.0)

title

TEXT

5

body

TEXT

1.0

url

TEXT

1.0

新增数据

127.0.0.1:6379> HSET doc:1 title "hello world" body "lorem ipsum" url "http://redis.io"

满足索引key前缀定义的数据,在新增后都会自动加入到对应的索引中

使用索引

127.0.0.1:6379> FT.SEARCH myIdx "hello world" LIMIT 0 10
1) (integer) 1
2) "doc:1"
3) 1) "title"
   2) "hello world"
   3) "body"
   4) "lorem ipsum"
   5) "url"
   6) "http://redis.io"

支持JSON数据的索引

这个就需要上面提到的RedisJson模块的支持了。

简单演示如下:创建一个索引,字段包含:name, description,price和embedding

127.0.0.1:6379> FT.CREATE itemIdx ON JSON PREFIX 1 item: SCHEMA $.name AS name TEXT $.description as description TEXT $.price AS price NUMERIC $.embedding AS embedding VECTOR FLAT 6 DIM 4 DISTANCE_METRIC L2 TYPE FLOAT32

JSON数据格式如下:

示例数据1:

{
  "name": "Noise-cancelling Bluetooth headphones",
  "description": "Wireless Bluetooth headphones with noise-cancelling technology",
  "connection": {
    "wireless": true,
    "type": "Bluetooth"
  },
  "price": 99.98,
  "stock": 25,
  "colors": [
    "black",
    "silver"
  ],
  "embedding": [0.87, -0.15, 0.55, 0.03]
}

示例数据2:

{
  "name": "Wireless earbuds",
  "description": "Wireless Bluetooth in-ear headphones",
  "connection": {
    "wireless": true,
    "type": "Bluetooth"
  },
  "price": 64.99,
  "stock": 17,
  "colors": [
    "black",
    "white"
  ],
  "embedding": [-0.7, -0.51, 0.88, 0.14]
}

通过RedisJson写命令,比如JSON.SETJSON.ARRAPPEND 来创建和修改JSON数据

写入示例数据到数据库中:

127.0.0.1:6379> JSON.SET item:1 $ '{"name":"Noise-cancelling Bluetooth headphones","description":"Wireless Bluetooth headphones with noise-cancelling technology","connection":{"wireless":true,"type":"Bluetooth"},"price":99.98,"stock":25,"colors":["black","silver"],"embedding":[0.87,-0.15,0.55,0.03]}'
"OK"
127.0.0.1:6379> JSON.SET item:2 $ '{"name":"Wireless earbuds","description":"Wireless Bluetooth in-ear headphones","connection":{"wireless":true,"type":"Bluetooth"},"price":64.99,"stock":17,"colors":["black","white"],"embedding":[-0.7,-0.51,0.88,0.14]}'
"OK"

更多内容: Index and search JSON documents

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1. 首先需要在pom.xml中添加redis和jedis的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.0.1</version> </dependency> ``` 2. 在application.properties中配置redis连接信息: ```properties # Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redis.host=localhost # Redis服务器连接端口 spring.redis.port=6379 # Redis服务器连接密码(默认为空) # spring.redis.password= # 连接超时时间(毫秒) spring.redis.timeout=10000 ``` 3. 编写一个RedisStack类,用于实现栈的基本操作: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import java.util.List; @Component public class RedisStack<T> { private static final String KEY_PREFIX = "redis_stack:"; private final RedisTemplate<String, T> redisTemplate; @Autowired public RedisStack(RedisTemplate<String, T> redisTemplate) { this.redisTemplate = redisTemplate; } public void push(String key, T value) { redisTemplate.opsForList().leftPush(KEY_PREFIX + key, value); } public T pop(String key) { return redisTemplate.opsForList().leftPop(KEY_PREFIX + key); } public List<T> getAll(String key) { return redisTemplate.opsForList().range(KEY_PREFIX + key, 0, -1); } } ``` 4. 编写一个DutySchedule类,用于实现值班表的轮询: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; @Component public class DutySchedule { private final RedisStack<String> redisStack; @Autowired public DutySchedule(RedisStack<String> redisStack) { this.redisStack = redisStack; } public String getNextDuty(String key) { List<String> allDuties = redisStack.getAll(key); if (allDuties == null || allDuties.isEmpty()) { return null; } String lastDuty = redisStack.pop(key); redisStack.push(key, lastDuty); return lastDuty; } } ``` 5. 在其他代码中使用RedisStack和DutySchedule: ```java @Autowired private RedisStack<String> redisStack; @Autowired private DutySchedule dutySchedule; @Test public void testRedisStack() { redisStack.push("mykey", "value1"); redisStack.push("mykey", "value2"); redisStack.push("mykey", "value3"); List<String> allValues = redisStack.getAll("mykey"); System.out.println(allValues); String popValue = redisStack.pop("mykey"); System.out.println(popValue); allValues = redisStack.getAll("mykey"); System.out.println(allValues); } @Test public void testDutySchedule() { redisStack.push("duty_schedule", "张三"); redisStack.push("duty_schedule", "李四"); redisStack.push("duty_schedule", "王五"); System.out.println(dutySchedule.getNextDuty("duty_schedule")); System.out.println(dutySchedule.getNextDuty("duty_schedule")); System.out.println(dutySchedule.getNextDuty("duty_schedule")); System.out.println(dutySchedule.getNextDuty("duty_schedule")); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JYCJ_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值