coreseek3.2 php 怎样更新索引
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了coreseek3.2 php 怎样更新索引相关的知识,希望对你有一定的参考价值。
php是无法更新 coreseek 的索引的,需要使用coreseek的语法,配合定时任务来自动更新索引。
这个写起来很麻烦,我们的系统正好用了 coreseek ,说一下我的思路吧。
1、首先建立一个 search 表,这个表用来存你要进行搜索的、经过分词的数据,分词系统你们自己选,我使用的是php的pscws4中文分词。
DROP TABLE IF EXISTS `search`;CREATE TABLE `search` (
`searchid` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`content` text NOT NULL,
`add_time` int(11) NOT NULL,
PRIMARY KEY (`searchid`)
) ENGINE=MyISAM AUTO_INCREMENT=15209 DEFAULT CHARSET=utf8;
2、还需要一个 索引计数表 search_counter,这个表用来存放每次索引更新后的最大一个ID,下次更新索引的时候,就不需要从头更新了,只需要比这个ID大的就可以。
DROP TABLE IF EXISTS `search_counter`;CREATE TABLE `search_counter` (
`counter_id` int(11) NOT NULL,
`max_doc_id` int(11) NOT NULL,
PRIMARY KEY (`counter_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3、配置 coreseek ,以下是我在windows下 coreseek的配置文件,linux 在服务器上,没去找。这里配置了2个索引源,一个是main,一个是增量索引delta,这样不需要每次重建所有索引,只需要合并 main和delta就可以了。
#源定义
source main
type = mysql
sql_host = 192.168.0.10
sql_user = root
sql_pass = root
sql_db = database
sql_port = 3306
sql_query_pre = SET NAMES utf8
sql_query_pre = REPLACE INTO search_counter SELECT 1, MAX(searchid) FROM qhb_search
sql_query = SELECT searchid, title, content, controller_id, controller,add_time FROM search
#sql_query第一列id需为整数
#title、content作为字符串/文本字段,被全文索引
#sql_attr_uint = searchid #从SQL读取到的值必须为整数
sql_attr_uint = controller_id # 数据库ID过滤
sql_attr_uint = controller # 控制器过滤
sql_attr_timestamp = add_time #从SQL读取到的值必须为整数,作为时间属性
sql_query_info_pre = SET NAMES utf8 #命令行查询时,设置正确的字符集
#sql_query_info = SELECT * FROM qhb_search WHERE searchid=$searchid #命令行查询时,从数据库读取原始数据信息
source delta : main
sql_query_pre = SET NAMES utf8
sql_query = SELECT searchid, title, content, controller_id, controller,add_time FROM qhb_search WHERE searchid>( SELECT max_doc_id FROM qhb_search_counter WHERE counter_id=1 )
sql_query_post = REPLACE INTO qhb_search_counter SELECT 1, MAX(searchid) FROM qhb_search
#index定义
index main
source = main #对应的source名称
path = D:/WebSoft/coreseek/var/data/main #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
html_strip = 0
#中文分词配置,详情请查看:http://www.coreseek.cn/products-install/coreseek_mmseg/
#charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux环境下设置,/符号结尾
charset_dictpath = D:/WebSoft/coreseek/etc/ #Windows环境下设置,/符号结尾,最好给出绝对路径,例如:C:/usr/local/coreseek/etc/...
charset_type = zh_cn.utf-8
index delta : main
source = delta
path = D:/WebSoft/coreseek/var/data/delta
#全局index定义
indexer
mem_limit = 128M
#searchd服务定义
searchd
listen = 9312
read_timeout = 5
max_children = 30
max_matches = 1000
seamless_rotate = 0
preopen_indexes = 0
unlink_old = 1
pid_file = D:/WebSoft/coreseek/var/log/searchd_main.pid #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
log = D:/WebSoft/coreseek/var/log/searchd_main.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
query_log = D:/WebSoft/coreseek/var/log/query_main.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
4、建立索引。必须要先建立索引, coreseek 才能启动。下面是我在Windows的建立索引命令,如何使用命令行我就不赘述了。
5、配置并启动服务
D:\\WebSoft\\coreseek\\bin\\sphinx.conf --servicename coreseek
6、Windows创建定时任务,每分钟更新一次索引
D:\\WebSoft\\coreseek\\bin\\indexer.exe --config D:\\WebSoft\\coreseek\\bin\\sphinx.conf delta --rotate
echo indexing, window will close when complete
7、Windows创建定时任务,每天凌晨2点合并索引
D:\\WebSoft\\coreseek\\bin\\indexer.exe --config D:\\WebSoft\\coreseek\\bin\\sphinx.conf --merge main delta --rotate
echo indexing, window will close when complete
8、附上 创建索引,重建索引,合并索引在windows及linux上的方法,以及一些使用上的小问题
建立索引
D:\\WebSoft\\coreseek\\bin\\indexer --all --config d:\\WebSoft\\coreseek\\bin\\sphinx.conf
重建索引
D:\\WebSoft\\coreseek\\bin\\indexer --config D:\\WebSoft\\coreseek\\bin\\sphinx.conf main --rotate
增量索引
D:\\WebSoft\\coreseek\\bin\\indexer --config D:\\WebSoft\\coreseek\\bin\\sphinx.conf delta --rotate
合并索引
D:\\WebSoft\\coreseek\\bin\\indexer --config D:\\WebSoft\\coreseek\\bin\\sphinx.conf --merge main delta --rotate
配置并启动服务
D:\\WebSoft\\coreseek\\bin\\searchd --install --config D:\\WebSoft\\coreseek\\bin\\sphinx.conf --servicename coreseek
创建自定义词库方法:
1、先去 http://pinyin.sogou.com/dict/ 搜狗细胞词库下载需要的词库
2、使用 深蓝词库转换 将词库转换为 txt
3、使用PHP程序将 生成的txt转换为 coreseek 所需要的格式
4、附加到 unigram.txt
5、使用命令更新分词词库
cmd 进入 bin目录,执行下面命令
mmseg -u D:\\WebSoft\\coreseek\\etc\\unigram.txt
6、将生成的 unigram.txt.uni 改名为:uni.lib
7、重建索引
8、重启coreseek服务
注意:
必须先建立索引,服务才能启动
1、coreseek索引或者查询时提示ERROR: invalid token in etc解决办法
该提示表示当前的配置文件的编码不是UTF-8(无BOM头)格式,无法正确解析,请使用编辑软件打开配置文件,另存为UTF-8(无BOM头)格式;
2、failed to lock .....try --rotate
索引已经建立,使用重建索引命令
3、报警告:failed to scanf pid from
没有启动coreseek服务
4、过滤搜索结果,必须使用数组传递,只支持
无符号整数(1-32位宽);
UNIX 时间戳(timestamps);
浮点值(32位,IEEE 754单精度);
字符串序列 (尤其是计算出的整数值);
多值属性 MVA( multi-value attributes ) (32位无符号整型值的变长序列)
$this->shpinx->SetFilter('controller', array(1,2) );
CENTOS 操作方法
开机启动coreseek搜索服务:
vi /etc/rc.d/rc.local
在最后一行添加
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/bin/sphinx.conf
##如要停止搜索服务,请使用/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/bin/sphinx.conf --stop
##如要已启动服务,要更新索引,请使用/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/bin/sphinx.conf --all --rotate
linux下编辑定时任务 crontab -e
#凌晨4点合并索引,其余时间每分钟更新索引
* 0-3 * * * /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf delta --rotate
* 6-23 * * * /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf delta --rotate
0 4 * * * /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --merge main delta --rotate
启动服务:
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/bin/sphinx.conf
建立索引
/usr/local/coreseek/bin/indexer --all --config /usr/local/coreseek/bin/sphinx.conf
重建索引
/usr/local/coreseek/bin/indexer --config /usr/local/coreseek/bin/sphinx.conf main --rotate
增量索引
/usr/local/coreseek/bin/indexer --config /usr/local/coreseek/bin/sphinx.conf delta --rotate
合并索引
/usr/local/coreseek/bin/indexer --config /usr/local/coreseek/bin/sphinx.conf --merge main delta --rotate 参考技术A coreseek索引分为
增量索引和全量索引
这些不是通过php实现的,
参考:
我的coreseek运行在linux下
而是通过脚本来实现的,简单来讲就是命令行来实现
通过定时任务来跑shell脚本来是实现的 参考技术B 说明主索引:index_main,增量索引:index_add
(重建主索引和增量索引)
indexer --config /usr/local/coreseek/etc/csft.conf -rotate index_main
indexer --config /usr/local/coreseek/etc/csft.conf -rotate index_add
(合并建主索引和增量索引)
indexer --config /usr/local/coreseek/etc/csft.conf --merge index_main index_add --merge-dst-range deleted 0 0 -rotate
(重建整个索引)
indexer --config /usr/local/coreseek/etc/csft.conf --rotate -all本回答被提问者采纳 参考技术C 平滑重建beginbuild 参考技术D indexer --config /usr/local/coreseek/etc/csft.conf --rotate -all
coreseek实时索引更新之增量索引
coreseek实时索引更新有两种选择:
1.使用基于磁盘的索引,手动分区,然后定期重建较小的分区(被称为“增量”)。通过尽可能的减小重建部分的大小,可以将平均索引滞后时间降低到30~60秒.在0.9.x版本中,这是唯一可用的方法。在一个巨大的文档集上,这可能是最有效的一种方法
2.版本1.x(从版本1.10-beta开始)增加了实时索引(简写为Rt索引)的支持,用于及时更新全文数据。在RT索引上的更新,可以在1~2毫秒(0.001-0.002秒)内出现在搜索结果中。然而,RT实时索引在处理较大数据量的批量索引上效率并不高。
这篇我们只要是增量索引
基本思路是设置两个数据源和两个索引,对很少更新或根本不更新的数据建立主索引,而对新增文档建立增量索引
在配置文件中定义了主索引和增量索引之后,不能直接用indexer –config d:\\coreseek\\csft.conf –all,再添加数据到数据库中,再用indexer –config d:\\coreseek\\csft.confg main delta –rotate来弄(我居然这样弄了两次)。正确的步骤为:
1.创建主索引:indexer –cd:\\coreseek\\csft.conf --all
2.添加数据
3.再创建增量索引:indexer –cd:\\coreseek\\csft.conf delta --rotate
4.合并索引:indexer –cd:\\coreseek\\csft.conf --merge main delta –rotate(为了防止多个关键字指向同一个文档加上--merge-dst-range deleted 0 0)
增量配置文件如下:
[plain] view plain copy- #增量索引
- source main
- type = mysql
- sql_host = localhost
- sql_user = root
- sql_pass = 123456
- sql_db = hottopic
- sql_port = 3306
- sql_query_pre = SET NAMES utf8
- sql_query_pre = replace into sph_counter select 1,max(id) from st_info
- sql_query_range = select 1,max(id) from st_info
- sql_range_step = 1000
- sql_query = SELECT id, pubDate, title, description,nav_id,rss_id FROM st_info where id>=$start and id <=$end and \\
- id <=(select max_doc_id from sph_counter where counter_id=1)
- sql_attr_uint = nav_id
- sql_attr_uint = rss_id
- sql_attr_timestamp = pubDate
- source delta : main
- sql_query_pre = SET NAMES utf8
- sql_query = SELECT id, pubDate, title, description,nav_id,rss_id FROM st_info where id>=$start and id <=$end and \\
- id >(select max_doc_id from sph_counter where counter_id=1)
- sql_query_post_index = replace into sph_counter select 1,max(id) from st_info
- #index定义
- index main
- source = main
- path = D:/coreseek/coreseek-4.1-win32/var/data/mysqlInfoSPHMain
- docinfo = extern
- mlock = 0
- morphology = none
- min_word_len = 1
- html_strip = 0
- stopwords =
- charset_dictpath = D:/coreseek/coreseek-4.1-win32/etc
- charset_type = zh_cn.utf-8
- index delta : main
- source = delta
- path = D:/coreseek/coreseek-4.1-win32/var/data/mysqlInfoSPHDelta
- #全局index定义
- indexer
- mem_limit = 128M
- #searchd服务定义
- searchd
- listen = 127.0.0.1:9312
- read_timeout = 5
- max_children = 30
- max_matches = 1000
- seamless_rotate = 0
- preopen_indexes = 0
- unlink_old = 1
- pid_file = D:/coreseek/coreseek-4.1-win32/var/log/searchd_mysqlInfoSph.pid
- log = D:/coreseek/coreseek-4.1-win32/var/log/searchd_mysqlInfoSph.log
- query_log = D:/coreseek/coreseek-4.1-win32/var/log/query_mysqlInfoSph.log
- binlog_path =
- compat_sphinxql_magics = 0
注意问题:如果我的主索引为50W条我前天建立的,我昨天增加了10W条的数据,并且建立了增量索引还和主索引合并了,我今天增加了10W的数据并且建立增量索引而且也和主索引合并了,在这两天内我是没有重新建立主索引的,问题来了:昨天是对10W数据进行建立,今天就是20W的数据建立,并且这20W数据中有10W数据其实在主索引中了,这个是非常可怕的?解决方案:
1.一天建立一次主索引
2.在不考虑重新建立主索引的时候,在添加增量索引的时候用sql_query_post_index来改变maxid值我是windows下面手动输入代码成功(不知道用脚本的时候会怎么样)
3.在不考虑重新建立主索引的时候,在合并索引的时候,用脚本链接数据库直接去修改(可以查看:http://banu.blog.163.com/blog/static/2314648201092911412539)
以上是关于coreseek3.2 php 怎样更新索引的主要内容,如果未能解决你的问题,请参考以下文章