一、选择对应的操作系统,在官网下载,这里使用window示范Sphinx 3 downloads | Sphinxhttp://sphinxsearch.com/downloads/current/
解压出来之后,bin目录创建配置文件 sphinx.conf
粘贴配置,MySQL账号密码、log目录 I:/sphinx-3.4.1/ 替换成自己的就可以,如果目录不存在要提前先创建
# 定义资源,即数据来源,资源名称
source localhost
{
# 资源类型
type = mysql
# 地址
sql_host = localhost
# 账号
sql_user = root
# 密码
sql_pass = 123456
#库名
sql_db = test
# 端口
sql_port = 3306
# 获取数据的查询语句 ,主键ID 必须放在第一位,剩余的字段可以在后续的match查询中使用
sql_query = \
SELECT customer_id, customer_name,wx_nickname, user_name, customer_mobile \
FROM hx_crm_customer
# 后续查询追加的字段
#sql_attr_uint = group_id
#sql_attr_uint = date_added
}
# 索引名称
index hx_crm_customer
{
#来源于哪个资源,在上面定义的
source = localhost
# sphinx数据存放的位置
path = I:/sphinx-3.4.1/data/test/
}
index testrt
{
type = rt
rt_mem_limit = 128M
path = I:/sphinx-3.4.1/data/testrt/
rt_field = title
rt_field = content
rt_attr_uint = gid
}
indexer
{
mem_limit = 128M
}
searchd
{
listen = 9312
listen = 9306:mysql41
# 运行日志
log = I:/sphinx-3.4.1/log/searchd.log
# 查询日志
query_log = I:/sphinx-3.4.1/log/query.log
read_timeout = 5
max_children = 30
pid_file = I:/sphinx-3.4.1/log/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
workers = threads # for RT to work
# bin目录
binlog_path = I:/sphinx-3.4.1/binlog
}
这里使用的测试数据
DROP TABLE IF EXISTS `hx_crm_customer`;
CREATE TABLE `hx_crm_customer`
(
`customer_id` int NOT NULL AUTO_INCREMENT,
`customer_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '客户微信号',
`wx_nickname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '微信昵称',
`user_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '''' COMMENT '客户姓名',
`customer_mobile` varchar(13) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '手机号',
PRIMARY KEY (`customer_id`) USING BTREE
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8mb4 COMMENT ='客户表';
INSERT INTO `hx_crm_customer`(`customer_id`, `customer_name`, `wx_nickname`, `customer_mobile`, `user_name`)
VALUES (1, 'wx123', '测试1', '13800138000', '小明');
INSERT INTO `hx_crm_customer`(`customer_id`, `customer_name`, `wx_nickname`, `customer_mobile`, `user_name`)
VALUES (2, 'wx789', '测试2', '13800138002', '小红');
INSERT INTO `hx_crm_customer`(`customer_id`, `customer_name`, `wx_nickname`, `customer_mobile`, `user_name`)
VALUES (3, 'wx789', '测试3', '13800138003', '小红');
在根目录下面执行初始化索引 indexer.exe --all --rotate
I:\sphinx-3.4.1\bin>indexer.exe --all --rotate
Sphinx 3.4.1 (commit efbcc658d)
Copyright (c) 2001-2021, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file './sphinx.conf'...
indexing index 'hx_crm_customer'...
collected 3 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 3 docs, 0.1 Kb
total 0.0 sec, 2.9 Kb/sec, 101 docs/sec
skipping non-plain index 'testrt'...
WARNING: failed to open pid_file 'I:/sphinx-3.4.1/log/searchd.pid'.
WARNING: indices NOT rotated.
I:\sphinx-3.4.1\bin>
安装服务 searchd.exe --install --config I:\sphinx-3.4.1\bin\sphinx.conf --servicename SphinxSearch
I:\sphinx-3.4.1\bin>searchd.exe --install --config I:\sphinx-3.4.1\bin\sphinx.conf --servicename SphinxSearch
Sphinx 3.4.1 (commit efbcc658d)
Copyright (c) 2001-2021, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
Installing service...
Service 'SphinxSearch' installed successfully.
这时候会发现多一个服务
启动服务即可,第一次启动可以在CMD运行bin/searchd.exe 输出日志方便调试
连接服务,注意cmd默认是gbk,中文要注意编码
PS C:\Users\Administrator> mysql -P 9306 -uroot -p
Enter password: *********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 3.4.1 (commit efbcc658d)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show tables;
+-----------------+-------+
| Index | Type |
+-----------------+-------+
| hx_crm_customer | local |
| testrt | rt |
+-----------------+-------+
2 rows in set (0.00 sec)
mysql> select * from hx_crm_customer where match('13800138000');
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> select * from hx_crm_customer where match('wx789');
+------+
| id |
+------+
| 2 |
| 3 |
+------+
2 rows in set (0.00 sec)
mysql>