PHP 简单调用 RediSearch
Github:https://github.com/RediSearch/RediSearch
官方文档:https://redis.io/docs/stack/search/quick_start/
1. 安装 Docker
2. 获取镜像
redislabs/redisearch
3. 创建容器
4. 连接,172.17.0.2 是容器ip
redis-cli -h 172.17.0.2 -p 6379
查看 RediSearch 是否加载成功
module list
ok
5. 创建索引
ft.create archive on hash prefix 1 arc: language chinese schema title text weight 5.0 desc text time
创建 archive 索引包含两个字段 title 和 desc ,weight 为权重,默认值为 1.0
// ft.create 创建索引
// archive 索引名称
// on hash 索引数据基于 hash 类型源数据构建
// prefix 1 arc: 会自动将以 arc: 前缀开头的 key 添加至索引, arc:1, arc:2 ...
// language chinese 支持中文分词
// schema 字段定义,title 属性名 text 字段类型
6. 给索引添加一条数据
hset arc:1 title "村级债务已达9000亿" desc "2023年全国两会期间,民盟中央提交的提案显示..."
arc: 前缀会自动添加至索引
7. 搜索
ft.search archive "村级债务" language "chinese"
需要指定 language "chinese" 不然查不出来
8. 删除数据
ft.del archive arc:1
9. 查看索引相关信息
ft.info archive
10. 删除索引
ft.drop archive
11. 官方列出的 RediSearch API 的库
Project | Language | License | Author | Stars | Comment |
---|---|---|---|---|---|
jedis | Java | MIT | Redis | ||
redis-py | Python | MIT | Redis | ||
node-redis | Node.js | MIT | Redis | ||
nredisstack | .NET | MIT | Redis | ||
redis-om | Python | BSD | Redis | ||
lettusearch | Java | Apache 2.0 | Redis | ||
spring-redisearch | Java | Apache 2.0 | Redis | ||
redis-om-spring | Java | BSD | Redis | ||
redisearch-go | Go | BSD | Redis | ||
rueidis | Go | Apache 2.0 | Rueian | ||
Redis-om | JavaScript | BSD | Redis | ||
Redis.OM | .NET | BSD | Redis | ||
redisearch-php | PHP | MIT | Ethan Hann | ||
php-redisearch | PHP | MIT MacFJA | MIT | ||
redisearch-api-rs | Rust | BSD | Redis | ||
redi_search_rails | Ruby | MIT | Dmitry Polyakovsky | ||
redisearch-rb | Ruby | MIT | Victor Ruiz | ||
redi_search | Ruby | MIT | Nick Pezza |
12. PHP测试一下
redisearch-php 只支持老版本
安装 php-redisearch
composer require macfja/redisearch
测试文件
<?php require __DIR__ . '/vendor/autoload.php'; use MacFJA\RediSearch\Redis\Client\ClientFacade; use MacFJA\RediSearch\IndexBuilder; use MacFJA\RediSearch\Index; use MacFJA\RediSearch\Redis\Command\Search; // 获取 Redis 客户端 $redis = new \Redis(); $redis->connect('172.17.0.2', 6379, 0); $clientFacade = new ClientFacade(); $client = $clientFacade->getClient($redis); // 创建索引 archive $builder = new IndexBuilder(); $builder ->setIndex('archive') ->setDefaultLanguage('chinese') ->addTextField('title') ->addTextField('desc') ->addNumericField('time', true) // true 支持排序, 如果是 addTextField 第5个参数才是排序 ->create($client); // 添加数据 // addDocumentFromArray([...], key) key 是数据存入 redis 的键值 $index = new Index('archive', $client); $index->addDocumentFromArray([ 'title' => '标题 村级债务已达9000亿', 'desc' => '2023年全国两会期间,民盟中央提交的提案显示...', 'time' => '1678291200', ], 'arc:1'); $index->addDocumentFromArray([ 'title' => '标题 卫星视角下的中国大桥有多震撼', 'desc' => '世界第一高桥北盘江第一桥、世界最大岛陆联络工程舟山跨海大桥、跨越赤水河峡谷的赤水河红军大桥...', 'time' => '1678292200', ], 'arc:2'); // 搜索 $search = new Search(); $search->setLanguage('chinese'); $search->setIndex('archive'); $search->setQuery('标题'); $search->setLimit(0, 10); $search->setSortBy('time', 'DESC'); // DESC 区分大小写 $results = $client->execute($search); $archive = []; foreach ($results->current() as $item) { $archive[] = $item->getFields(); } var_dump($archive);
RediSearch 中文查询准确率不高
https://www.jianshu.com/p/8c7fc78a6f57
https://juejin.cn/post/7193944505614073915