linux下安装支持中文全文索引的sphinx
2022-02-26 15:18:24
一、下载sphinx-for-chinese(下载地址:https://www.codelovers.cn/Public/file/sphinx-for-chinese2.2.1.tar.gz)并解压
二、进入已解压的目录执行,将程序安装到 /usr/local/sphinxforchinese目录下:
./configure --prefix=/usr/local/sphinxforchinese --with-mysql
make && make install
注:如果在执行make的时候报错:
报错信息

原因是g++没有添加 -libiconv选项
vim src/Makefile
把LIBS = -lm -lz -lexpat -L/usr/local/lib -lrt -lpthread
改成
LIBS = -lm -lz -lexpat -L/usr/local/lib -lrt -lpthread -liconv 即可
三、生成字典
1)执行命令
cd /usr/local/sphinxforchinese
2)将文件夹中的 xdict_1.1.txt (https://www.codelovers.cn/Public/file/xdict_1.1.tar.gz)文件下载到 /usr/local/sphinxforchinese目录下
3)执行命令
/usr/local/sphinxforchinese/bin/mkdict xdict_1.1.txt etc/xdict
执行成功后会在/usr/local/sphinxforchinese/etc/目录生成xdict文件。
四、重建索引
1)将 sphinx.conf 放到 /usr/local/sphinxforchinese/etc 中并且配置数据库连接信息
sphinx.conf 文件信息如下:
### Sphinx 搜索引擎 索引配置文件 新增的索引追加到该文件 ###
# 资讯源
source articles
{
type = mysql
sql_host = 127.0.0.1 # 数据库host
sql_user = root # 数据库用户名
sql_pass = root # 数据库密码
sql_db = test # 数据库名称
sql_port = 3306
sql_query_pre = SET NAMES utf8
sql_query = SELECT \
id, state, title, UNIX_TIMESTAMP(created_at) AS created_at \
FROM \
articles
### 以下是用来过滤、条件查询、排序字段的属性 ###
sql_attr_uint = state
sql_attr_uint = created_at
# sql_query_info_pre = SET NAMES utf8 #命令行查询时,设置正确的字符集
# sql_query_info = SELECT * FROM articles WHERE id=$id #命令行查询时,从数据库读取原始数据信息
}
# 资讯索引
index articles
{
source = articles #对应的source名称
path = /usr/local/sphinxforchinese/data/articles #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
docinfo = extern
mlock = 0 # searchd会将spa和spi预读取到内存中。但是如果这部分内存数据长时间没有访问,则它会被交换到磁盘上。
# 设置了mlock就不会出现这个问题,这部分数据会一直存放在内存中的。
min_word_len = 1 # 索引的词最小长度
#min_prefix_len = 1 # 最小前缀
#min_infix_len = 1 # 最小中缀
expand_keywords = 1 # 是否尽可能展开关键字的精确格式或者型号形式
ngram_len = 1 # 对于非字母型数据的长度切割
ngram_chars = U+3000..U+2FA1F # 对于非字母型数据的长度切割,N-Gram是指不按照词典,而是按照字长来分词
#charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F, U+226..U+252 # 字符表和大小写转换规则
charset_type = utf-8
# 中文分词配置 sphinxforchinese
chinese_dictionary = /usr/local/sphinxforchinese/etc/xdict
}
# 资讯增量索引 realtime
# index articlesrt
# {
# type = rt
# rt_mem_limit = 128M
# path = /usr/local/sphinxforchinese/data/articlesrt
# charset_type = utf-8
# rt_field = title
# rt_attr_uint = gid
# }
#全局index定义
indexer
{
mem_limit = 128M
}
#searchd服务定义
searchd
{
listen = 9312
listen = 9306:mysql41
read_timeout = 5
max_children = 30
max_matches = 1000
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
pid_file = /usr/local/sphinxforchinese/log/searchd.pid #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
log = /usr/local/sphinxforchinese/log/searchd.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
query_log = /usr/local/sphinxforchinese/log/query.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
workers = threads # for RT to work
binlog_path = /usr/local/sphinxforchinese/data #关闭binlog日志
}
2)执行命令重建索引
/usr/local/sphinxforchinese/bin/indexer -c /usr/local/sphinxforchinese/etc/sphinx.conf --all --rotate
五、启动sphinx
/usr/local/sphinxforchinese/bin/searchd -c /usr/local/sphinxforchinese/etc/sphinx.conf
作者:乐融沃官方账号 https://www.bilibili.com/read/cv15427640 出处:bilibili
下一篇:
乐融沃b2b2c开源项目计划