2023年7月10日发(作者:)
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、nameRedis指令: first_index SCHEMA title TEXT name TEXTPython代码:index__index((TextField('title'), TextField('name')))如果index已经存在,重复创建则报错:(error) Index already exists⼆、向index中插⼊document:Redis指令: first_index doc1 1.0 FIELDS title "123" name "123"Python代码:# Addindex__document('doc1', title='123', name='234')# Update:index__document('doc1', title='123', name='234', replace=True)如果document已存在,重复插⼊报错(error) Document already exists可通过添加REPLACE覆盖之前的document,类似update的操作 first_index doc1 1.0 REPLACE FIELDS title "1234" name "1234"三、查询1、获取:利⽤document_id直接获取documentRedis指令: doc1Python代码:data = index_('doc1')2、条件查询:a、查询所有:类似select * from first_index: first_index *b、条件查询:与:将条件并列即可,如 '@title:123 @name:11'或:使⽤'|'链接条件,如 '@title:123|@name:11'⾮:在条件之前放置 '-',如 '-@title:123' first_index "@title:123"Python代码:# 查询所有data = index_("*")# 条件查询from redisearch import Queryquery_filter = "@title:123"query = Query(query_filter)data = index_(query)四、删除:1、删除document:Redis指令: first_index doc1Python代码:index__document('doc1')2、删除indecent :相当于drop table,删除表Redis指令: first_indexPython代码:index__index()五、查看index详细信息: first_index结果如下:1) index_name2) first_index3) index_options4) (empty list or set)5) fields6) 1) 1) title2) type3) TEXT4) WEIGHT5) "1"2) 1) name2) type3) TEXT4) WEIGHT5) "1"7) num_docs8) "1"9) max_doc_id10) "2"11) num_terms12) "2"13) num_records14) "1"15) inverted_sz_mb16) "6.67572021484375e-06"17) total_inverted_index_blocks18) "726"19) offset_vectors_sz_mb20) "3.8e-06"21) doc_table_size_mb22) "0.421875"23) sortable_values_六、python中redisearch-BatchIndex的使⽤,本质就是批处理,在redisearch中使⽤pipeline,以达到节省时间的⽬的。batch_index = ndexer(index_client)batch__document('doc5', id='111', name='11')batch__document('doc6', id='222', name='22')batch__document('doc7', id='333', name='33')data = batch_()然后是⼀段完整的Python代码:from redis import ConnectionPool, StrictRedisfrom redisearch import Client, TextField, Queryhost = "127.0.0.1" # redis 服务器port = "50000" # 端⼝pool = ConnectionPool(host=host, port=port)redis = StrictRedis(connection_pool=pool)# Create indexindex_client = Client(index_name='first_index', conn=redis)index__index((TextField('title'), TextField('name')))# Addindex__document('doc1', title='123', name='234')# Update:index__document('doc1', title='123', name='234', replace=True)# 查询所有data = index_("*")# 条件查询query_filter = "@title:123"query = Query(query_filter)data = index_(query)# 删除documentindex__document('doc1')# 删除indexindex__index()# BatchIndex (pipeline的使⽤)batch_index = ndexer(index_client)batch__document('doc5', id='111', name='11')batch__document('doc6', id='222', name='22')batch__document('doc7', id='333', name='33')data = batch_()
发布者:admin,转转请注明出处:http://www.yc00.com/news/1688932590a184973.html
评论列表(0条)