Sphinx 2.3.2-beta 笔记

版权

本文为原创, 遵循 CC 4.0 BY-SA 版权协议, 转载需注明出处: https://blog.csdn.net/big_cheng/article/details/122112520.

2.3.2-beta

Sphinx 虽然功能强大, 但上手有一些’坑’, 希望本文对您有帮助.

环境(CentOS-7, virtualbox虚机) 无法成功运行目前最新的Sphinx 3.4.1(*** Oops, indexer crashed! Please send the following report to developers. …), 所以选用了http://sphinxsearch.com/downloads/archive/ 的最新版:

rpm -Uhv sphinx-2.3.2-1.rhel7.x86_64.rpm

注: 帮助文档在.rpm 内usr\share\doc\sphinx-2.3.2\sphinx.html - 建议您事先阅读(内容很多).

mysql lib

环境是10.6.3-MariaDB 而非mysql, 开始时总是连不到mysql 数据库, 尝试了yum install mariadb-devel/mariadb-libs/mysql-community-libs/mysql-community-client/mysql-community-devel 都不行.

https://sphinxsearch.com/forum/view.html?id=15745

# 因为libmysqlclient.so link的目标不存在
cd /usr/lib64
rm libmysqlclient.so
ln -s libmariadbd.so.19 libmysqlclient.so
ls libmysql* -l

在3x文档"Installing indexer SQL drivers on Linux" 里提到具体名称: “libmysqlclient.so or libmariadb.so” - Sphinx 按顺序尝试加载这2个lib.

服务启动失败

似乎启动服务命令运行成功, 且日志里无错误, 但服务状态总是出错: “searchd.service start operation timed out”.

改为手动启动:

# 修改索引文件权限
cd /var/lib/sphinx
chown sphinx.sphinx *
ls -l

# 手工su sphinx 后启动、停止ok, .pid文件创建、删除正常
searchd --config /etc/sphinx/sphinx.conf
searchd --config /etc/sphinx/sphinx.conf --stopwait

中文(ngram)

sphinx.conf
    source
        sql_query_pre = SET NAMES utf8
    index
        stopwords  = /etc/sphinx/stopwords-innodb.txt  /etc/sphinx/stopwords-myisam.txt
        ngram_len = 1
        ngram_chars = U+3000..U+2FA1F
example.sql
    + SET NAMES 'utf8'
    CREATE TABLE ... DEFAULT CHARSET=utf8;

见文档12.1.11. sql_query_pre: “Sphinx accepts only UTF-8 texts”.

stopwords 借用mysql 的: mysql-5.7 Chapter-12 functions.html “12.10 Full-Text Search Functions”.

ngram_chars = U+3000…U+2FA1F
https://www.unicode.org/versions/Unicode14.0.0/ch02.pdf “2.9 Details of Allocation”. CJK 没有一个单一连续的范围, 上面范围(Plane 0)里也包含非CJK 字符.

main+delta (plain+plain)

main 有记录1,2, delta 也有记录1,2, 查询时delta 将覆盖main 的:

# conf "source srcDelta" 注掉"sql_query_killlist = select 2" 行

# (sphinx用户)
indexer --all # 重建索引
searchd --config /etc/sphinx/sphinx.conf # 启动searchd
# (root用户) mysql -h0 -P9306
MySQL [(none)]> select * from main;
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   11 | foo  |       0 |
|    2 |   22 | bar  |       0 |
+------+------+------+---------+
2 rows in set (0.001 sec)

MySQL [(none)]> select * from delta;
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   33 | baz  |       0 |
|    2 |   44 | xyz  |       1 |
+------+------+------+---------+
2 rows in set (0.001 sec)
# 4条记录都匹配'cat'. 因为delta 在后, 所以相同id 只返回delta 的记录(main 的记录被屏蔽):
MySQL [(none)]> select * from main,delta where match('cat');
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   33 | baz  |       0 |
|    2 |   44 | xyz  |       1 |
+------+------+------+---------+
2 rows in set (0.001 sec)
# delta 记录2 被"deleted"条件过滤掉后, main 记录2 不再被屏蔽:
MySQL [(none)]> select * from main,delta where match('cat') and deleted=0;
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   33 | baz  |       0 |
|    2 |   22 | bar  |       0 |
+------+------+------+---------+
2 rows in set (0.001 sec)

可见准确理解是: main和delta 均匹配后, 2组结果里重复的记录选用delta的 - delta不能完全屏蔽main(实质是delta or main).

把delta 合并到main:

# (sphinx用户) (因为searchd 正在使用索引文件, 加"--rotate")
indexer --merge main delta --rotate
# (root用户)
MySQL [(none)]> select * from main;
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   33 | baz  |       0 |
|    2 |   44 | xyz  |       1 |
+------+------+------+---------+
2 rows in set (0.001 sec)

MySQL [(none)]> select * from main where match('中');
Empty set (0.001 sec)

MySQL [(none)]> select * from main where match('日');
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   33 | baz  |       0 |
|    2 |   44 | xyz  |       1 |
+------+------+------+---------+
2 rows in set (0.001 sec)

可见delta 记录的field/attr 都覆盖到了main.

kill-list, main+delta (plain+plain)

见文档"12.1.16. sql_query_killlist".

main 有记录1,2. delta 有记录1,2, kill-list 含记录2:

# conf "source srcDelta" 启用"sql_query_killlist = select 2" 行

# 注意: test数据库delta表仍有记录1,2 (2未删).

# (sphinx用户)
cd /var/lib/sphinx # sphinx 索引文件目录
searchd --config /etc/sphinx/sphinx.conf --stopwait # 停searchd
rm * # 删除所有索引文件
indexer --all # 重建所有索引
searchd --config /etc/sphinx/sphinx.conf # 启动searchd
# (root用户) mysql -h0 -P9306
# delta 仍有记录1,2(且查询时屏蔽了main 记录):
MySQL [(none)]> select * from main;
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   11 | foo  |       0 |
|    2 |   22 | bar  |       0 |
+------+------+------+---------+
2 rows in set (0.001 sec)

MySQL [(none)]> select * from delta;
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   33 | baz  |       0 |
|    2 |   44 | xyz  |       1 |
+------+------+------+---------+
2 rows in set (0.001 sec)

MySQL [(none)]> select * from main,delta where match('cat');
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   33 | baz  |       0 |
|    2 |   44 | xyz  |       1 |
+------+------+------+---------+
2 rows in set (0.001 sec)
# 但是delta 记录2 被过滤掉时, main 记录2 仍会被屏蔽:
MySQL [(none)]> select * from main,delta where match('cat') and deleted=0;
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   33 | baz  |       0 |
+------+------+------+---------+
1 row in set (0.001 sec)

MySQL [(none)]> select * from main,delta where match('中');
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   11 | foo  |       0 |
+------+------+------+---------+
1 row in set (0.001 sec)

MySQL [(none)]> select * from main,delta where addr='bar';
Empty set (0.000 sec)

可见delta kill-list 彻底屏蔽了main 的相关记录, 无论delta 本身返回这些记录否(可理解为delta or (main - delta.killlist)).

合并delta 到main:

# (sphinx用户)
indexer --merge main delta --rotate
# (root用户)
MySQL [(none)]> select * from main;
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   33 | baz  |       0 |
|    2 |   44 | xyz  |       1 |
+------+------+------+---------+
2 rows in set (0.000 sec)

MySQL [(none)]> select * from main where match('中');
Empty set (0.001 sec)

MySQL [(none)]> select * from main where match('日');
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   33 | baz  |       0 |
|    2 |   44 | xyz  |       1 |
+------+------+------+---------+
2 rows in set (0.001 sec)

delta 完全覆盖了main.

另, 测试delta 仅含记录1 且kill-list 含记录2 (数据库表里删除记录2; conf 启用"sql_query_killlist = select 2" 行), 合并后main 仅含记录1(33-baz) - 即delta kill-list 始终有效, 无论delta 本身有无这些记录.

main+rt (plain+rt)

rt 的优势是可以在线增删改且立即反映(plain 只能在线改attr), 但存在rt or main 问题: rt 记录不匹配时, 重复的main 记录(代表修改前的旧值) 只要匹配仍能返回:

# (sphinx用户) 重建:
searchd --config /etc/sphinx/sphinx.conf --stopwait
rm *
indexer --all
searchd --config /etc/sphinx/sphinx.conf
# (root用户) mysql -h0 -P9306
# rt 初始为空:
MySQL [(none)]> select * from main;
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   11 | foo  |       0 |
|    2 |   22 | bar  |       0 |
+------+------+------+---------+
2 rows in set (0.001 sec)

MySQL [(none)]> select * from rt;
Empty set (0.002 sec)
# rt 新增记录1 - 立即反映:
MySQL [(none)]> replace into rt (id, title, content, age, addr, deleted) values (1, 'rt one', '日本 cat', 33, 'baz', 0);
Query OK, 1 row affected (0.001 sec)

MySQL [(none)]> select * from rt;
+------+------+---------+------+
| id   | age  | deleted | addr |
+------+------+---------+------+
|    1 |   33 |       0 | baz  |
+------+------+---------+------+
1 row in set (0.002 sec)
# main+rt 也是rt or main:
MySQL [(none)]> select * from main,rt where match('cat');
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   33 | baz  |       0 |
|    2 |   22 | bar  |       0 |
+------+------+------+---------+
2 rows in set (0.002 sec)

MySQL [(none)]> select * from main,rt where match('cat') and addr!='baz';
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   11 | foo  |       0 |
|    2 |   22 | bar  |       0 |
+------+------+------+---------+
2 rows in set (0.001 sec)

通常不希望也能搜到修改前的旧内容. 可以在修改rt 记录同时也修改main 旧记录(置一个标志attr) - 查询时总是检查此标志:

# 因为rt 记录1 最新, 弃用main 记录1:
MySQL [(none)]> update main set deleted=1 where id=1;
Query OK, 1 row affected (0.001 sec)
# 查询时总是检查弃用标志:
MySQL [(none)]> select * from main,rt where deleted=0 and match('cat') and addr!='baz';
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    2 |   22 | bar  |       0 |
+------+------+------+---------+
1 row in set (0.001 sec)

如此rt 可以完全屏蔽重复的main 记录(即使之后rt 记录又删除了).

注1-rt 不能合并到plain:

bash-4.2$ indexer --merge main rt --rotate
......
using config file '/etc/sphinx/sphinx.conf'...
merging index 'rt' into index 'main'...
FATAL: failed to merge index 'rt' into index 'main': source index preload failed: failed to open /var/lib/sphinx/rt.sph: No such file or directory

注2-rt 磁盘结构实际是append-only:
见文档"4.3. RT index internals": rt 增删改先写到一个唯一的内存chunk, 内存chunk 满了后再写到磁盘chunk. 所有磁盘chunk 不会更新只会append, rt 也使用plain kill-list 的方式来屏蔽旧记录. 所以rt 的实时可更新是有代价的 - 过多的删/改会导致磁盘碎片和搜索变慢.
见文档"8.36. OPTIMIZE INDEX syntax" rt 碎片整理.

--merge-dst-range

见文档"3.13. Index merging" 和"7.1. indexer command reference".

delta 合并到main(rt 不能参与合并) 时可以指定main 记录的过滤条件: 先过滤main 再合并.
测试案例: main 记录1,2, 1更新为deleted=1. delta 无记录1, 记录2 deleted=1. 合并时指定"deleted 0 0". 结果无记录1, 记录2合并了:

# conf "source srcDelta" 注掉"sql_query_killlist" 行

# 确认test数据库(非sphinx索引) 记录如下:
MariaDB [test]> select * from main;
+----+------------+---------------------------+-----+------+---------+
| id | title      | content                   | age | addr | deleted |
+----+------------+---------------------------+-----+------+---------+
|  1 | main 1 one | this 中 国 abc cat from   |  11 | foo  |       0 |
|  2 | main 1 two | this中国def cats cannot   |  22 | bar  |       0 |
+----+------------+---------------------------+-----+------+---------+
2 rows in set (0.001 sec)

MariaDB [test]> select * from delta;
+----+-------------+------------+-----+------+---------+
| id | title       | content    | age | addr | deleted |
+----+-------------+------------+-----+------+---------+
|  2 | delta 1 two | 日本cats   |  44 | xyz  |       1 |
+----+-------------+------------+-----+------+---------+
1 row in set (0.001 sec)

# (sphinx用户)
searchd --config /etc/sphinx/sphinx.conf --stopwait
rm *
indexer --all
searchd --config /etc/sphinx/sphinx.conf
# (root用户) mysql -h0 -P9306
MySQL [(none)]> update main set deleted=1 where id=1;
Query OK, 1 row affected (0.001 sec)

MySQL [(none)]> select * from main;
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   11 | foo  |       1 |
|    2 |   22 | bar  |       0 |
+------+------+------+---------+
2 rows in set (0.001 sec)

MySQL [(none)]> select * from delta;
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    2 |   44 | xyz  |       1 |
+------+------+------+---------+
1 row in set (0.001 sec)
# (sphinx用户) 执行合并:
indexer --merge main delta --merge-dst-range deleted 0 0 --rotate
# (root用户)
MySQL [(none)]> select * from main;
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    1 |   11 | foo  |       0 |
|    2 |   44 | xyz  |       1 |
+------+------+------+---------+
2 rows in set (0.001 sec)

Oops! 为什么记录1 没有被过滤掉? 原来"update main deleted=1" 时仅仅更新了内存的attr缓存 - 并未写到磁盘, 看起来merge 时只读了磁盘!
从头再来:

# 同上......

# attr缓存写到磁盘:
MySQL [(none)]> flush attributes;
+------+
| tag  |
+------+
|    1 |
+------+
1 row in set (0.502 sec)
# (sphinx用户) 执行合并:
indexer --merge main delta --merge-dst-range deleted 0 0 --rotate
# (root用户)
MySQL [(none)]> select * from main;
+------+------+------+---------+
| id   | age  | addr | deleted |
+------+------+------+---------+
|    2 |   44 | xyz  |       1 |
+------+------+------+---------+
1 row in set (0.001 sec)

现在正常: main 记录1 在合并前先被条件过滤掉, 而不满足条件的delta 记录2 没有被过滤, 可见过滤条件仅用于原目标索引(main).

与kill-list 的关系

前面章节讲过delta kill-list 在合并时也能屏蔽main 记录, 看起来这2种都是合并时过滤的不同方式, 适用于不同场合: kill-list 完全不触动main, 而--merge-dst-range 需要更新main attr(到磁盘).

设计测试案例-改用kill-list: main 记录1,2(deleted均0). delta 无记录1, 记录2 deleted=1, kill-list 含1. 合并时不指定过滤条件. 结果相同.

事务

经试验Sphinx 的事务与db 事务差异较大. Update:

MySQL [(none)]> select * from rt;
+------+-------------+------+------+---------+
| id   | content_str | age  | addr | deleted |
+------+-------------+------+------+---------+
|    1 | 日 本 cat   |   33 | baz  |       0 |
|    2 | 日本cats    |   44 | xyz  |       1 |
+------+-------------+------+------+---------+
2 rows in set (0.001 sec)

# (autocommit 设不设置似乎没区别)
MySQL [(none)]> set autocommit=0;
Query OK, 0 rows affected (0.000 sec)

MySQL [(none)]> begin;
Query OK, 0 rows affected (0.000 sec)

MySQL [(none)]> update rt set deleted=3 where id=1;
Query OK, 1 row affected (0.001 sec)

MySQL [(none)]> select * from rt;
+------+-------------+------+------+---------+
| id   | content_str | age  | addr | deleted |
+------+-------------+------+------+---------+
|    1 | 日 本 cat   |   33 | baz  |       3 |
|    2 | 日本cats    |   44 | xyz  |       1 |
+------+-------------+------+------+---------+
2 rows in set (0.001 sec)

# 直接kill 进程:
ps -ef|grep searchd
kill xxxx yyyy
ps -ef|grep searchd # 确认已kill

searchd --config /etc/sphinx/sphinx.conf

# mysql -h0 -P9306
MySQL [(none)]> select * from rt;
+------+-------------+------+------+---------+
| id   | content_str | age  | addr | deleted |
+------+-------------+------+------+---------+
|    1 | 日 本 cat   |   33 | baz  |       3 |
|    2 | 日本cats    |   44 | xyz  |       1 |
+------+-------------+------+------+---------+
2 rows in set (0.002 sec)

未commit 的值保留下来了, 可见update 是立即生效. Update rollback:

MySQL [(none)]> begin;
Query OK, 0 rows affected (0.000 sec)

MySQL [(none)]> select * from rt;
+------+-------------+------+------+---------+
| id   | content_str | age  | addr | deleted |
+------+-------------+------+------+---------+
|    1 | 日 本 cat   |   33 | baz  |       0 |
|    2 | 日本cats    |   44 | xyz  |       1 |
+------+-------------+------+------+---------+
2 rows in set (0.001 sec)

MySQL [(none)]> update rt set deleted=3 where id=1;
Query OK, 1 row affected (0.001 sec)

MySQL [(none)]> select * from rt;
+------+-------------+------+------+---------+
| id   | content_str | age  | addr | deleted |
+------+-------------+------+------+---------+
|    1 | 日 本 cat   |   33 | baz  |       3 |
|    2 | 日本cats    |   44 | xyz  |       1 |
+------+-------------+------+------+---------+
2 rows in set (0.001 sec)

MySQL [(none)]> rollback;
Query OK, 0 rows affected (0.000 sec)

MySQL [(none)]> exit
Bye

# mysql -h0 -P9306

MySQL [(none)]> select * from rt;
+------+-------------+------+------+---------+
| id   | content_str | age  | addr | deleted |
+------+-------------+------+------+---------+
|    1 | 日 本 cat   |   33 | baz  |       3 |
|    2 | 日本cats    |   44 | xyz  |       1 |
+------+-------------+------+------+---------+
2 rows in set (0.001 sec)

改为3, rollback 后退出重连, 查询到仍是3. 可见update 仍是立即生效的, rollback 对它无效.

Replace:

MySQL [(none)]> begin;
Query OK, 0 rows affected (0.000 sec)

MySQL [(none)]> select * from rt;
+------+-------------+------+------+---------+
| id   | content_str | age  | addr | deleted |
+------+-------------+------+------+---------+
|    1 | 日 本 cat   |   33 | baz  |       0 |
|    2 | 日本cats    |   44 | xyz  |       1 |
+------+-------------+------+------+---------+
2 rows in set (0.001 sec)

MySQL [(none)]> replace into rt values (1, 'delta 1 one', '日 本 cat', '日 本 cat', 33, 'baz', 3);
Query OK, 1 row affected (0.001 sec)

MySQL [(none)]> select * from rt;
+------+-------------+------+------+---------+
| id   | content_str | age  | addr | deleted |
+------+-------------+------+------+---------+
|    1 | 日 本 cat   |   33 | baz  |       0 |
|    2 | 日本cats    |   44 | xyz  |       1 |
+------+-------------+------+------+---------+
2 rows in set (0.001 sec)

MySQL [(none)]> commit;
Query OK, 0 rows affected (0.000 sec)

MySQL [(none)]> select * from rt;
+------+-------------+------+------+---------+
| id   | content_str | age  | addr | deleted |
+------+-------------+------+------+---------+
|    1 | 日 本 cat   |   33 | baz  |       3 |
|    2 | 日本cats    |   44 | xyz  |       1 |
+------+-------------+------+------+---------+
2 rows in set (0.001 sec)

replace 后在事务内查到的仍是旧值, commit 后才能查到新值. 文档"8.11. BEGIN, COMMIT, and ROLLBACK syntax" 最后一句"Overly isolated means that the changes are not only invisible to the concurrent transactions" 就是这个意思.

Replace rollback:

MySQL [(none)]> begin;
Query OK, 0 rows affected (0.000 sec)

MySQL [(none)]> select * from rt;
+------+-------------+------+------+---------+
| id   | content_str | age  | addr | deleted |
+------+-------------+------+------+---------+
|    1 | 日 本 cat   |   33 | baz  |       0 |
|    2 | 日本cats    |   44 | xyz  |       1 |
+------+-------------+------+------+---------+
2 rows in set (0.001 sec)

MySQL [(none)]> replace into rt values (1, 'delta 1 one', '日 本 cat', '日 本 cat', 33, 'baz', 3);
Query OK, 1 row affected (0.001 sec)

MySQL [(none)]> select * from rt;
+------+-------------+------+------+---------+
| id   | content_str | age  | addr | deleted |
+------+-------------+------+------+---------+
|    1 | 日 本 cat   |   33 | baz  |       0 |
|    2 | 日本cats    |   44 | xyz  |       1 |
+------+-------------+------+------+---------+
2 rows in set (0.001 sec)

MySQL [(none)]> rollback;
Query OK, 0 rows affected (0.000 sec)

MySQL [(none)]> select * from rt;
+------+-------------+------+------+---------+
| id   | content_str | age  | addr | deleted |
+------+-------------+------+------+---------+
|    1 | 日 本 cat   |   33 | baz  |       0 |
|    2 | 日本cats    |   44 | xyz  |       1 |
+------+-------------+------+------+---------+
2 rows in set (0.001 sec)

replace rollback 前后均是旧值. 之前是事务内看不到新值, 之后说明rollback 成功了.

试验结果总结:

  • update 不支持事务, 总是直接生效了: begin => update => 查询值已生效 => 再commit/rollback无作用
  • replace 支持事务: begin => replace => 查询值未变 => commit/rollback 后再查起作用了
  • delete 支持
  • (单笔)insert 支持

附录

sphinx.conf

(注: 未经优化)

source srcMain
{
	type			= mysql

	sql_host		= localhost
	sql_user		= root
	sql_pass		= 1
	sql_db			= test
	sql_port		= 3306	# optional, default is 3306
	
	sql_query_pre = SET NAMES utf8

	sql_query		= \
		SELECT id, title, content, age, addr, deleted \
		FROM main

	sql_attr_uint		= age
	sql_attr_string		= addr
	sql_attr_uint = deleted
}

index main
{
	source			= srcMain
	path			= /var/lib/sphinx/main
	morphology  = stem_en
	stopwords  = /etc/sphinx/stopwords-innodb.txt  /etc/sphinx/stopwords-myisam.txt
	ngram_len = 1
	ngram_chars = U+3000..U+2FA1F
}


source srcDelta : srcMain {
  sql_query = select id, title, content, age, addr, deleted \
    from delta
  sql_query_killlist = select 2
}

index delta : main {
  source = srcDelta
  path = /var/lib/sphinx/delta
}


index rt
{
	type			= rt
	rt_mem_limit		= 128M

	path			= /var/lib/sphinx/rt
	morphology  = stem_en
	stopwords  = /etc/sphinx/stopwords-innodb.txt  /etc/sphinx/stopwords-myisam.txt
	ngram_len = 1
	ngram_chars = U+3000..U+2FA1F

	rt_field = title
	rt_field = content
	rt_attr_uint   = age
	rt_attr_string = addr
	rt_attr_uint   = deleted
}


indexer
{
	mem_limit		= 128M
}


searchd
{
	listen			= 9306:mysql41
	log			= /var/log/sphinx/searchd.log
	query_log		= /var/log/sphinx/query.log
	read_timeout		= 5
	max_children		= 30
	pid_file		= /var/run/sphinx/searchd.pid
	seamless_rotate		= 1
	preopen_indexes		= 1
	unlink_old		= 1
	workers			= threads # for RT to work
	binlog_path		= /var/lib/sphinx/
}

example.sql

SET NAMES 'utf8';
use test;


DROP TABLE IF EXISTS test.main;
CREATE TABLE test.main
(
	id          INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
	title       VARCHAR(255) NOT NULL,
	content     TEXT NOT NULL,
	age         INTEGER NOT NULL,
	addr       VARCHAR(24) NOT NULL,
	deleted    INTEGER NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

REPLACE INTO test.main ( id, title, content, age, addr, deleted ) VALUES
	( 1, 'main 1 one', 'this 中 国 abc cat from', 11, 'foo', 0 ),
	( 2, 'main 1 two', 'this中国def cats cannot', 22, 'bar', 0 );


DROP TABLE IF EXISTS test.delta;
CREATE TABLE test.delta
(
	id          INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
	title       VARCHAR(255) NOT NULL,
	content     TEXT NOT NULL,
	age         INTEGER NOT NULL,
	addr       VARCHAR(24) NOT NULL,
	deleted    INTEGER NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

replace into test.delta (id, title, content, age, addr, deleted) VALUES
	(1, 'delta 1 one', '日 本 cat', 33, 'baz', 0),
	(2, 'delta 1 two', '日本cats', 44, 'xyz', 1 );

CentOS-8 环境

经试验CentOS-8 环境(MariaDB-10.6) 可如下安装2.3.2-beta:

# install pg|odbc 依赖
yum install postgresql-libs unixODBC
# 忽略目前缺的依赖, 强制安装:
rpm -Uhv -i --nodeps sphinx-2.3.2-1.rhel7.x86_64.rpm
# 缺的依赖直接ln 现有的文件:
# (缺依赖运行searchd 会报错例如: error while loading shared libraries: libmysqlclient.so.18: ...)
# (可从CentOS-7环境拷贝, 文件版本可略有差异)
cd /usr/lib64
ln -s libmariadb.so.3 libmysqlclient.so.18
ln -s libssl.so.1.1.1g libssl.so.10
ln -s libcrypto.so.1.1.1g libcrypto.so.10

安装rpm后会提示:
[/usr/lib/tmpfiles.d/searchd.conf:1] Line references path below legacy directory /var/run/, updating /var/run/sphinx → /run/sphinx; please update the tmpfiles.d/ drop-in file accordingly.
这个好像是建议弃用/var/run/sphinx 改用/run/sphinx, 但因为sphinx.conf “pid_file” 及searchd.service 里都用老的路径, 试验时没改.

可修改/usr/lib/systemd/system/searchd.service 改用root 账号启动服务:

User=root
Group=root
# ExecStartPre=/bin/chown sphinx.sphinx /var/run/sphinx

参考

使用Sphinx 2.3.2-beta 实现全文搜索(rt 方案)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值