在2010.7.19,sphinx 1.10-beta 正式发布。本人当时做了一些很简单的测试,体验了一下其实时索引及字符串属性,今天因工作需求,再次使用
sphinx,详细的测试了一下这两个功能。并做一些记录,以供日后查询!
warning:这里只是简单的体验记录及发现的一些问题,并非指南。如需操作,请根据自己的环境调试!
1、软件:sphinx-1.10-beta-win32.zip
下载地址:http://www.sphinxsearch.com/downloads/sphinx-1.10-beta-win32.zip
系统:windows xp
2、安装配置
sphinx window下不需要安装,直接配置即可
解压 sphinx-1.10-beta-win32.zip 至 E:sphinxserversphinx-1.10
复制 E:sphinxserversphinx-1.10sphinx-min.conf.in E:sphinxserversphinx-1.10sphinx.conf
编辑sphinx.conf 配置参数
# # Minimal Sphinx configuration sample (clean, simple, functional) # index testrt { type = rt rt_mem_limit = 128M path = E:/sphinxserver/sphinx-1.10/data/testrt //utf-8 编码设置,此处只采用一元切分 charset_type = utf-8 charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F ngram_chars = U+3000..U+2FA1F //实时索引的字段、属性 rt_field = title rt_field = content rt_attr_uint = gid //字符串属性 rt_attr_string = content rt_attr_string = title } indexer { mem_limit = 128M } searchd { listen = 9312 #searchd 端口 # 实时索引监听端口,这些采用mysql 41协议,从0.9.9-rc2开始,sphinx searchd守护进程支持mysql二进制网络协议 # 并可通过标准的mysql api存取数据 listen = 9306:mysql41 log = E:/sphinxserver/sphinx-1.10/data/log/searchd.log #自己在对应目录下建立 data和log目录 query_log = E:/sphinxserver/sphinx-1.10/data/log/query.log read_timeout = 5 max_children = 30 pid_file = E:/sphinxserver/sphinx-1.10/data/log/searchd.pid max_matches = 1000 seamless_rotate = 1 preopen_indexes = 0 unlink_old = 1 workers = threads # for RT to work }
3、测试
开启服务端:sphinx 守护进程 searchd
E:sphinxserversphinx-1.10in>searchd -c ../sphinx.conf
若无错误,运行并等待接收查询
客户端 :
a、在cli上测试:
- C:>mysql -P9306 -hlocalhost ##注意,我的机器直接把mysql的路径注册在系统路径,所以直接使用mysql客户端,和以往的索引进程一样,这里不设置权限,若在生产中,请注意使用防火墙进行权限过滤
- C:>mysql -P9306 -hlocalhost
- Welcome to the MySQL monitor. Commands end with ; or g.
- Your MySQL connection id is 1
- Server version: 1.10-beta (r2420)
- Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
- mysql>
- #插入一条数据
- mysql> insert into testrt(id,gid,title,content) values(1,1,'hello sphinx','this
- is sphinx test record');
- #查询刚才的结果
- mysql> select * from testrt where match('sphinx');
- +------+--------+------+----------------------------+--------------+
- | id | weight | gid | content | title |
- +------+--------+------+----------------------------+--------------+
- | 1 | 2812 | 1 | this is sphinx test record | hello sphinx |
- +------+--------+------+----------------------------+--------------+
- 1 row in set (0.00 sec) #正确命中
- mysql> select * from testrt where match('mysql');
- Empty set (0.00 sec) #没有查询到
- Query OK, 1 row affected (0.00 sec)
###测试证明刚才的实时索引设置正确
b、在程序端(php)上测试:
- <?php
- header('Content-type:text/html;charset=utf-8');
- //这里我使用专门用来测试sphinx的中文数据
- $link = mysql_connect('localhost','root','password');
- mysql_query('set names utf8');
- mysql_select_db('sphinx',$link);
- $sphinxLink = mysql_connect('localhost:9306');
- //测试数据
- //获取real-time当前最大id
- $maxRs = mysql_query('select * from testrt order by id desc limit 1 ');
- $maxVal = mysql_result($maxRs,0,'id');
- $sets = mysql_query('select co_id,co_name,co_do from sph_comp limit 1,10000',$link);
- $i = $maxVal; ##此处必须使用最大值,否则会出现数据插入失败
- ##注意原先可以使用 insert into values (value),(value)的逗号分隔的值方式,但这里防止内存不足,每次都直接插入索引
- while ($row=mysql_fetch_assoc($sets)) {
- $sphinxSql = "insert into testrt(id,gid,title,content) values ($i,{$row['co_id']},'{$row['co_name']}','{$row['co_do']}')";
- $res = mysql_query($sphinxSql,$sphinxLink);
- $i++;
- }
- //搜索测试,针对中文
- $key = '商品信息';
- $sql = "select * from testrt where match('{$key}')";
- $rs = mysql_query($sql,$sphinx);
- while ($row = mysql_fetch_assoc($rs)) {
- print_r($row);
- }
- die;
- ?>
运行后显示结果:
Array
(
[id] => 347
[weight] => 8588
[gid] => 208
[content] => 商品信息咨询服务。
[title] => 广州市越秀区姿营商品信息咨询服务部
)
…
至此整个操作完成