Sphinx主索引和增量索引来实现索引实时更新的关键步骤

时间:2023-11-28 18:55:44

1.配置csft.conf文件

vim /etc/csft.conf

#
# Minimal Sphinx configuration sample (clean, simple, functional)
# source test_t0
{
type = mysql sql_host = localhost
sql_user = root
sql_pass = 123123
sql_db = test
sql_port = 3306 # optional, default is 3306
sql_query_pre = SET NAMES utf8
sql_query_pre = REPLACE INTO count_t0 select 1,max(id) from t0
sql_query = \
SELECT id, description, name, age \
FROM t0 WHERE id <= (SELECT max_id FROM count_t0 where c_id=1 ) sql_attr_uint = age
#sql_attr_timestamp = date_added #sql_query_info = SELECT * FROM documents WHERE id=$id
} source test_t0_delta:test_t0
{
sql_query_pre = SET NAMES utf8
sql_query_pre =
sql_query = \
SELECT id, description, name, age \
FROM t0 WHERE id > (SELECT max_id FROM count_t0 where c_id=1 )
} index test_t0
{
source = test_t0
path = /usr/local/coreseek/var/data/test_t0
docinfo = extern
charset_dictpath = /usr/local/mmseg/etc/
charset_type = zh_cn.utf-8
} index test_t0_delta:test_t0
{
source = test_t0_delta
path = /usr/local/coreseek/var/data/test_t0_delta
docinfo = extern
charset_dictpath = /usr/local/mmseg/etc/
charset_type = zh_cn.utf-8
} indexer
{
mem_limit = 512M
} searchd
{
port = 9312
log = /usr/local/coreseek/var/log/searchd.log
query_log = /usr/local/coreseek/var/log/query.log
read_timeout = 5
max_children = 30
pid_file = /usr/local/coreseek/var/log/searchd.pid
max_matches = 1000
seamless_rotate = 1
preopen_indexes = 0
unlink_old = 1
}

2.重新生成主索引的shell脚本文件

cd /usr/local/shell

vim test_t0.sh

#!/bin/sh
/usr/local/coreseek/bin/searchd -c /etc/csft.conf --stop #停止searchd
/usr/local/coreseek/bin/indexer -c /etc/csft.conf test_t0 #生成索引
/usr/local/coreseek/bin/searchd -c /etc/csft.conf #启动searchd

chmod 755 test_t0.sh

3.重新生成增量索引的shell脚本文件

cd /usr/local/shell

vim test_t0_delta.sh

#!/bin/sh
/usr/local/coreseek/bin/searchd -c /etc/csft.conf --stop
/usr/local/coreseek/bin/indexer -c /etc/csft.conf test_t0_delta /usr/local/coreseek/bin/searchd -c /etc/csft.conf #启动searchd

chmod 755 test_t0_delta.sh

4.添加到定时任务

crontab -e

  * * * /usr/local/shell/test_t0.sh #每天凌晨3点半更新主索引
*/ * * * * /usr/local/shell/test_t0_delta.sh #每隔10分钟更新增量索引