记下昨天跟今天干的活儿~昨天没写日记,都有点儿忘了。想想昨天都干了些啥捏。还是想到哪儿写到哪儿吧。
先说说今天弄coreseek分词以及建立主索引跟增量索引的情况。
上次装coreseek好像没提到安装词库。
今天依旧用的mysql数据源,不过换了个数据库,用了那个日志表,省得自己造记录。
配置文件,生成的索引,log,词典,以及后面自动建立索引要用到的脚本,放的位置太乱了,所以今天统一了一下。
#mkdir -p /data/coreseek
然后在coreseek目录下建立conf,index,log,dict,sh目录。
首先生成词典,就是将源码包中的文本文件,mmseg后生成词典文件uni.lib,然后在配置文件中将词典文件的路径指向这里就行了。
[root@localhost ~]# cd /data/coreseek/dict/
[root@localhost dict]# cp /opt/mmseg-3.1/data/unigram.txt /data/coreseek/dict/
[root@localhost dict]# /usr/local/mmseg/bin/mmseg -u unigram.txt
[root@localhost dict]# mv unigram.txt.uni uni.lib
下面是配置文件
/data/coreseek/conf/post.conf
#
# Minimal Sphinx configuration sample (clean, simple, functional)
#
source post_main
{
type = mysql
sql_host = hostname
sql_user = userame
sql_pass = password
sql_db = Post
sql_port = 3306 # optional, default is 3306
sql_query_pre = SET NAMES utf8
sql_query_pre = REPLACE INTO sph_counter SELECT 1, MAX(PostId) FROM Post
sql_query = \
SELECT PostId, UserId, PostCateId, Title, Content, UNIX_TIMESTAMP(PubTime) AS PubTime \
FROM Post \
WHERE PostId <= (SELECT max_doc_id FROM sph_counter WHERE counter_id = 1)
sql_attr_uint = UserId
sql_attr_uint = PostCateId
sql_attr_timestamp = PubTime
}
source post_delta:post_main
{
sql_query_pre = SET NAMES utf8
sql_query = \
SELECT PostId, UserId, PostCateId, Title, Content, UNIX_TIMESTAMP(PubTime) AS PubTime \
FROM Post \
WHERE PostId > (SELECT max_doc_id FROM sph_counter WHERE counter_id = 1)
}
index post_main
{
source = post_main
path = /data/coreseek/index/post_main
docinfo = extern
charset_type = zh_cn.utf-8
charset_dictpath = /data/coreseek/dict
}
index post_delta:post_main
{
source = post_delta
path = /data/coreseek/index/post_delta
}
indexer
{
mem_limit = 32M
}
searchd
{
log = /data/coreseek/log/searchd.log
query_log = /data/coreseek/log/query.log
read_timeout = 5
max_children = 30
pid_file = /data/coreseek/log/searchd.pid
max_matches = 1000
seamless_rotate = 1
preopen_indexes = 0
unlink_old = 1
}
建立索引,说下索引建立与更新机制吧。增量索引定时更新,比如我这里是1分钟建立一次,查询的时候,同时从两个索引中查询。然后凌晨的时候重建一次索引,所有记录会被添加到主索引,增量索引也会被清空v,这个可以看我后面的测试用例。【刚刚龙兄问我为什么不是合并,后来我想也是,用到这个的,一般数据量不会小,合并索引相对重建更高效一些,不过合并的话,增量索引是如何处理的呢?这个,明天再来解决。】
#/usr/local/coreseek/bin/indexer –config /data/coreseek/conf/post.conf –all
#/usr/local/coreseek/bin/searchd –config /data/coreseek/conf/post.conf
#/usr/local/coreseek/bin/search -q –config /data/coreseek/conf/post.conf
上面这个可以查看到索引,嘿嘿,这是神仙告诉我的。
在这里我还碰到了一个问题,cannot open /data/coreseek/dict/mmseg.ini
解决办法
vi /data/coreseek/dict/mmseg.ini,输入下面内容
[mmseg]
merge_number_and_ascii=1;
number_and_ascii_joint=-;
compress_space=0;
seperate_number_ascii=1;
以上解释如下
/*
merge_number_and_ascii: 字母和数字连续出现是非切分
number_and_ascii_joint:连接数字和字母可用的符号,如’-’ ‘.’ 等
compress_space:暂时无效
seperate_number_ascii:是否拆分数字,如 1988 -> 1/x 9/x 8/x 8/x
*/
这个在网上随处可见,但是,很多人是开启守护进程searchd出现问题,我倒是在search时出现了问题。
下面写更新索引的脚本,先是增量索引:
#vi /data/coreseek/sh/build_delta_index.sh
#!/bin/sh
/usr/local/coreseek/bin/indexer post_delta –config /data/coreseek/conf/post.conf –rotate >> /var/log/coreseek/deltaindexlog
更新增量索引,加上–rotate,不用停止守护进程更新索引。
更新索引:
#!/bin/sh
/usr/local/coreseek/bin/indexer –rotate –all –config /data/coreseek/conf/post.conf >> /var/log/coreseek/mainindexlog
下面建立crontab规则定时更新索引。
#service crond start
#crontab -e
*/1 * * * * /bin/sh /data/coreseek/sh/build_delta_index.sh > /dev/null
0 0 * * * /bin/sh /data/coreseek/sh/build_main_index.sh > /dev/null
下面是简单的测试用例,用了api
<?php
header(“Content-type:text/html;charset=utf-8″);
include ’sphinxapi.php’;
$s = new SphinxClient();
$s->SetServer(‘localhost’, 3312);
$r = $s->Query(‘日记’,'post_main, post_delta’);
echo ‘<pre>’;
if($r)
print_r($r);
else
echo $s->GetLastError();
?>
在浏览器中打开,显示查询到的数据。然后添加记录,记录中包括“日记”,一分钟之内,将会看到增加的记录。
说说昨天跟今天,让我有些无奈的两件事吧。
昨天的测试用例,加入中文后,中文依旧没有搜索出来,今天用search -q查看,也只有四条索引,建立索引时明明看到 6 docs,后来在看配置文件时发现,sql_query_pre=utf8,前面的注释没有去掉。粗心啊粗心。
今天的crontab更是让我崩溃啊,开始service crond start木有此服务,问了好几个师傅都木有答案,后来问了系统GG,yum -y install vixie-cron,安装好了,后来写好规则,几分钟过去了,增量索引却死活不见更新,俺找了很多原因,一一排除,最后想想,难道是服务没开?service crond start。。。成功,无语啊无语,面壁去。
再说个事儿,今天在公司,secureCRT死活不能输入中文,我明明记得在家可以输入的,相关配置也应该基本一致,可就是不行。后来神仙师傅让我试试智能ABC行不行,居然可以,万恶的万能五笔,继续无语。
===========================华丽的分割线=======================
昨天主要是在虚拟机下折腾mysql吧。
因为折腾coreseek的全文检索测试中文,原有的数据是木有中文的,后来就想直接数据库里插入数据,= =不过命令行MS对中文支持不是很好,后来就想还是通过navicat来操作吧。
连接,填入主机名,用户名,密码,确定。
Host **** is not allowed to connect to this MySQL server
google之,原来是root不支持远程登录。
于是在虚拟机下进入mysql,添加帐户
grant all on *.* to ‘erika’@'%’ identified by password
重新用navicat连接,成功。
打开表,直接加一行中文记录,刷新,中文全变成了???
查看表信息,居然是latin1,应该是建表时木有指定编码造成的。
改编码:
mysql> alter table documents charset=utf8;
再加入中文记录,正常显示。
后来想想不能每次建表都这么指定编码吧,= =,可是虚拟机上的mysql居然是木有my.cnf的。这问题昨晚木有解决。今天早上幸亏炽天使童鞋帮忙,终于解决鸟。
#cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
#vi /etc/my.cnf
在[client]跟[mysqld]下分别加上default-character-set=utf8
重启mysql
#/usr/local/mysql/bin/mysqladmin -uuser -ppassword shutdown
#/usr/local/mysql/bin/mysqld_safe &
这下好了,不要每次都要指定编码了。
为嘛每次写就写了这么点,做起来总是耗费很多时间捏,这是一个问题。