本篇笔记记录了CentOS6.9编译安装Sphinx,编译安装php-sphinx扩展,并使用php7的sphinx扩展实现全文搜索的过程
nginx+mysql+php安装请参考以下笔记:
CentOS7源码编译安装nginx+php7.2+mysql5.7并使用systemctl管理
CentOS7yum安装nginx+php7+mysql
CentOS6.9源码编译安装nginx+php7+mysql环境
CentOS6.9yum安装nginx+php7+mysql环境
安装依赖
yum install wget gcc gcc-c++ automake autoconf expat expat-devel mysql-devel
进入源码存放目录,下载Sphinx
cd /usr/local/src/
wget -c http://sphinxsearch.com/files/sphinx-2.2.11-release.tar.gz
解压,编译,安装
tar zxvf sphinx-2.2.11-release.tar.gz
cd sphinx-2.2.11-release
./configure --prefix=/usr/local/sphinx --with-mysql --with-libexpat --enable-id64
make && make install
安装libsphinxclient,php扩展依赖
cd api/libsphinxclient
./configure --prefix=/usr/local/sphinx/libsphinxclient
make && make install
下载安装Sphinx的PHP扩展
php5
wget -c https://pecl.php.net/get/sphinx-1.3.3.tgz
tar zxvf sphinx-1.3.3.tgz
cd sphinx-1.3.3
php7
wget -c https://git.php.net/?p=pecl/search_engine/sphinx.git;a=snapshot;h=37594d5c4bf8da83880e1086f83735bd35ff5c5c;sf=tgz
tar zxvf sphinx-28afb06.tar.gz
cd sphinx-28afb06
编译安装
phpize
./configure --with-sphinx=/usr/local/sphinx/libsphinxclient/ --with-php-config=/usr/bin/php-config
make && make install
echo "[Sphinx]" >> /etc/php.ini
echo "extension = sphinx.so" >> /etc/php.ini
重启php-fpm
service php-fpm reload
创建测试数据
CREATE TABLE IF NOT EXISTS `product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`created` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='商品数据表' AUTO_INCREMENT=1 ;
INSERT INTO `product` (`id`, `title`, `created`) VALUES
(1, 'OSPREY Kestrel小鹰登山包 双肩 运动包男户外轻量大容量背包户外', '2019-02-18 00:00:00'),
(2, 'Deuter多特ACT征途 55/65/75L透气运动户外旅行双肩背包登山包', '2019-02-17 00:00:00'),
(3, 'Gregory格里高利Baltoro B65 B75 B85男款户外徒步登山包双肩背包', '2019-02-16 00:00:00'),
(4, 'Gregory格里高利登山背包AMBER 28L/34L/44L/60L女款户外徒步', '2019-02-15 00:00:00'),
(5, 'Gregory格里高利 TARGHEE 滑雪登山包男女户外双肩背包', '2019-02-14 00:00:00'),
(6, 'OSPREY Talon 小鹰魔爪登山包双肩男 户外 徒步 超轻多功能旅行背包', '2019-02-13 00:00:00'),
(7, 'Salomon 萨洛蒙男女款户外防滑耐磨登山鞋 GTX防水透气徒步鞋', '2019-02-12 00:00:00'),
(8, 'LOWA爆款户外鞋防水RENEGADE GTX E男中帮徒步登山鞋L510952 047', '2019-02-11 00:00:00');
配置Sphinx
cd /usr/local/sphinx/etc
cp sphinx-min.conf.dist sphinx.conf
vim sphinx.conf
配置示例
#
# Minimal Sphinx configuration sample (clean, simple, functional)
#
source product_src
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass =
sql_db = test
sql_port = 3306 # optional, default is 3306
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type = OFF
sql_query = \
SELECT id, title, created \
FROM product
#sql_attr_uint = group_id
#sql_attr_timestamp = date_added
}
index product
{
source = product_src
path = /usr/local/sphinx/var/data/product
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
#charset_type = utf-8
#charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
ngram_len = 1
ngram_chars = U+4E00..U+9FBF, U+3400..U+4DBF, U+20000..U+2A6DF, U+F900..U+FAFF,U+2F800..U+2FA1F, U+2E80..U+2EFF, U+2F00..U+2FDF, U+3100..U+312F, U+31A0..U+31BF,U+3040..U+309F, U+30A0..U+30FF,U+31F0..U+31FF, U+AC00..U+D7AF, U+1100..U+11FF,U+3130..U+318F, U+A000..U+A48F, U+A490..U+A4CF
}
indexer
{
mem_limit = 128M
}
searchd
{
listen = 9312
listen = 9306:mysql41
log = /usr/local/sphinx/var/log/searchd.log
query_log = /usr/local/sphinx/var/log/query.log
read_timeout = 5
max_children = 30
pid_file = /usr/local/sphinx/var/log/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
workers = threads # for RT to work
binlog_path = /usr/local/sphinx/var/data
}
创建索引
/usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf product
Sphinx 2.2.11-id64-release (95ae9a6)
Copyright (c) 2001-2016, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file '/usr/local/sphinx/etc/sphinx.conf'...
indexing index 'product'...
WARNING: Attribute count is 0: switching to none docinfo
collected 8 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 8 docs, 761 bytes
total 0.004 sec, 154486 bytes/sec, 1624.03 docs/sec
total 3 reads, 0.000 sec, 0.6 kb/call avg, 0.0 msec/call avg
total 9 writes, 0.000 sec, 0.4 kb/call avg, 0.0 msec/call avg
后台运行sphinx
/usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf &
[1] 24939
[root@localhost etc]# Sphinx 2.2.11-id64-release (95ae9a6)
Copyright (c) 2001-2016, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file '/usr/local/sphinx/etc/sphinx.conf'...
listening on all interfaces, port=9312
listening on all interfaces, port=9306
precaching index 'product'
precached 1 indexes in 0.001 sec
php测试代码
<?php
$keyword = '登山包';
$cl = new SphinxClient();
$cl->SetServer('127.0.0.1', 9312);
#$cl->SetMatchMode(SPH_MATCH_EXTENDED);
$index="product";
$res = $cl->Query($keyword, $index);
$err = $cl->GetLastError();
print_r($res);
php search.php
Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => title
[1] => created
)
[attrs] => Array
(
)
[matches] => Array
(
[2] => Array
(
[weight] => 3323
[attrs] => Array
(
)
)
[3] => Array
(
[weight] => 3323
[attrs] => Array
(
)
)
[5] => Array
(
[weight] => 3323
[attrs] => Array
(
)
)
[6] => Array
(
[weight] => 3323
[attrs] => Array
(
)
)
[1] => Array
(
[weight] => 3319
[attrs] => Array
(
)
)
[4] => Array
(
[weight] => 2332
[attrs] => Array
(
)
)
)
[total] => 6
[total_found] => 6
[time] => 0
[words] => Array
(
[登] => Array
(
[docs] => 8
[hits] => 8
)
[山] => Array
(
[docs] => 8
[hits] => 8
)
[包] => Array
(
[docs] => 6
[hits] => 12
)
)
)
sphinxQL测试
mysql -h 127.0.0.1 -P 9306
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 2.2.11-id64-release (95ae9a6)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
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> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from product where match('登山包');
+------+
| id |
+------+
| 2 |
| 3 |
| 5 |
| 6 |
| 1 |
| 4 |
+------+
6 rows in set (0.00 sec)
mysql>
OK,至此sphinx安装和测试完毕!