使用python测试sphinx(coreseek)作全文索引

 

coreseek是一个基础sphinx的中文索引工具,我今天写一下关于使用python作测试的方式。python

1.coreseek安装,链接mysql的配置,先启动sphinx进程,./searchedmysql

2.python要链接sphinx确定是须要第三方库的,sql

pip install sphinx

/coreseek-3.2.14/csft-3.2.14/api  找到sphinxapi.py,test.pyapi

 

 

3.测试脚本,python test.py  python工具

cl = SphinxClient()测试

cl.SetServer ( 'localhost', 9312 )  #主机与端口code

cl.SetWeights ( [100, 1] )索引

cl.SetMatchMode (SPH_MATCH_ANY) #查询的匹配模式,这个本身去了解吧,通常用any进程

res = cl.Query ( 'python', '*' )  #前一个是关键字,后一个是索引,*表明全部索引ip

打印print res,看到结果

{'status': 0, 'matches': [{'id': 2, 'weight': 2, 'attrs': {}}, {'id': 4, 'weight': 2, 'attrs': {}}], 'fields': ['title', 'content'], 'time': '0.025', 'total_found': 2, 'warning': '', 'attrs': [], 'words': [{'docs': 2, 'hits': 4, 'word': 'python'}], 'error': '', 'total': 2}

因此res['matches']列表包含了2个字典,遍历一下

for i in res['matches']:

print i['id']

打印出了id,咱们取出了id

相关文章
相关标签/搜索