当前位置:   article > 正文

RedisStack之RedisSearch使用

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命令

  1. // 进入容器里面
  2. docker exec -it 28c3e594f5aa /bin/sh
  3. // 连接redis
  4. #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前缀定义的数据,在新增后都会自动加入到对应的索引中

使用索引

  1. 127.0.0.1:6379> FT.SEARCH myIdx "hello world" LIMIT 0 10
  2. 1) (integer) 1
  3. 2) "doc:1"
  4. 3) 1) "title"
  5. 2) "hello world"
  6. 3) "body"
  7. 4) "lorem ipsum"
  8. 5) "url"
  9. 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:

  1. {
  2. "name": "Noise-cancelling Bluetooth headphones",
  3. "description": "Wireless Bluetooth headphones with noise-cancelling technology",
  4. "connection": {
  5. "wireless": true,
  6. "type": "Bluetooth"
  7. },
  8. "price": 99.98,
  9. "stock": 25,
  10. "colors": [
  11. "black",
  12. "silver"
  13. ],
  14. "embedding": [0.87, -0.15, 0.55, 0.03]
  15. }

示例数据2:

  1. {
  2. "name": "Wireless earbuds",
  3. "description": "Wireless Bluetooth in-ear headphones",
  4. "connection": {
  5. "wireless": true,
  6. "type": "Bluetooth"
  7. },
  8. "price": 64.99,
  9. "stock": 17,
  10. "colors": [
  11. "black",
  12. "white"
  13. ],
  14. "embedding": [-0.7, -0.51, 0.88, 0.14]
  15. }

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

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

  1. 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]}'
  2. "OK"
  3. 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]}'
  4. "OK"

更多内容: Index and search JSON documents

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/590492
推荐阅读
相关标签
  

闽ICP备14008679号