基于php的Sphinx以及coreseek的全文搜索,中文分词的使用(二)
##1.续
在上一片文章
基于php的Sphinx以及coreseek的全文搜索,中文分词的使用(一) 中我们介绍了使用sphinx进行全文搜索,然后我们了解到sphinx对中文分词不是太智能,所以接下来我将带着大家一起来安装使用sphinx(coreseek)进行全文搜索中文分词
以下内容请在新环境进行配置测试
##2.coreseek介绍
coreseek是一款基于sphinx开源的搜索引擎,专门为用户提供免费的中文全文检索系统,coreseek被称为带有中文分词的sphinx,与sphinx不同的是coreseek增加了一个带有中文分司的词库,目前coreseek的官网已经不能访问,且该开源项目已不再维护,但这并不妨碍我们使用coreseek进行全文搜索,中文分词
3.安装
1.系统环境
centos7.2 +lnmp环境
2.下载coreseek并上传到服务器
下载链接:https://pan.baidu.com/s/1g74zH9tCE3lQH9UyQUnyyw 密码:o9gj
3.安装mmseg分词库
1. tar -zxvf coreseek-4.1-beta.tar.gz
2. cd coreseek-4.1-beta/mmseg-3.2.14/
3. ./bootstrap
4. echo $?
5. ./configure --prefix=/usr/local/mmseg
6. make
7. make install
4.安装coreseek
1. cd ../csft-4.1/
2. sh buildconf.sh ##警告可以忽略
3. ./configure --prefix=/usr/local/coreseek --without-python --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg/lib/ --with-mysql
4. make ##警告可忽略
5. make install
###可能会报的错
1. sh buildconf.sh 报错 automake: warnings are treated as errors
#解决办法 将configure.ac文件的
AM_INIT_AUTOMAKE([-Wall -Werror foreign]) 修改为
AM_INIT_AUTOMAKE([-Wall foreign])subdir-objects
2.make报错1
make[2]: *** [sphinxexpr.o] Error 1
make[2]: Leaving directory `/root/coreseek-4.1-beta/csft-4.1/src'
make[1]: *** [all] Error 2
make[1]: Leaving directory `/root/coreseek-4.1-beta/csft-4.1/src'
make: *** [all-recursive] Error 1
##解决办法
将src目录下sphinxexpr.cpp 文件的所有的
T val = ExprEval ( this-m_pArg, tMatch );
修改为
T val = this->ExprEval ( this-m_pArg, tMatch );
然后执行 make clean 在执行make
3.make 报错2
collect2: error: ld returned 1 exit status
make[2]: *** [indexer] Error 1
make[2]: Leaving directory `/root/coreseek-4.1-beta/csft-4.1/src'
make[1]: *** [all] Error 2
make[1]: Leaving directory `/root/coreseek-4.1-beta/csft-4.1/src'
##解决办法
编辑configure 文件 修改 #define USE_LIBICONV 1 的 1 修改为0
##4.配置coreseek 以及demo测试
4.1 coreseek配置
1. cd /usr/local/coreseek/etc
2. vim sphinx.conf.dist
3. ##配置基本与sphinx相同 不同的是
coreseek需要在索引attr_node 里添加
charset_type = zh_cn.utf-8
charset_dictpath = /usr/local/mmseg/etc/
添加这两行,意思是把中文分词加入到配置文件中
修改保存完执行 egrep -v '#|^$' sphinx.conf.dist >sphinx.conf
## sphinx.conf 的内容如下:
source node
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = 123456
sql_db = test
sql_sock = /tmp/mysql.sock
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF
sql_query = SELECT id,title,content,created FROM node
sql_field_string = title
sql_field_string = content
sql_field_string = created
sql_ranged_throttle = 0
}
index attr_node
{
source = node
path = /usr/local/coreseek/var/data/attr_node
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
charset_type = zh_cn.utf-8
charset_dictpath = /usr/local/mmseg/etc/
html_strip = 0
}
indexer
{
mem_limit = 128M
}
searchd
{
listen = 9312
listen = 9306:mysql41
log = /usr/local/coreseek/var/log/searchd.log
query_log = /usr/local/coreseek/var/log/query.log
read_timeout = 5
client_timeout = 300
max_children = 30
pid_file = /usr/local/coreseek/var/log/searchd.pid
max_matches = 1000
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
mva_updates_pool = 1M
max_packet_size = 8M
max_filters = 256
max_filter_values = 4096
max_batch_queries = 32
}
4.2 创建启动索引
1. cd ../bin
2. ./indexer --config /usr/local/coreseek/etc/sphinx.conf -all
3. ./searchd --config /usr/local/coreseek/etc/sphinx.conf
4.3 demo 编写测试
1. cd /home/wwwroot/default/
2. mkdir coreseek
3. cd
4. cd coreseek-4.1-bata/csft-4.1/api/
5. cp sphinxapi.php /home/wwwroot/default/coreseek/
6. cd /home/wwwroot/default/coreseek/
##创建并编辑 index.html
vim index.html
##写入以下代码
<!DOCTYPE html>
<html>
<head>
<title>欢迎使用sphinx!</title>
<meta charset="utf-8">
</head>
<body>
<h1>欢迎使用sphinx搜索</h1>
<form method="post" action="sphinxtest.php" >
<input type="text" name="centent"/>
<input type="submit" vlaue="搜索">
</form>
</body>
</html>
##然后保存退出
然后我们
新建并编辑一个 sphinxtest.php
vim sphinxtest.php
##写入以下代码
<?php
require('sphinxapi.php');
$cl = new SphinxClient ();
$q = $_POST['centent'];
$host = "localhost";
$port = 9312;
$index = "attr_node";
$cl->SetServer ( $host, $port );
$cl->SetConnectTimeout ( 1 );
$cl->SetArrayResult ( true );
//匹配查询词中的任意一个
$cl->SetMatchMode(SPH_MATCH_ANY);
$res = $cl->Query ( $q, $index );
echo '<pre>';
var_dump($res);
echo '</pre>';
?>
##保存并退出
4.4 开始测试
我们在搜索框输入 “阳光明媚”
显示如下:
array(10) {
["error"]=>
string(0) ""
["warning"]=>
string(0) ""
["status"]=>
int(0)
["fields"]=>
array(3) {
[0]=>
string(5) "title"
[1]=>
string(7) "content"
[2]=>
string(7) "created"
}
["attrs"]=>
array(3) {
["title"]=>
int(7)
["content"]=>
int(7)
["created"]=>
int(7)
}
["matches"]=>
array(3) {
[0]=>
array(3) {
["id"]=>
int(6)
["weight"]=>
string(1) "8"
["attrs"]=>
array(3) {
["title"]=>
string(33) "笔墨流芬芳,醉爱文字情"
["content"]=>
string(366) "文字,是一米阳光,温暖着我的心。阳光明媚,我在阳光里享受着它的融融暖意。文字,发着光,发着热,让我的灵魂不再四处漂游。文字,好暖,让我结冰的心湖慢慢融化。阳光,让早已心如死灰的心感受到从未有过的温暖。只要抓住那一米阳光,就等于抓住了一颗救命稻草"
["created"]=>
string(19) "2018-07-17 11:12:00"
}
}
[1]=>
array(3) {
["id"]=>
int(10)
["weight"]=>
string(1) "8"
["attrs"]=>
array(3) {
["title"]=>
string(24) "走在迷途的拾荒人"
["content"]=>
string(423) "时光就像一个美少女,在低眉浅笑中,就将有些人一些事隔到了光阴的对面。其实光阴从不曾厚过谁也不曾薄过谁,生活就是一种积累,你若储存的温暖多,你的生活就会阳光明媚,你若储存太多寒凉,你的生活就会阴云密布。放下烦恼与忧愁,带着最美的微笑出发,脚下路在,前方希望在,回眸处爱与温暖一直都在。"
["created"]=>
string(19) "2018-07-17 11:12:00"
}
}
[2]=>
array(3) {
["id"]=>
int(9)
["weight"]=>
string(1) "1"
["attrs"]=>
array(3) {
["title"]=>
string(15) "阳光的滋味"
["content"]=>
string(336) "明眸善睐,云袖轻舒,花影婆娑,夜鸟伏声。望月,终团圆。融化一季相思苦楚,泪凝妙目。开襟解怀,邀月畅饮。一季愁情尽遣。蘸墨狂书,满腹恩爱无度。羞愧了月里仙子,忧郁了河汉星辰。明月缱惓,夜色飘香。尝遍了相思苦涩,领略这相聚欢欣。"
["created"]=>
string(19) "2018-07-17 11:12:00"
}
}
}
["total"]=>
string(1) "3"
["total_found"]=>
string(1) "3"
["time"]=>
string(5) "0.003"
["words"]=>
array(2) {
["阳光"]=>
array(2) {
["docs"]=>
string(1) "3"
["hits"]=>
string(1) "7"
}
["明媚"]=>
array(2) {
["docs"]=>
string(1) "2"
["hits"]=>
string(1) "2"
}
}
}
在words里我们可以发现 阳光明媚被拆分成了两个词组 而不是像sphinx 切割成一个一个的字符
相比sphinx 又智能了许多!
到此sphinx以及coreseek的安装 配置以及基本使用就介绍到这里了 sphinx和coreseek 还有更多其他功能等例如分页 排序 指定查询等 功能十分强大
如在安装中遇到什么问题请在下方留言,我看到一定会帮大家解决问题的。
非特殊说明,本博所有文章均为博主原创。
如若转载,无需和我联系,但请注明出处:https://www.yangpanyao.com/archives/73.html