1、安装sphinx
安装sphinx-3.1.1很简单,直接下载、解压即可
#下载地址:http://sphinxsearch.com/downloads/current/
#http://sphinxsearch.com/files/sphinx-3.1.1-612d99f-linux-amd64.tar.gz
wget http://sphinxsearch.com/files/sphinx-3.1.1-612d99f-linux-amd64.tar.gz
tar zxf sphinx-3.1.1-612d99f-linux-amd64.tar.gz
mv sphinx-3.1.1 /usr/local/sphinx-3.1.1
ln -sf /usr/local/sphinx-3.1.1 /usr/local/sphinx
ln -sf /usr/local/sphinx/bin/indexer /usr/bin/.
ln -sf /usr/local/sphinx/bin/searchd /usr/bin/.
ln -sf /usr/local/sphinx/bin/wordbreaker /usr/bin/.
ln -sf /usr/local/sphinx/bin/indextool /usr/bin/.
#解决centos7 mysql不能读取:
sudo yum install mysql-devel
sudo ln -s /usr/lib64/mysql/libmysqlclient.so /usr/lib64/
sudo ln -s /usr/lib64/mysql/libmysqlclient.so.18 /usr/lib64/
2、启动sphinx(配置文件附录)
启动服务:
searchd -c /usr/local/sphinx/etc/sphinx.conf
建立索引:
indexer -c /usr/local/sphinx/etc/sphinx.conf --all
停止服务:
searchd --stop
增量索引:
indexer -c /usr/local/sphinx/etc/sphinx.conf --rotate delta
合并索引:
indexer -c /usr/local/sphinx/etc/sphinx.conf --rotate --merge main delta
更新单个索引:
indexer -c /usr/local/sphinx/etc/sphinx.conf --rotate cmstop_video
3、配置增量
vim /data/sh/sphinx.sh
//脚本开始
#/bin/bash
indexer -c /usr/local/sphinx/etc/sphinx.conf --rotate hospital_delta test1_delta cmstop_content_delta cmstop_video_delta cmstop_article_delta cmstop_space_delta
indexer -c /usr/local/sphinx/etc/sphinx.conf --merge cmstop_video cmstop_video_delta --rotate
indexer -c /usr/local/sphinx/etc/sphinx.conf --merge cmstop_content cmstop_content_delta --rotate
indexer -c /usr/local/sphinx/etc/sphinx.conf --merge test1 test1_delta --rotate
indexer -c /usr/local/sphinx/etc/sphinx.conf --merge hospital hospital_delta --rotate
indexer -c /usr/local/sphinx/etc/sphinx.conf --merge cmstop_space cmstop_space_delta --rotate
indexer -c /usr/local/sphinx/etc/sphinx.conf --merge cmstop_article cmstop_article_delta --rotate
//脚本结束
chmod a+x /data/sh/sphinx.sh
crontab -e
*/10 * * * * /data/sh/sphinx.sh
附录:
#
# Minimal Sphinx configuration sample (clean, simple, functional)
#
#问答数据源+索引
source src1
{
type = mysql
sql_host = 127.0.0.1
sql_user = db_user
sql_pass = db_password
sql_db = db_name
sql_port = 3306 # optional, default is 3306
sql_query_pre = SET NAMES utf8
sql_query_pre = replace into newvodjk_sphinx_counter select 1,max(id) from newvodjk_ask
sql_query = SELECT id, title, `desc`, tags FROM newvodjk_ask WHERE isverify = 1 AND id <= (select max_doc_id from newvodjk_sphinx_counter where counter_id = 1)
#sql_attr_uint = id
#sql_attr_timestamp = date_added
}
index test1
{
source = src1
path = /data/sphinx/data/test1
# 设置中文匹配
min_word_len = 1
# 指定字符集(已废弃)
#charset_type = utf-8
# 指定utf-8的编码表
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
min_prefix_len = 0
min_infix_len = 2
# 开启中文分词支持
ngram_len = 1
# 需要分词的字符
ngram_chars = U+4E00..U+9FBB, U+3400..U+4DB5, U+20000..U+2A6D6, U+FA0E, U+FA0F, U+FA11, U+FA13, U+FA14, U+FA1F, U+FA21, U+FA23, U+FA24, U+FA27, U+FA28, U+FA29, U+3105..U+312C, U+31A0..U+31B7, U+3041, U+3043, U+3045, U+3047, U+3049, U+304B, U+304D, U+304F, U+3051, U+3053, U+3055, U+3057, U+3059, U+305B, U+305D, U+305F, U+3061, U+3063, U+3066, U+3068, U+306A..U+306F, U+3072, U+3075, U+3078, U+307B, U+307E..U+3083, U+3085, U+3087, U+3089..U+308E, U+3090..U+3093, U+30A1, U+30A3, U+30A5, U+30A7, U+30A9, U+30AD, U+30AF, U+30B3, U+30B5, U+30BB, U+30BD, U+30BF, U+30C1, U+30C3, U+30C4, U+30C6, U+30CA, U+30CB, U+30CD, U+30CE, U+30DE, U+30DF, U+30E1, U+30E2, U+30E3, U+30E5, U+30E7, U+30EE, U+30F0..U+30F3, U+30F5, U+30F6, U+31F0, U+31F1, U+31F2, U+31F3, U+31F4, U+31F5, U+31F6, U+31F7, U+31F8, U+31F9, U+31FA, U+31FB, U+31FC, U+31FD, U+31FE, U+31FF, U+AC00..U+D7A3, U+1100..U+1159, U+1161..U+11A2, U+11A8..U+11F9, U+A000..U+A48C, U+A492..U+A4C6
}
#增量问答
source test1_delta_src:src1
{
sql_range_step = 10000
#增量索引只查询新增的数据
sql_query = SELECT id, title, `desc`, tags FROM newvodjk_ask WHERE isverify = 1 AND id >= (select max_doc_id from newvodjk_sphinx_counter where counter_id = 1)
}
index test1_delta:test1
{
source = test1_delta_src
path = /data/sphinx/data/test1_delta
}
#医院
source hospital_src
{
type = mysql
sql_host = 127.0.0.1
sql_user = db_user
sql_pass = db_password
sql_db = db_name
sql_port = 3306 # optional, default is 3306
sql_query_pre = SET NAMES utf8
sql_query_pre = replace into newvodjk_sphinx_counter select 2,max(id) from newvodjk_hospital
sql_query = SELECT id, hos_name FROM newvodjk_hospital WHERE id <= (select max_doc_id from newvodjk_sphinx_counter where counter_id = 2)
#sql_attr_uint = id
#sql_attr_timestamp = date_added
}
index hospital:test1
{
source = hospital_src
path = /data/sphinx/data/hospital
}
#增量医院
source hospital_delta_src:hospital_src
{
sql_range_step = 10000
#增量索引只查询新增的数据
sql_query = SELECT id, hos_name FROM newvodjk_hospital WHERE id >= (select max_doc_id from newvodjk_sphinx_counter where counter_id = 2)
}
index hospital_delta:hospital
{
source = hospital_delta_src
path = /data/sphinx/data/hospital_delta
}
#综合
source cmstop_content_src
{
type = mysql
sql_host = 127.0.0.1
sql_user = db_user
sql_pass = db_password
sql_db = db_name
sql_port = 3306 # optional, default is 3306
sql_query_pre = SET NAMES utf8
sql_query_pre = replace into cmstop_sphinx_counter select 1,max(contentid) from cmstop_content
sql_query = SELECT contentid, title, tags FROM cmstop_content WHERE contentid <= (select max_doc_id from cmstop_sphinx_counter where counter_id = 1)
#sql_attr_uint = id
#sql_attr_timestamp = date_added
}
index cmstop_content:test1
{
source = cmstop_content_src
path = /data/sphinx/data/cmstop_content
}
#增量综合
source cmstop_content_delta_src:cmstop_content_src
{
sql_range_step = 10000
#增量索引只查询新增的数据
sql_query = SELECT contentid, title, tags FROM cmstop_content WHERE contentid >= (select max_doc_id from cmstop_sphinx_counter where counter_id = 1)
}
index cmstop_content_delta:cmstop_content
{
source = cmstop_content_delta_src
path = /data/sphinx/data/cmstop_content_delta
}
#视频
source cmstop_video_src
{
type = mysql
sql_host = 127.0.0.1
sql_user = db_user
sql_pass = db_password
sql_db = db_name
sql_port = 3306 # optional, default is 3306
sql_query_pre = SET NAMES utf8
sql_query_pre = replace into cmstop_sphinx_counter select 2,max(contentid) from cmstop_content
sql_query = SELECT contentid, title, tags FROM cmstop_content WHERE modelid = 4 AND contentid <= (select max_doc_id from cmstop_sphinx_counter where counter_id = 2)
#sql_attr_uint = id
#sql_attr_timestamp = date_added
}
index cmstop_video:test1
{
source = cmstop_video_src
path = /data/sphinx/data/cmstop_video
}
#增量视频
source cmstop_video_delta_src:cmstop_video_src
{
sql_range_step = 10000
#增量索引只查询新增的数据
sql_query = SELECT contentid, title, tags FROM cmstop_content WHERE modelid = 4 AND contentid >= (select max_doc_id from cmstop_sphinx_counter where counter_id = 2)
}
index cmstop_video_delta:cmstop_content
{
source = cmstop_video_delta_src
path = /data/sphinx/data/cmstop_video_delta
}
#文章
source cmstop_article_src
{
type = mysql
sql_host = 127.0.0.1
sql_user = db_user
sql_pass = db_password
sql_db = db_name
sql_port = 3306 # optional, default is 3306
sql_query_pre = SET NAMES utf8
sql_query_pre = replace into cmstop_sphinx_counter select 3,max(contentid) from cmstop_content
sql_query = SELECT contentid, title, tags FROM cmstop_content WHERE modelid = 1 AND contentid <= (select max_doc_id from cmstop_sphinx_counter where counter_id = 3)
#sql_attr_uint = id
#sql_attr_timestamp = date_added
}
index cmstop_article:test1
{
source = cmstop_article_src
path = /data/sphinx/data/cmstop_article
}
#增量文章
source cmstop_article_delta_src:cmstop_article_src
{
sql_range_step = 10000
#增量索引只查询新增的数据
sql_query = SELECT contentid, title, tags FROM cmstop_content WHERE modelid = 1 AND contentid >= (select max_doc_id from cmstop_sphinx_counter where counter_id = 3)
}
index cmstop_article_delta:cmstop_article
{
source = cmstop_video_delta_src
path = /data/sphinx/data/cmstop_article_delta
}
#专家
source cmstop_space_src
{
type = mysql
sql_host = 127.0.0.1
sql_user = db_user
sql_pass = db_password
sql_db = db_name
sql_port = 3306 # optional, default is 3306
sql_query_pre = SET NAMES utf8
sql_query_pre = replace into cmstop_sphinx_counter select 4,max(spaceid) from cmstop_content
sql_query = SELECT spaceid, name FROM cmstop_space WHERE spaceid <= (select max_doc_id from cmstop_sphinx_counter where counter_id = 4)
#sql_attr_uint = id
#sql_attr_timestamp = date_added
}
index cmstop_space:test1
{
source = cmstop_space_src
path = /data/sphinx/data/cmstop_space
}
#增量专家
source cmstop_space_delta_src:cmstop_space_src
{
sql_range_step = 10000
#增量索引只查询新增的数据
sql_query = SELECT spaceid, name FROM cmstop_space WHERE spaceid >= (select max_doc_id from cmstop_sphinx_counter where counter_id = 4)
}
index cmstop_space_delta:cmstop_space
{
source = cmstop_space_delta_src
path = /data/sphinx/data/cmstop_space_delta
}
index testrt
{
type = rt
rt_mem_limit = 128M
path = /data/sphinx/data/testrt
rt_field = title
rt_field = content
rt_attr_uint = gid
}
indexer
{
mem_limit = 128M
}
searchd
{
listen = 9312
listen = 9306:mysql41
log = /data/sphinx/log/searchd.log
query_log = /data/sphinx/log/query.log
read_timeout = 5
max_children = 30
pid_file = /data/sphinx/log/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
workers = threads # for RT to work
binlog_path = /data/sphinx/data
}