1、先安装环境:
yum install make gcc gcc++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2-devel
expat-devel
注: 检测以上软件是否安装,如果没有请确保安装;否则无法正常安装Coreseek-3.2.14
2、开始安装:
cd /usr/local/src #把安装包放到此处
wget http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gz #下载CoreSeek,这里
注意一下,这个路径现在暂时已经用不了(至少我写这篇博客的时候用不了),但是毕竟这是官网,还是要
放一下的。我的网盘中有该软件 http://url.cn/2I77CON ,大家可以先下载到本地,然后再用 Filezilla
等软件上传到服务器即可。
tar -zxvf coreseek-3.2.14.tar.gz
cd coreseek-3.2.14 #解压出来的文件夹
##############安装 mmseg #################
cd mmseg-3.2.14
./bootstrap #输出的warning信息可以忽略,如果出现error则需要解决,一般不会出现错误
./configure --prefix=/usr/local/mmseg3 #指定 mmseg 的安装目录
make && make install
#############安装 csft-3.2.14 ###############
cd /usr/local/src/csft-3.2.14
#这里我们要修改 src/sphinxexpr.cpp 文件,将该文件中的 1013、1047、1080 行的 ExprEval 改为
this->ExprEval(这里的行数跟网上的教程说的行数不一样,可能是版本的问题吧,但是如果你用的是我提
供的版本,该行数是正确的),懂点 C++ 的同学也可以自己看看 ExprEval 错在哪里。
#改完上面所说的问题后:
sh buildconf.sh
./configure --prefix=/usr/local/coreseek --without-unixodbc --with-mmseg --with-mmseg-
includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --with-mysql
make && make install
在安装的过程中,只要不提示错误 error,那么其他的如 warning 信息都是可以忽略的。
至此我们已经成功的将中文检索引擎安装到我们服务器上来了。
./configure --prefix=/usr/local/coreseek --with-mmseg-includes=/usr/local/mmseg/include/mmseg/
--with-mmseg-libs=/usr/local/mmseg/lib/
make && make install
测试:
cd testpack
cat var/test/test.xml #此时应该正确显示中文
/usr/local/mmseg3/bin/mmseg -d /usr/local/mmseg3/etc var/test/test.xml #整篇文章进行分词
/usr/local/coreseek/bin/indexer -c etc/csft.conf --all #创建索引
/usr/local/coreseek/bin/search -c etc/csft.conf 网络搜索 #搜索关键字 网络搜索
/usr/local/coreseek/bin/searchd -c etc/csft.conf #正常开启搜索服务
/usr/local/coreseek/bin/searchd -c etc/csft.conf --stop #如要停止搜索服务
/usr/local/coreseek/bin/indexer -c etc/csft.conf --all --rotate #如要已启动服务,要更新索引
编译添加php7下的sphinx扩展
wget http://pecl.php.net/get/sphinx-1.3.3.tgz
tar zvxf sphinx-1.3.3.tgz
cd sphinx-1.3.3/
/usr/local/php71/bin/phpize
./configure --with-php-config=/usr/local/php71/bin/php-config
提示出错:
checking for libsphinxclient headers in default path... not found
configure: error: Cannot find libsphinxclient headers
cd /usr/local/src/coreseek-3.2.14/csft-3.2.14/api/libsphinxclient
./configure
make && make install
安装完后继续编译sphinx扩展
编译出错,下载匹配php7的最新扩展
http://git.php.net/?p=pecl/search_engine/sphinx.git;a=snapshot;h=refs/heads/php7;sf=tgz
重新编译安装
vim /usr/local/php71/lib/php.ini
加入extension = sphinx.so
编写php代码测试:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>sphinx</title>
</head>
<body>
<div style="margin:20px;">
<h3>sphinx分词检索</h3>
<form action="" method="post">
关键字:<input type="text" name="keyword">
<br>
<input type="submit" value="检索">
</form>
<?php
// require_once 'sphinxapi.php';
echo '<h3>检索信息:</h3>';
$keyword = $_POST['keyword'];
//访问Sphinx获得主键值
$sphinx = new SphinxClient();
$sphinx->SetServer('localhost',9312);
$sphinx->SetConnectTimeout(10);//设置连接的超时时间
$sphinx->SetMaxQueryTime(30);//设置最大的查询时间
//$sphinx->SetArrayResult(true);//设置Sphinx返回结果的类型
/*
* 切词方式
* SPH_MATCH_ALL:切词。所有词完全匹配
* SPH_MATCH_ANY:切词。只要有一个词匹配
* SPH_MATCH_PHRASE:不切词
*/
$sphinx->SetMatchMode(SPH_MATCH_ANY);//设置切词方式
$index = 'attr_node';
//查询索引文件
$result = $sphinx->query($keyword, $index);
if (isset($result['matches'])) {
$ids = join(',', array_keys($result['matches']));
try{
$pdo = new PDO('mysql:host=127.0.0.1;dbname=coreseek', 'root', 'root');
$res = $pdo->query('select * from node where id in ('.$ids.')');
$data = $res->fetchAll(PDO::FETCH_ASSOC);
$opts = [
'before_match' => '<font style="color:red;font-weight:bold">',
'after_match' => '</font>'
];
foreach ($data as $row)
{
$light = $sphinx->buildExcerpts($row, $index, $keyword, $opts);
var_dump($light);die();
echo '编号:'.$light[0].'<br>';
echo '标题:'.$light[1].'<br>';
echo '内容:'.$light[2].'<br>';
echo '创建时间:'.$light[3].'<hr>';
}
} catch (PDOException $e){
echo $e->getMessage();
}
} else {
echo '没有匹配到数据@';
}
?>
</div>
</body>
</html>
参考文档:
https://zhuanlan.zhihu.com/p/40040761