首页 > 数据库 >redisearch入门

redisearch入门

时间:2023-12-26 23:01:11浏览次数:36  
标签:search bicycle 入门 price bike ft redisearch condition

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

参考

rediseach实战

官方文档

标签:search,bicycle,入门,price,bike,ft,redisearch,condition
From: https://blog.51cto.com/happyshare/8988786

相关文章

  • Unreal入门,开关03,利用事件调度器来制作电源总开关
    这里总开关的实现方式是在总开关ActorPowerOff上添加事件调度器PowerOff,其它需要响应这个事件的Actor需要自己初始化相应的事件,并作出合适的处理即可,这里是将PowerOff事件关联上ToggleLight即可实现跟随主开关的开关效果1.创建基于Actor的蓝图类,电源总开关BP_PowerSwitch添......
  • Kotlin从入门到精通,正确的学习路径+学习资料
    前言Kotlin是一种针对Java平台的新编程语言。它简洁、安全、务实,专注于与Java的互操作性,可以很好地与所有现存的Java库和框架一起工作,且性能与Java相当。Kotlin可以用于几乎所有Java使用的地方,如服务端开发、Android应用开发等。如何学习学习Kotlin从入门到精通需要按照一定的步......
  • Linux入门(二)
    Linux入门(二)1:创建文件1.1touch命令使用touch命令在文件不存在时,会创建文件。同时不支持在创建文件时,写入一些内容,该文件的大小是0kb.touchtest.txt注意,无法创建多级目录文件如:touchtest/test.txt在test文件夹不存在时,会提示错误:touch:无法创建'test/test.txt':......
  • Linux 入门
    Linux入门1:linux用户root用户:也叫超级用户,UID=0,其权限最高。系统用户:也叫虚拟用户,UID=1-999普通用户:UID=1000-60000,可以登录系统,操作自己目录下的文件.1.1:用户操作命令切换用户:su用户名;输入密码即可查询用户:logname1.2:top命令:显示进程信息.......
  • xxl_job系列---【xxl_job快速入门教程】
    1.gitee仓库地址(克隆到本地)地址:https://gitee.com/xuxueli0323/xxl-job2.项目结构3.部署xxl-job-admin切换到2.4.0分支3.1修改数据库连接mysql8(默认)3.2修改数据库连接mysql5.71.修改mysql驱动2.修改配置文件4.新springboot项目集成xxl_job4.1.pom中加入下面依赖<?x......
  • P1339 [USACO09OCT] Heat Wave G 最短路入门题 Dijkstra/SPFA/Dijkstra+优先队列优化
    目录朴素的Dijkstra算法SPFA算法Dijkstra+优先队列优化题目链接:https://www.luogu.com.cn/problem/P1339题目大意:无向图有单源最短路。朴素的Dijkstra算法时间复杂度\(O(n^2)\)。#include<bits/stdc++.h>usingnamespacestd;constintmaxn=2505;structEdge......
  • java接口自动化系列(01):自动化测试框架设计(入门版)
     前言想必很多测试小伙伴自动化都是用的python吧?从当前测试招聘要求可以看到,测试开发就是全栈要求,要想在职场有竞争力,就得多个技术方向逐个提升;而和自动化、测开、性能、白盒等都相关的语言就是java,当然,这是基于很多公司项目是java来说的,毕竟Java已经发展了近20年,丰富的周边框架打......
  • 【Flink从入门到精通 05】Source&Sink
    【Flink从入门到精通05】Source&SinkFlink用于处理有状态的流式计算,需要对Source端的数据进行加工处理,然后写入到Sink端,下图展示了在Flink中数据所经历的过程,今天就根据这张图分别给大家分享下。01EnvironmentFlink所有的程序都从这一步开始,只有创建了执行环境,才能开......
  • JDBC快速入门:从环境搭建到代码编写,轻松实现数据库增删改查操作!
    通过上篇文章我们已经对JDBC的基本概念和工作原理都有了一定的了解,本篇文章我们继续来探索如何从零开始,一步步搭建开发环境,编写代码,最后实现数据库的增删改查操作。一、开发环境搭建首先,我们需要准备的开发环境有:Java开发工具包(JDK)、数据库(如MySQL)、数据库驱动(如MySQLConnector/......
  • drf 入门--精通 07days
    视图层总结 视图集ModelViewSet-视图类:GenericAPIView-映射:listcreateretrieveupdatedestroyCreateModelMixin,ListModelMixin,UpdateModelMixin,DestroyModelMixin,RetrieveModelMixin-路由写法变了:ViewSetMixin只要继承它,路由写法变了......