作者:jAne | 来源:互联网 | 2023-05-18 12:13
下载sphinx3.1.1wgethttp:sphinxsearch.comfilessphinx-3.1.1-612d99f-linux-amd64.tar.gz解压 tarzx
- 下载sphinx3.1.1
- 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
- 改名 sphinx 并移动到 /usr/local/
- mv sphinx-3.1.1 sphinx
- mv sphinx /usr/local/
- 到sphinx目录下创建 data,log文件夹,并赋777权限
- cd /usr/local/sphinx
- mkdir data
- chmod -r 777 data
- mkdir log
- chmod -r 777 log
- 在/usr/local/sphinx/etc 编写 sphinx.conf 配置文件
- cd /usr/local/sphinx/etc
- vim sphinx.conf
#
# minimal sphinx configuration sample (clean, simple, functional)
#
source src1
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = root
sql_db = test
sql_port = 3306 # optional, default is 3306
sql_query_pre = set names utf8
sql_query = \
select id, group_id, unix_timestamp(date_added) as date_added, title, content \
from documents
sql_attr_uint = group_id
sql_attr_timestamp = date_added
}
index test1
{
source = src1
path = /usr/local/sphinx/data
min_word_len = 1
ngram_len = 1
ngram_chars = u+3000..u+2fa1f
}
indexer
{
mem_limit = 128m
}
searchd
{
listen = 9312
listen = 9306:mysql41
log = /usr/local/sphinx/log/searchd.log
query_log = /usr/local/sphinx/log/query.log
read_timeout = 5
max_children = 30
pid_file = /usr/local/sphinx/log/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
binlog_path = /usr/local/sphinx/data
}
- 在test数据库中 运行/usr/local/sphinx/etc目录下的example.sql文件
- 进入mysql
- use test;
- source /usr/local/sphinx/etc/example.sql
- 添加索引
- /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf test1
- 运行sphinx
- /usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf
- php操作sphinx
- 下载sphinxapi.php 自带的类文件缺少function
https://files.cnblogs.com/files/wxzxc/sphinxclient.zip
$sphinx = new sphinxclient();
$q = $_get['key'] ?? 'test'; //搜索关键字
$sql = "";
$host = "127.0.0.1";
$port = 9312;
$index = "*";
$sphinx->setserver($host, $port);
$sphinx->setconnecttimeout(10);
$sphinx->setarrayresult(true);
$sphinx->setmatchmode(sph_match_all);
$res = $sphinx->query($q, $index);
print_r($res);
运行结果如下:
结束,记得数据库数据改变后需重新生成索引