Redis是一个key-value的存储系统,在Redis 4.0时引入了一种扩展机制:Modules,使得用户可以通过redis module提供的api接口来定制化功能,Redisearch应运而生。
Redisearch通过全文搜索索引为原始的key-value数据添加数据结构,使得定向过滤数据的速度更快,更方便。
Redisearch引入了几个新的概念,如index:可以理解为关系型数据库中的数据表
document:数据表中的数据
fields:索引字段,类似于数据表中的列,可用于检索
了解了以上概念后,再看redisearch的操作就非常简单。
一、创建Index:
创建index:first_index ,并带有两个field(索引字段):title、name
Redis指令:
FT.CREATE first_index SCHEMA title TEXT name TEXT
Python代码:
index_client.create_index((TextField('title'), TextField('name')))
如果index已经存在,重复创建则报错:
(error) Index already exists
二、向index中插入document:
Redis指令:
FT.ADD first_index doc1 1.0 FIELDS title "123" name "123"
Python代码:
# Add
index_client.add_document('doc1', title='123', name='234')
# Update:
index_client.add_document('doc1', title='123', name='234', replace=True)
如果document已存在,重复插入报错
(error) Document already exists
可通过添加REPLACE覆盖之前的document,类似update的操作
FT.ADD first_index doc1 1.0 REPLACE FIELDS title "1234" name "1234"
三、查询
1、获取:利用document_id直接获取document
Redis指令:
ft.get doc1
Python代码:
data = index_client.get('doc1')
2、条件查询:
a、查询所有:类似select * from first_index:
ft.search first_index *
b、条件查询:与:将条件并列即可,如 '@title:123 @name:11'
或:使用'|'链接条件,如 '@title:123|@name:11'
非:在条件之前放置 '-',如 '-@title:123'
ft.search first_index "@title:123"
Python代码:
# 查询所有
data = index_client.search("*")
# 条件查询
from redisearch import Query
query_filter = "@title:123"
query = Query(query_filter)
data = index_client.search(query)
四、删除:
1、删除document:
Redis指令:
ft.del first_index doc1
Python代码:
index_client.delete_document('doc1')
2、删除indecent :相当于drop table,删除表
Redis指令:
ft.drop first_index
Python代码:
index_client.drop_index()
五、查看index详细信息:
ft.info first_index
结果如下:
1) index_name
2) first_index
3) index_options
4) (empty list or set)
5) fields
6) 1) 1) title
2) type
3) TEXT
4) WEIGHT
5) "1"
2) 1) name
2) type
3) TEXT
4) WEIGHT
5) "1"
7) num_docs
8) "1"
9) max_doc_id
10) "2"
11) num_terms
12) "2"
13) num_records
14) "1"
15) inverted_sz_mb
16) "6.67572021484375e-06"
17) total_inverted_index_blocks
18) "726"
19) offset_vectors_sz_mb
20) "3.814697265625e-06"
21) doc_table_size_mb
22) "0.000164031982421875"
23) sortable_values_size_mb
...
六、python中redisearch-BatchIndex的使用,本质就是批处理,在redisearch中使用pipeline,以达到节省时间的目的。
batch_index = Client.BatchIndexer(index_client)
batch_index.add_document('doc5', id='111', name='11')
batch_index.add_document('doc6', id='222', name='22')
batch_index.add_document('doc7', id='333', name='33')
data = batch_index.commit()
然后是一段完整的Python代码:
from redis import ConnectionPool, StrictRedis
from redisearch import Client, TextField, Query
host = "127.0.0.1" # redis 服务器
port = "50000" # 端口
pool = ConnectionPool(host=host, port=port)
redis = StrictRedis(connection_pool=pool)
# Create index
index_client = Client(index_name='first_index', conn=redis)
index_client.create_index((TextField('title'), TextField('name')))
# Add
index_client.add_document('doc1', title='123', name='234')
# Update:
index_client.add_document('doc1', title='123', name='234', replace=True)
# 查询所有
data = index_client.search("*")
# 条件查询
query_filter = "@title:123"
query = Query(query_filter)
data = index_client.search(query)
# 删除document
index_client.delete_document('doc1')
# 删除index
index_client.drop_index()
# BatchIndex (pipeline的使用)
batch_index = Client.BatchIndexer(index_client)
batch_index.add_document('doc5', id='111', name='11')
batch_index.add_document('doc6', id='222', name='22')
batch_index.add_document('doc7', id='333', name='33')
data = batch_index.commit()