我服务器上部署的sphinx-for-chinese全文搜索引擎主要是做博客的搜索,数据量不大,因此,这里更新索引每次都是更新所有。
但是相对于大数据量来说,每次都全部更新的话,速度可能会比较慢。
关于这个问题,sphinx为我们提供了增量索引这个功能。
也就是说,我们每次为新增加的数据增加索引,然后和之前的索引再进行合并,这个相对来讲速度比较快。
1:创建sphinx索引表,用于记录那些数据已经被创建索引
create table sphinx
(
max_id int unsigned not null default 0
)
#该表作用:用于记录article表哪些数据已被创建索引,哪些数据未被创建索引
#获取未创建索引数据:select * from article where id > sphinx表记录的数字
2:修改配置文件(增加新索引)
将如下代码追加至你的sphinx.conf中
# 增量索引数据源
source src1_add
{
# 下面是sql数据库特有的端口,用户名,密码,数据库名等。
type = mysql
sql_host = localhost
sql_user = mysql
sql_pass = xxxxx
sql_db = xxxxx
sql_port = 3306
sql_query_pre = SET NAMES utf8
# (SELECT MAX(max_id) FROM sphinx) 目的,规避重复创建索引
sql_query = SELECT p1.*,p2.content FROM article p1 LEFT JOIN article_info p2 ON p1.id = p2.article_id WHERE p1.id > (SELECT MAX(max_id) FROM sphinx)
# 建完索引这后 ,把最后一条记录的id存到sphinx表中
# 在主查询(sql_query)之后执行的SQL
sql_query_post = UPDATE sphinx SET max_id = (SELECT MAX(id) FROM article)
}
# 增量索引数据索引
index src1_add
{
# 对应的source数据来源名称
source = src1_add
# 索引存放位置
path = /usr/xxx/xxx/xxx/data/src1_add
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
html_strip = 0
charset_type = zh_cn.utf-8
}
3:使用增量索引
(1):重启sphinx服务
使用如下命令:
/usr/local/sphinxforchinese/bin/indexer -c /usr/local/sphinxforchinese/etc/sphinx.conf src1_add
#用于给test1表未创建索引数据增加索引
/usr/local/sphinxforchinese/bin/indexer -c /usr/local/sphinxforchinese/etc/sphinx.conf --merge test1 src1_add --rotate
#将src1_add索引合并到test1索引中
以上大概就是sphinx增量索引的使用。不是很麻烦。
有好的建议,请在下方输入你的评论。
欢迎访问个人博客
https://guanchao.site
网友评论