python中search⽤法_Redisearch快速教程(附python⽤法)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代码:
ate_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指令:
< doc1
Python代码:
data = ('doc1')
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
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_indexmit()
然后是⼀段完整的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)
ate_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')
exists的用法# 删除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_indexmit()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。