sphinx(四)centos7安装sphinx3.3.1
Sphinx使用的版本我是一点点增高的。
Coreseek3.2是基于sphinx0.9开发的。
Sphinx-for-chinese是基于sphinx2.3.1开发的。
我这里尝试一下sphinx的新版本sphinx3.3.1,当然,这个只做为了解就好,我也只是尝试一下。
百度上有提供sphinx+swcs中文分词实现中文全文检索的样例,我没有尝试。
有兴趣,请移步百度。
1:安装sphinx
Sphinx官网:sphinxsearch.com/
安装的文件包也是在官网中。
Sphinx3.3.1版本是不需要编译安装的,下载下来,解压,直接就能用。 这个和我目前正在使用的sphinx-for-chinese 是有不同的。
2:创建索引报错
创建单个索引:
using config file '/usr/local/sphinx/etc/sphinx.conf'...
indexing index 'test1'...
ERROR: index 'test1': sql_connect: failed to load libmysqlclient (or libmariadb) (DSN=mysql://root:***@localhost:3306/test).
复制代码
解决:
查找libmysqlclient
find / -name 'libmysqlclient*'
/usr/lib64/mysql/libmysqlclient.so.18
/usr/lib64/mysql/libmysqlclient.so.18.0.0
/usr/local/mariadb/lib/libmysqlclient_r.so
/usr/local/mariadb/lib/libmysqlclient.a
/usr/local/mariadb/lib/libmysqlclient.so
/usr/local/mariadb/lib/libmysqlclient_r.a
/usr/local/download/mariadb-10.5.6/debian/libmysqlclient18.install
/usr/local/download/mariadb-10.5.6/libmariadb/libmariadb/libmysqlclient_r.so
/usr/local/download/mariadb-10.5.6/libmariadb/libmariadb/libmysqlclient.a
/usr/local/download/mariadb-10.5.6/libmariadb/libmariadb/libmysqlclient.so
/usr/local/download/mariadb-10.5.6/libmariadb/libmariadb/libmysqlclient_r.a
复制代码
创建软连接:
ln -s /usr/local/mariadb/lib/libmysqlclient.so /usr/lib/libmysqlclient.so
vim /etc/ld.so.conf
复制代码
增加配置:
/usr/local/mariadb/bin/mysql # 你的数据库执行文件
复制代码
再次创建索引成功:
/usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf --all --rotate
/usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf --stop #停止服务
复制代码
然后重启
/usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf #启动服务
复制代码
3:停止sphinx服务报错:
/usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf --stop
Sphinx 3.3.1 (commit b72d67b)
Copyright (c) 2001-2020, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file '/usr/local/sphinx/etc/sphinx.conf'...
FATAL: stop: pid file '/usr/local/sphinx/log/searchd.pid' does not exist or is not readable
shutdown complete
复制代码
重启服务器再启动sphinx就可以了。
4:sphinx配置文件
Sphinx.conf
#
# Minimal Sphinx configuration sample (clean, simple, functional)
#
source src1
{
type = mysql
sql_host = localhost
sql_user = mysql
sql_pass =
sql_db = test
sql_port = 3306 # optional, default is 3306
sql_query = \
SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
FROM documents
sql_attr_uint = group_id
sql_attr_uint = date_added
}
index test1
{
source = src1
path = /usr/local/sphinx/data/test1
}
index testrt
{
type = rt
rt_mem_limit = 128M
path = /usr/local/sphinx/data/testrt
rt_field = title
rt_field = content
rt_attr_uint = gid
}
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
workers = threads # for RT to work
binlog_path = /usr/local/sphinx/data
}
复制代码
5:创建数据表并写入测试数据
Sphinx为我们提供了一个测试的sql文件
文件在安装目录下etc目录中(example.sql),下边是文件内容:
DROP TABLE IF EXISTS test.documents;
CREATE TABLE test.documents
(
id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
group_id INTEGER NOT NULL,
group_id2 INTEGER NOT NULL,
date_added DATETIME NOT NULL,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL
);
REPLACE INTO test.documents ( id, group_id, group_id2, date_added, title, content ) VALUES
( 1, 1, 5, NOW(), 'test one', 'this is my test document number one. also checking search within phrases.' ),
( 2, 1, 6, NOW(), 'test two', 'this is my test document number two' ),
( 3, 2, 7, NOW(), 'another doc', 'this is another group' ),
( 4, 2, 8, NOW(), 'doc number four', 'this is to test groups' );
DROP TABLE IF EXISTS test.tags;
CREATE TABLE test.tags
(
docid INTEGER NOT NULL,
tagid INTEGER NOT NULL,
UNIQUE(docid,tagid)
);
INSERT INTO test.tags VALUES
(1,1), (1,3), (1,5), (1,7),
(2,6), (2,4), (2,2),
(3,15),
(4,7), (4,40);
复制代码
6:PHP调用sphinx
Demo.php
<?php
require("/usr/local/sphinx/api/sphinxapi.php");
$sphinx = new Sphinxclient();
$sphinx->setServer('127.0.0.1',9312);
$keyword='test';//要搜索的关键字
$index= 'test1';//索引名称
//查询出关键字所在的主键ID
$sphinx->_limit=2000;
$res= $sphinx->Query($keyword, $index);
// $res = $sphinx->Query($keyword, '*');
if(isset($res['matches'])){
$ids = array_keys($res['matches']);
$ids = implode(',',$ids);
}else{
print_r('内容不存在');exit;
}
$sql = "SELECT * from documents where id in ($ids)";
$mysqli_con= mysqli_connect('127.0.0.1','mysql', '', 'test', '3306', '/usr/local/mariadb/tmp/mysql.sock');
$res = mysqli_query($mysqli_con, $sql);
while($row = mysqli_fetch_assoc($res)){
$data[] = $row;
}
foreach($data as $key=>$v)
{
$v = str_replace($keyword, "<font color='red'>{$keyword}</font>", $v);
$data[$key]= $v;
}
print_r($data);
复制代码
这篇的细节可能写的不太清楚,具体使用流程可以参照前两篇的使用流程。
相关文章