OnceDB:使用Redis全文字符串模糊搜索,多條件查詢,創建索引搜索等使用幫助教程
OnceDB 是基於Redis實現的全文搜索數據庫,可以像SQL數據庫那樣創建輔助索引,提高條件搜索的性能。
OnceDB並不改變Redis的數據存儲結構,Redis數據庫文件可以直接在OnceDB中操作,然後再返回Redis中使用。
項目地址: http://oncedb.com
全文搜索
OnceDB提供直接搜索指令,支持 String 和 Hash 等對象,
搜索String: search pattern operator value
搜索 String 類型的鍵,可使用 search
# 創建數據 set test1 'this is testing' > OK set test2 'this is article' > OK # 搜索數據 search test* ~ article 1) test1 2) this is testing 3) test2 4) this is article
operator操作符支持: = 完全匹配 ~ 部分匹配 > >= < <= 比較匹配
搜索Hash: hsearch pattern field operator value ...
hsearch 可以實現對 Hash 對象的搜索。
在redis中沒有表的概念,一般使用 schema:id 的鍵名包含表名和主鍵。比如 user:001 爲 user 表,主鍵值爲001的 Hash 對象。
# 創建 Hash 數據 hmset user:001 name kris email [email protected] gender male age 16 > OK hmset user:002 name sunny age 24 > OK # 搜索 Hash 數據 hsearch user:* age > 18 name = * 1) user:002 2) 24 3) sunny
搜索ZSET: zhsearch key from to schema field operator value ...
直接使用 test* 或 use:* 模式匹配,因爲會遍歷所有的鍵,性能低下。zhsearch可以在有序列表中搜索對應主鍵,並指定搜索範圍。
在上例中,我們將 user 的主鍵值存放到 *user 有序列表中, 分數爲一個關於時間的整數
zadd *user 20020201 001 20020202 002
搜索第一條匹配的數據(從0到0)
zhsearch *user 0 0 user: age > 10 1) -1 2) user:001 3) 16
使用全文搜索時,第一個返回的參數始終是 -1。
索引搜索
搜索原理
全文搜索的性能比較差,可通過創建索引的辦法提高性能,辦法是爲索引字段創建一個有序列表,然後在條件查詢時,對這些有序列表做交集查詢操作。
# 創建4條 Hash 數據 hmset article:001 poster dota visit 21 key js hmset article:002 poster dota visit 11 key c hmset article:003 poster like visit 34 key js hmset article:004 poster like visit 44 key c
然後我們爲上面的字段創建索引,權重分數設爲: 202000201,一個關於時間的整數,值爲article的ID值
# 維護索引 zadd *article.poster:dota 20200201 001 20200201 002 zadd *article.poster:like 20200201 003 20200201 004 zadd *article.key:js 20200201 001 20200201 003 zadd *article.key:c 20200201 002 20200201 004 # visit 的索引直接使用其值爲權重分數 zadd *article.visit 21 001 11 002 34 003 44 004
按索引查詢
求 *article.key:js 和 *article.poster:dota 兩個索引的交集,並存放在 *tmp1 有序列表中:
zinterstore *tmp1 2 *article.key:js *article.poster:dota > 1
然後 *tmp1 存放的就是 滿足 key = js 和 poster = dota 條件的 ID集合:
zrange *tmp1 0 -1 > 001
可使用zrangehmget指令打印相應的HASH值:
zrangehmget *tmp1 0 -1 article: key poster 1) 001 2) 40400402 3) js 4) dota 5) 6)
其結果與直接全文搜索 key = js 和 poster = dota 的搜索結果相同
hsearch article:* key = js poster = dota 1) article:001 2) js 3) dota
搜索範圍
比如要搜索 visit 數量在 20 到 30 之間,key = js 的數據,可通過控制權重的方法實現
創建臨時索引,只取 *article.visit 的權重 和 key = js 的數據
zinterstore *tmp2 2 *article.key:js *article.visit weights 0 1 > 2
取 20 ~ 30 之間的數據
zrangebyscore *tmp2 20 30 > 001
可使用 zrangehmgetbyscore 打印出對應的hash數據:
zrangehmgetbyscore *tmp2 20 30 article: key visit 1) 001 2) 21 3) js 4) 21 5) 6)
其結果與使用全文搜索的結果一致:
hsearch article:* visit >= 20 visit <= 30 key = js 1) article:001 2) 21 3) 4) js
因爲裏面有兩個相同的字段,visit >= 20 visit <= 30,搜索結果只會輸出一個,第3行重複的字段會輸出空。
OnceDB更多擴展指令可查看: OnceDB 搜索、查詢、計算、求和指令
自動索引
Redis索引的創建和維護並不十分方便,OnceDB 在數據修改時可選擇自動創建輔助索引。
創建索引:upsert schema field operator value ...
使用 upsert/insert/update 指令和特殊的操作符可自動創建索引:
如上文的例子可寫成:
upsert article id @ 001 poster ? dota visit / 21 key ? js upsert article id @ 002 poster ? dota visit / 11 key ? c upsert article id @ 003 poster ? like visit / 34 key ? js upsert article id @ 004 poster ? like visit / 44 key ? c
操作符:
@ : 主鍵 ? : 分組索引 / : 排序索引
操作後會自動創建: *article *article.poster:dota *article.poster:like *article.visit *article.key:js *article.key:c 等索引。
多條件索引查詢:find schema from to field operator value ...
含有索引的字段,可使用 find 命令通過索引字段查詢出來,比如查詢:key = js 和 poster = dota 的數據,可通過 "?" 指明這兩個字段是分組索引:
find article 0 -1 key ? js poster ? dota 1) 1 2) article:001 3) js 4) dota
1: 代表符合條件的數據總數,如果是 -1 則代表使用了全文搜索,性能較差。
索引範圍查詢
可添加 @ 指定索引範圍,並使用 + 指定使用哪個索引字段的分數權重範圍。
find article 0@20 -1@30 key ? js visit /+ * 1) 1 2) article:001 3) js 4) 21
刪除自動索引
OnceDB不存儲索引定義,刪除時需要手動指出哪些字段含有索引,需要指定字段名和索引操作符即可。
remove article @ 001 key ? poster ? visit /
還可以自定義索引名稱,權重分數,更多說明可查看: OnceDB數據修改和查詢幫助文檔