coreseek是一个基础sphinx的中文索引工具,我今天写一下关于使用python做测试的方式。
1.coreseek安装,连接mysql的配置,先启动sphinx进程,./searched
2.python要连接sphinx肯定是需要第三方库的,
pip install sphinx
/coreseek-3.2.14/csft-3.2.14/api 找到sphinxapi.py,test.py
3.测试脚本,python test.py python
cl = SphinxClient()
cl.SetServer ( 'localhost', 9312 ) #主机与端口
cl.SetWeights ( [100, 1] )
cl.SetMatchMode (SPH_MATCH_ANY) #查询的匹配模式,这个自己去了解吧,一般用any
res = cl.Query ( 'python', '*' ) #前一个是关键字,后一个是索引,*代表所有索引
打印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