redisearch
1. 安装
docker 安装
docker run -d --name redisearch -p 8379:6379 redislabs/redisearch
2. 验证
验证search是否安装
docker exec -it redisearch redis-cli
module list
1) 1) "name"
2) "ReJSON"
3) "ver"
4) (integer) 999999
2) 1) "name"
2) "search"
3) "ver"
4) (integer) 20613
ReJSON是json模块
search:搜索模块
3. 添加索引
# 添加索引测试
ft.add myidx doc2 1.0 language "chinese" fields title "Java 14 发布了!新功能速览" desc "Java 14 在 2020.3.17 日发布正式版了,但现在很多公司还在使用 Java 7 或 Java 8"
# 查看索引名
ft.info index(索引名)
# 添加索引
FT.CREATE idx:bicycle ON JSON PREFIX 1 bicycle: SCORE 1.0 SCHEMA $.brand AS brand TEXT WEIGHT 1.0 $.model AS model TEXT WEIGHT 1.0 $.description AS description TEXT WEIGHT 1.0 $.price AS price NUMERIC $.condition AS condition TAG SEPARATOR ,
# 添加数据
JSON.SET "bicycle:0" "." "{\"brand\": \"Velorim\", \"model\": \"Jigger\", \"price\": 270, \"description\": \"Small and powerful, the Jigger is the best ride for the smallest of tikes! This is the tiniest kids\\u2019 pedal bike on the market available without a coaster brake, the Jigger is the vehicle of choice for the rare tenacious little rider raring to go.\", \"condition\": \"new\"}"
JSON.SET "bicycle:1" "." "{\"brand\": \"Bicyk\", \"model\": \"Hillcraft\", \"price\": 1200, \"description\": \"Kids want to ride with as little weight as possible. Especially on an incline! They may be at the age when a 27.5\\\" wheel bike is just too clumsy coming off a 24\\\" bike. The Hillcraft 26 is just the solution they need!\", \"condition\": \"used\"}"
JSON.SET "bicycle:2" "." "{\"brand\": \"Nord\", \"model\": \"Chook air 5\", \"price\": 815, \"description\": \"The Chook Air 5 gives kids aged six years and older a durable and uberlight mountain bike for their first experience on tracks and easy cruising through forests and fields. The lower top tube makes it easy to mount and dismount in any situation, giving your kids greater safety on the trails.\", \"condition\": \"used\"}"
JSON.SET "bicycle:3" "." "{\"brand\": \"Eva\", \"model\": \"Eva 291\", \"price\": 3400, \"description\": \"The sister company to Nord, Eva launched in 2005 as the first and only women-dedicated bicycle brand. Designed by women for women, allEva bikes are optimized for the feminine physique using analytics from a body metrics database. If you like 29ers, try the Eva 291. It\\u2019s a brand new bike for 2022.. This full-suspension, cross-country ride has been designed for velocity. The 291 has 100mm of front and rear travel, a superlight aluminum frame and fast-rolling 29-inch wheels. Yippee!\", \"condition\": \"used\"}"
JSON.SET "bicycle:4" "." "{\"brand\": \"Noka Bikes\", \"model\": \"Kahuna\", \"price\": 3200, \"description\": \"Whether you want to try your hand at XC racing or are looking for a lively trail bike that's just as inspiring on the climbs as it is over rougher ground, the Wilder is one heck of a bike built specifically for short women. Both the frames and components have been tweaked to include a women\\u2019s saddle, different bars and unique colourway.\", \"condition\": \"used\"}"
JSON.SET "bicycle:5" "." "{\"brand\": \"Breakout\", \"model\": \"XBN 2.1 Alloy\", \"price\": 810, \"description\": \"The XBN 2.1 Alloy is our entry-level road bike \\u2013 but that\\u2019s not to say that it\\u2019s a basic machine. With an internal weld aluminium frame, a full carbon fork, and the slick-shifting Claris gears from Shimano\\u2019s, this is a bike which doesn\\u2019t break the bank and delivers craved performance.\", \"condition\": \"new\"}"
JSON.SET "bicycle:6" "." "{\"brand\": \"ScramBikes\", \"model\": \"WattBike\", \"price\": 2300, \"description\": \"The WattBike is the best e-bike for people who still feel young at heart. It has a Bafang 1000W mid-drive system and a 48V 17.5AH Samsung Lithium-Ion battery, allowing you to ride for more than 60 miles on one charge. It\\u2019s great for tackling hilly terrain or if you just fancy a more leisurely ride. With three working modes, you can choose between E-bike, assisted bicycle, and normal bike modes.\", \"condition\": \"new\"}"
JSON.SET "bicycle:7" "." "{\"brand\": \"Peaknetic\", \"model\": \"Secto\", \"price\": 430, \"description\": \"If you struggle with stiff fingers or a kinked neck or back after a few minutes on the road, this lightweight, aluminum bike alleviates those issues and allows you to enjoy the ride. From the ergonomic grips to the lumbar-supporting seat position, the Roll Low-Entry offers incredible comfort. The rear-inclined seat tube facilitates stability by allowing you to put a foot on the ground to balance at a stop, and the low step-over frame makes it accessible for all ability and mobility levels. The saddle is very soft, with a wide back to support your hip joints and a cutout in the center to redistribute that pressure. Rim brakes deliver satisfactory braking control, and the wide tires provide a smooth, stable ride on paved roads and gravel. Rack and fender mounts facilitate setting up the Roll Low-Entry as your preferred commuter, and the BMX-like handlebar offers space for mounting a flashlight, bell, or phone holder.\", \"condition\": \"new\"}"
JSON.SET "bicycle:8" "." "{\"brand\": \"nHill\", \"model\": \"Summit\", \"price\": 1200, \"description\": \"This budget mountain bike from nHill performs well both on bike paths and on the trail. The fork with 100mm of travel absorbs rough terrain. Fat Kenda Booster tires give you grip in corners and on wet trails. The Shimano Tourney drivetrain offered enough gears for finding a comfortable pace to ride uphill, and the Tektro hydraulic disc brakes break smoothly. Whether you want an affordable bike that you can take to work, but also take trail in mountains on the weekends or you\\u2019re just after a stable, comfortable ride for the bike path, the Summit gives a good value for money.\", \"condition\": \"new\"}"
JSON.SET "bicycle:9" "." "{\"model\": \"ThrillCycle\", \"brand\": \"BikeShind\", \"price\": 815, \"description\": \"An artsy, retro-inspired bicycle that\\u2019s as functional as it is pretty: The ThrillCycle steel frame offers a smooth ride. A 9-speed drivetrain has enough gears for coasting in the city, but we wouldn\\u2019t suggest taking it to the mountains. Fenders protect you from mud, and a rear basket lets you transport groceries, flowers and books. The ThrillCycle comes with a limited lifetime warranty, so this little guy will last you long past graduation.\", \"condition\": \"refurbished\"}"
参数说明
create后面 idx:bicycle 是索引名
on JSON: 指定数据格式为json格式 hash则使用 on hash
prefix 1 bicycle: 表示为索引为bicycle:开头的自动加入索引
schema后面为字段名和类型
text weight 1.0:表示权重
4. 查询
# 搜索测试 中文搜索需要添加 language "chinese"
ft.search myidx "发布了" language "chinese"
1) (integer) 1
2) "doc2"
3) 1) "desc"
2) "Java 14 \xe5\x9c\xa8 2020.3.17 \xe6\x97\xa5\xe5\x8f\x91\xe5\xb8\x83\xe6\xad\xa3\xe5\xbc\x8f\xe7\x89\x88\xe4\xba\x86\xef\xbc\x8c\xe4\xbd\x86\xe7\x8e\xb0\xe5\x9c\xa8\xe5\xbe\x88\xe5\xa4\x9a\xe5\x85\xac\xe5\x8f\xb8\xe8\xbf\x98\xe5\x9c\xa8\xe4\xbd\xbf\xe7\x94\xa8 Java 7 \xe6\x88\x96 Java 8"
3) "title"
4) "Java 14 \xe5\x8f\x91\xe5\xb8\x83\xe4\xba\x86\xef\xbc\x81\xe6\x96\xb0\xe5\x8a\x9f\xe8\x83\xbd\xe9\x80\x9f\xe8\xa7\x88"
# 添加条数限制 limit * 通配符
ft.search "idx:bicycle" "*" limit 0 10
# 单个词条查询
ft.search "idx:bicycle" "@model:Jigger" limit 0 10
# 精确查询 查询词需要用双引号引起来
ft.search "idx:bicycle" "@model:\"Jigger\"" limit 0 10
Numberic类型范围查询
# 两种方式 start=end 表示精确查询
ft.search "idx:bicycle" "@price:[1000 1500]"
ft.search "idx:bicycle" "*" filter price 1000 1500
1) (integer) 2
2) "bicycle:1"
3) 1) "$"
2) "{\"brand\":\"Bicyk\",\"model\":\"Hillcraft\",\"price\":1200,\"description\":\"Kids want to ride with as little weight as possible. Especially on an incline! They may be at the age when a 27.5\\\" wheel bike is just too clumsy coming off a 24\\\" bike. The Hillcraft 26 is just the solution they need!\",\"condition\":\"used\"}"
4) "bicycle:8"
5) 1) "$"
2) "{\"brand\":\"nHill\",\"model\":\"Summit\",\"price\":1200,\"description\":\"This budget mountain bike from nHill performs well both on bike paths and on the trail. The fork with 100mm of travel absorbs rough terrain. Fat Kenda Booster tires give you grip in corners and on wet trails. The Shimano Tourney drivetrain offered enough gears for finding a comfortable pace to ride uphill, and the Tektro hydraulic disc brakes break smoothly. Whether you want an affordable bike that you can take to work, but also take trail in mountains on the weekends or you\xe2\x80\x99re just after a stable, comfortable ride for the bike path, the Summit gives a good value for money.\",\"condition\":\"new\"}"
数值类型的范围查询
-
-inf <=> <=
-
inf
-
+inf <=> >=
> 1000
FT.SEARCH idx:bicycle "@price:[(1000 +inf]"
<= 2000 按价格排序 分页 上面的带括号 "(" 括号表示不包含
FT.SEARCH idx:bicycle "@price:[-inf 2000]" sortby price limit 0 5
基于tag字段过滤
tag字段是短text类型,可以当作枚举类型
# 语法
ft.search indexName "@field:{tag}"
# 示例
ft.search "idx:bicycle" "@condition:{new}"
ft.search "idx:bicycle" "@condition:{new}"
1) (integer) 5
2) "bicycle:5"
3) 1) "$"
2) "{\"brand\":\"Breakout\",\"model\":\"XBN 2.1 Alloy\",\"price\":810,\"description\":\"The XBN 2.1 Alloy is our entry-level road bike \xe2\x80\x93 but that\xe2\x80\x99s not to say that it\xe2\x80\x99s a basic machine. With an internal weld aluminium frame, a full carbon fork, and the slick-shifting Claris gears from Shimano\xe2\x80\x99s, this is a bike which doesn\xe2\x80\x99t break the bank and delivers craved performance.\",\"condition\":\"new\"}"
4) "bicycle:0"
5) 1) "$"
2) "{\"brand\":\"Velorim\",\"model\":\"Jigger\",\"price\":270,\"description\":\"Small and powerful, the Jigger is the best ride for the smallest of tikes! This is the tiniest kids\xe2\x80\x99 pedal bike on the market available without a coaster brake, the Jigger is the vehicle of choice for the rare tenacious little rider raring to go.\",\"condition\":\"new\"}"
6) "bicycle:6"
7) 1) "$"
2) "{\"brand\":\"ScramBikes\",\"model\":\"WattBike\",\"price\":2300,\"description\":\"The WattBike is the best e-bike for people who still feel young at heart. It has a Bafang 1000W mid-drive system and a 48V 17.5AH Samsung Lithium-Ion battery, allowing you to ride for more than 60 miles on one charge. It\xe2\x80\x99s great for tackling hilly terrain or if you just fancy a more leisurely ride. With three working modes, you can choose between E-bike, assisted bicycle, and normal bike modes.\",\"condition\":\"new\"}"
8) "bicycle:7"
9) 1) "$"
2) "{\"brand\":\"Peaknetic\",\"model\":\"Secto\",\"price\":430,\"description\":\"If you struggle with stiff fingers or a kinked neck or back after a few minutes on the road, this lightweight, aluminum bike alleviates those issues and allows you to enjoy the ride. From the ergonomic grips to the lumbar-supporting seat position, the Roll Low-Entry offers incredible comfort. The rear-inclined seat tube facilitates stability by allowing you to put a foot on the ground to balance at a stop, and the low step-over frame makes it accessible for all ability and mobility levels. The saddle is very soft, with a wide back to support your hip joints and a cutout in the center to redistribute that pressure. Rim brakes deliver satisfactory braking control, and the wide tires provide a smooth, stable ride on paved roads and gravel. Rack and fender mounts facilitate setting up the Roll Low-Entry as your preferred commuter, and the BMX-like handlebar offers space for mounting a flashlight, bell, or phone holder.\",\"condition\":\"new\"}"
10) "bicycle:8"
11) 1) "$"
2) "{\"brand\":\"nHill\",\"model\":\"Summit\",\"price\":1200,\"description\":\"This budget mountain bike from nHill performs well both on bike paths and on the trail. The fork with 100mm of travel absorbs rough terrain. Fat Kenda Booster tires give you grip in corners and on wet trails. The Shimano Tourney drivetrain offered enough gears for finding a comfortable pace to ride uphill, and the Tektro hydraulic disc brakes break smoothly. Whether you want an affordable bike that you can take to work, but also take trail in mountains on the weekends or you\xe2\x80\x99re just after a stable, comfortable ride for the bike path, the Summit gives a good value for money.\",\"condition\":\"new\"}"
精确查询
# 语法 必须被双引号引起来,不可以使用听用词开头
FT.SEARCH index "@field:\"phrase\""
# 示例
FT.SEARCH idx:bicycle "@description:\"rough terrain\""
1) (integer) 1
2) "bicycle:8"
3) 1) "$"
2) "{\"brand\":\"nHill\",\"model\":\"Summit\",\"price\":1200,\"description\":\"This budget mountain bike from nHill performs well both on bike paths and on the trail. The fork with 100mm of travel absorbs rough terrain. Fat Kenda Booster tires give you grip in corners and on wet trails. The Shimano Tourney drivetrain offered enough gears for finding a comfortable pace to ride uphill, and the Tektro hydraulic disc brakes break smoothly. Whether you want an affordable bike that you can take to work, but also take trail in mountains on the weekends or you\xe2\x80\x99re just after a stable, comfortable ride for the bike path, the Summit gives a good value for money.\",\"condition\":\"new\"}"
1. 直接根据关键词搜索
# 语法
ft.search index "word"
# 示例
FT.SEARCH idx:bicycle "mountain"
2. 限制某个字段包含关键词
# 语法
ft.search index "@field:word"
# 示例
FT.SEARCH idx:bicycle "@description:mountain"
3. 模糊匹配
# 前缀匹配
ft.search index "*word"
# 后缀匹配
ft.search index "word*"
# 限定字段右模糊匹配
ft.search index "@field:word*"
# 全模糊匹配
ft.search index "%word%"
组合查询
1. and 与查询
# 语法
ft.search index "(expr1) (expr2)"
# 示例
FT.SEARCH idx:bicycle "(book) (mountain)"
# 限制字段
# 语法
FT.SEARCH index "@text_field:( value1 value2 ... )"
# 示例
FT.SEARCH idx:bicycle "@description:(book mountain)"
# 多字段查询
# 语法
FT.SEARCH idx:bicycle "@price:[500 1000] @condition:{new}"
#关键词+多字段查询
# 语法 field tag仅仅是示例
FT.SEARCH index "keyword (@field:[500 1000] @tag:{tagName})"
# 示例
FT.SEARCH idx:bicycle "kids (@price:[500 1000] @condition:{new})"
2. or 或查询
# 语法 expr1 可以是 (expr11 expr12)
ft.search index "(expr1) | (expr2)"
# 示例
FT.SEARCH idx:bicycle "(book)| (mountain)"
# 指定字段
ft.search index "@text_field:(expr1 | expr2 |...)"
# 指定标签
ft.search index "@tag_field:{expr1 | expr2 |...}"
# 示例
FT.SEARCH idx:bicycle "@description:(book | mountain)"
可以与上面的与的结果对比,与的结果只有bicycle:9的结果
3. not 非
# 语法
ft.search index "-(expr)"
# 示例
ft.search idx:bicycle "@price:[500 1000] -@condition:{new}"
4. 数字类型过滤
# 语法
FT.SEARCH index "expr" FILTER numeric_field start end
# 示例
ft.search idx:bicycle "mountain" filter price 500 1500
聚合查询
聚合查询使用`fg.aggregate`
聚合查询包括
- 映射函数
- 分组
- 映射函数和分组查询数据
1. 简单映射
# 语法
FT.AGGREGATE index "query_expr" LOAD n "field_1" .. "field_n" APPLY "function_expr" AS "result_field"
# 示例
FT.AGGREGATE idx:bicycle "@condition:{new}" LOAD 2 "__key" "price" APPLY "@price - (@price * 0.1)" AS "discounted"
说明:
query_expr:跟ft.search中的表达式一样
load n: 加载几个字段,如果字段是需要聚合处理(比如计算) 跟在load 字段后面
mapping function: 函数计算,如果没有该字段可以用
as field_result
(别名)
2. 聚合不分组
# 语法
FT.AGGREGATE index "query_expr" ... GROUPBY n "field_1" .. "field_n" REDUCE AGG_FUNC m "@field_param_1" .. "@field_param_m" AS "aggregated_result_field"
# 示例 以下查询显示如何根据字段条件进行分组,并根据以前派生的price_category应用降价。如果价格低于1000美元,表达式@price<1000会导致自行车的价格类别为1。否则,它的价格类别为0。产出是按价格类别分组的平价自行车数量。
FT.AGGREGATE idx:bicycle "*" LOAD 1 price APPLY "@price<1000" AS price_category GROUPBY 1 @condition REDUCE SUM 1 "@price_category" AS "num_affordable"
说明
分组:分组一个或者多个字段,可以根据每个值的有序序列定义一个组,也可以按先前APPLY产生的值进行分组
聚合:必须将AGG_FUNC替换为支持的聚合函数之一(例如SUM或COUNT)。函数的完整列表可在聚合参考文档中找到。将aggregate_result_field替换为您选择的值
复杂的多个分组语法:GROUPBY ... REDUCE ... APPLY ... GROUPBY ... REDUCE
3. 分组不聚合
# 语法
FT.AGGREGATE idx:bicycle "*" LOAD 1 "__key" GROUPBY 1 "@condition" REDUCE TOLIST 1 "__key" AS bicylces
4.聚合函数
# 统计数量
count: reduce count 0
# 根据属性值唯一统计
count_distinct: reduce count_distinct 1 {property}
# 同count_distinct,这里提供一个近似值
count_distinctish: REDUCE COUNT_DISTINCTISH 1 {property}
# 数值类型字段做统计
sum: REDUCE SUM 1 {property}
# 属性最小值
min: reduce min 1 {property}
# 属性最大值
max: reduce max 1 {property}
# 将给定属性的所有不同值合并到单个数组中
tolist: REDUCE TOLIST 1 {property}
# 平均值
avg: reduce avg 1 {property}
#
quantile:
# 返回组中数字属性的标准偏差
stddev: reduce stddev 1 {property}
#
first_value
#
random_sample