赞
踩
这里我们选择通过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同理
可以通过redis-cli客户端来连接, 如果发现redis-cli命令不存在,可以选择本地安装一下或者直接进入上面运行的容器内部使用redis-cli命令
- // 进入容器里面
- docker exec -it 28c3e594f5aa /bin/sh
- // 连接redis
- #redis-cli
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"
这个就需要上面提到的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.SET 和JSON.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"
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。