OnceDB基于Redis的多条件关系查询,模糊全文搜索使用教程
二次开发 OnceDB by newghost on 1580461937866


OnceDB是基于Redis实现的全文搜索数据库,可以像SQL数据库那样创建辅助索引,提高条件搜索的性能。

OnceDB并不改变Redis的数据存储结构,Redis数据库文件可以直接在OnceDB中操作,然后再返回Redis中使用。

String 字符串

在字符串类型的键中搜索

search pattern operator value

准备测试数据

set test1 'this is testing'
> OK
set test2 'this is article'
> OK
set test3 kris
> OK
set test4 10
> OK
set test5 100
> OK

= : 完全匹配

找到完全匹配的结果,并输出键和值

search test* = kris
1) test3
2) kris

~ : 部分匹配

搜索部分匹配的结果

search test* ~ is
1) test2
2) this is article
3) test1
4) this is testing
5) test3
6) kris

<、>、<=、>= : 比较搜索

找到符合条件的数字

search test* > 20
1) test5
2) 100

HASH 哈希

hsearch pattern field operator value ...

在hash类型的健中搜索,所有条件需同时满足。

准备测试数据

hmset user:001 name kris email c2u@live.cn gender male age 16
> OK
hmset user:002 name sunny age 24
> OK

搜索age大于18的key和搜索到的值,并打印name字段

hsearch user:* age > 18 name = *
1) user:002
2) 24
3) sunny

horsearch pattern field operator value ...

搜索hash类型的健值,除 "field = *" 外,满足一个条件即可。

horsearch user:* age > 18 gender = male name = *
1) user:002
2) 24
3) 
4) sunny
5) user:001
6) 16
7) male
8) kris

hsearchids pattern [num of ids] id1 id2 ... field operator value ...

在指定ID中进行搜索

hsearchids user: 2 001 002 age > 18 name = *
1) user:002
2) 24
3) sunny

hselect [num of fields] field1 field2 ... key1 key2 ...

打印出指定键的指定字段值。

hselect 3 name email age user:001 user:002
1) user:001
2) kris
3) c2u@live.cn
4) 16
5) user:002
6) sunny
7) 
8) 24

hmgetall key1 key2 ...

打印出指定键的全部字段和值,每个键以连续两个空数据分隔

hmgetall user:001 user:002
1) user:001
2) name
3) kris
4) email
5) c2u@live.cn
6) gender
7) male
8) age
9) 16
10) 
11) 
12) user:002
13) name
14) sunny
15) age
16) 24
17) 
18) 

hsetzsetkeys hashpattern hashid field keys zsetpattern score

** Deprecated **

设置HASH键的关键字,并创建索引

hsetzsetkeys user: 001 skills java,php skill: 10001
1) 2

等效于

zadd skill:java 10001 001
zadd skill:php  10001 001
hset user:001 skills java,php

hincrby key field increment [start]

增加 key 指定的哈希集中指定字段的数值, start为默认值,如果原值存在,则start不起作用:

hincrby article:00test show 1
> 1
hincrby article:00test show 1 1000
> 2
hincrby article:00test visit 1 1000
> 1001

List 有序数列

lxrange key from to

从一个有序列表中查找出范围内的值

lpush records 10 11 20 34 54 84 130 210
> 8

lxrange records 12 60
1) 54
2) 34
3) 20

lxsearch key value

从一个有序列表中查找某值的位置

lxsearch records 54
> 3

lxinsert key value

创建一个小到大序列,返回新位置信息

lxinsert order 9
> 0
lxinsert order 5
> 0
lxinsert order 13
> 2
lxinsert order 7
> 1



lxrange order 0 10000
1) 5
2) 7
3) 9
4) 13

lxinsertdesc key value

将数字插入从大到小序列,返回新位置信息

lxinsertdesc numbers 9
> 0
lxinsertdesc numbers 5
> 1
lxinsertdesc numbers 13
> 0
lxinsertdesc numbers 7
> 2

lxrange numbers 0 10000
1) 13
2) 9
3) 7
4) 5

Sorted Set 有序集合

zexists key member1 member2 ...

获取sorts set中存在key的数量

zexists tags zbc efg hi
> 0

zadd tags 1000 zbc 2000 efg 3000 hi
> 3

zexists tags zbc efg hi
> 3

zrangehmget key from to schema field1 ...

获取zset中对应HASH键的对应对应字段值。

准备测试数据

upsert article id @=1000 002 name = dota age = 30 num = 28
upsert article id @=2000 003 name = like age = 40 num = 32
upsert article id @=3000 004 name = good age = 50 num = 34

*article 存放文章ID

zrange *article 0 -1 WITHSCORES
1) 002
2) 1000
3) 003
4) 2000
5) 004
6) 3000

article中的key全部取出,并输出所有 article: 的键和值

zrangehmget *article 0 -1 article:
1) 002              [member in zset]
2) 1000             [score in zset]
3) id               [field]
4) 002              [value]
5) name
6) dota
7) age
8) 30
9) num
10) 28
11) 
12) 
13) 003
14) 2000
15) id
16) 003
17) name
18) like
19) age
20) 40
21) num
22) 32
23) 
24) 
25) 004
26) 3000
27) id
28) 004
29) name
30) good
31) age
32) 50
33) num
34) 34
35) 
36) 

输出 article:* 的 name 和 age 字段值

zrangehmget *article 1 2 article: name age
1) 003
2) 2000
3) like
4) 40
5) 
6) 
7) 004
8) 3000
9) good
10) 50
11) 
12) 

zrevrangehmget key from to schema field1 ...

倒序打印,zset中成对应的HASH

zrevrangehmget *article 1 2 article: name age
1) 003
2) 2000
3) like
4) 40
5) 
6) 
7) 002
8) 1000
9) dota
10) 30
11) 
12)

zrangehmgetbyscore key from to schema field1 ...

按权重从指定zset字段输出对应的HASH

zrangehmgetbyscore *article 1500 2200 article:
1) 003      [number in zset]
2) 2000     [score]
3) id       [field]
4) 003      [value]
5) name
6) like
7) age
8) 40
9) num
10) 32
11) 
12) 

输出指定字段:

zrangehmgetbyscore *article 1500 2200 article: name
1) 003
2) 2000
3) like
4) 
5) 

zrevrangehmgetbyscore key from to schema field1 ...

倒序输出

zrevrangehmgetbyscore *article 2200 1500 article: name
1) 003
2) 2000
3) like
4) 
5) 

zrangehmsum key from to schema field1 ...

从有序列表,获取对应hash键,求字段值的和

zrangehmsum *article 0 10 article: age num
1) 3        [the number of elements in zset]
2) 3        [the number of elements exist in article:*]
3) 3        [count of age]
4) 120      [sums of age]
5) 3
6) 94

zrevrangehmsum key from to schema field1 ...

按有序列表倒序,求对应HASH键值的和

zrevrangehmsum *article 0 1 article: age num
1) 2
2) 2
3) 2
4) 90
5) 2
6) 66

zrangehmsumbyscore key score1 score2 schema field1 ...

按分值从range中获取key,查找对应hash,求字段值的和

zrangehmsumbyscore *article 1500 2200 article: age num
1) 1      [the number of elements in zset]
2) 1      [the umber of elements exists in article:*]
3) 1      [count of age]
4) 40     [sums of age]
5) 1
6) 32

zrevrangehmsumbyscore key score1 score2 schema field1 ...

倒序求和

zrevrangehmsumbyscore *article 2200 1500 article: age num
1) 1
2) 1
3) 1
4) 40
5) 1
6) 32

zhsearch key from to schema field operator value ...

从zset中搜索对应的hash

upsert blog id @ 001 title = "God like" poster = dota       visit = 10
upsert blog id @ 002 title = "First blood" poster = dota    visit = 200
upsert blog id @ 003 title = "test title" poster = like     visit = 40


zhsearch *blog 0 100 blog: visit > 28 poster = *
1) -1
2) blog:002
3) 200
4) dota
5) blog:003
6) 40
7) like

zrevhsearch key from to schema field operator value ...

倒序查找

zrevhsearch *blog 0 100 blog: visit > 28 poster = *
1) -1
2) blog:003
3) 40
4) like
5) blog:002
6) 200
7) dota

zhorsearch zsetkey from to schema field operator value ...

或条件查找

zhorsearch *blog 0 100 blog: visit > 40 poster = like
1) -1
2) blog:002
3) 200
4) dota
5) blog:003
6) 40
7) like

zrevhorsearch zsetkey from to schema field operator value ...

倒序或条件查找

zrevhorsearch *blog 0 100 blog: visit > 40 poster = like
1) -1
2) blog:003
3) 40
4) like
5) blog:002
6) 200
7) dota

Set 集合

shmget key schema field1 field2 ...

按 SET 成员输出 HASH 字段的值

hset blog:001 name dota
> 1
hset blog:002 name kris
> 1
sadd blogs 001 002
> 2


shmget blogs blog: name
1) 002
2) kris
3) 
4) 
5) 001
6) dota
7) 
8) 

shmset key schema field value ...

** Deprecated **

从 set 提取所有成员,设置对应 hash 成员的值,返回受影响的hash值数量

shmset blogs blog: home China tel 021-7000000
> 2

结果:

hmgetall blog:001 blog:002
1) blog:001
2) name
3) dota
4) home
5) China
6) tel
7) 021-7000000
8) 
9) 
10) blog:002
11) name
12) kris
13) home
14) China
15) tel
16) 021-7000000
17) 
18) 

shmsum setkey schema field ...

求 SET 对应 hash 成员中指定字段的和

hmset job:001 salary 1000 worktime 6
> ok
hmset job:002 salary 2000 worktime 10
> ok
sadd jobs 001 002 
> 2


shmsum jobs job: salary worktime
1) 2        [the number of elements in zset]
2) 2        [the number of elements exist in article:*]
3) 2        [counts]
4) 3000     [sums]
5) 2        ...
6) 16       ...

BUG, crashed:

Field weight (sort by)

Use "+" or "-" sign to specify which weight score to use, similar to SORT BY

+: Use the score of field as range and ignore others

find article 0 99 id = * title = * poster ?+ kris public ? 1 keys * c,node.js

-: Do not use the score of field as range

find article @20190801 @20190801 id = * title = * poster ?- kris public ? 1 keys * c,node.js