一个文档包含了安装、增量备份、扩展、api调用示例,省去了查找大量文章的时间。

搭建coreseek(sphinx+mmseg3)安装

[第一步] 先安装mmseg3

cd /var/install
wget http://www.coreseek.cn/uploads/csft/4.0/coreseek-4.1-beta.tar.gz
tar zxvf coreseek-4.1-beta.tar.gz cd coreseek-4.1-beta
cd mmseg-3.2.
./bootstrap
./configure --prefix=/usr/local/mmseg3
make && make install 遇到的问题:
error: cannot find input file: src/Makefile.in
或者遇到其他类似error错误时... 解决方案:
依次执行下面的命令,我运行'aclocal'时又出现了错误,解决方案请看下文描述 yum -y install libtool aclocal
libtoolize --force
automake --add-missing
autoconf
autoheader
make clean

安装好'libtool'继续从'aclocal'开始执行上面提到的一串命令,执行完后再运行最开始的安装流程即可。

[第二步] 安装coreseek

##安装coreseek
$ cd csft-3.2. 或者 cd csft-4.0. 或者 cd csft-4.1
$ sh buildconf.sh #输出的warning信息可以忽略,如果出现error则需要解决
$ ./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
##如果提示mysql问题,可以查看MySQL数据源安装说明 http://www.coreseek.cn/product_install/install_on_bsd_linux/#mysql
$ make && make install
$ cd .. ##命令行测试mmseg分词,coreseek搜索(需要预先设置好字符集为zh_CN.UTF-,确保正确显示中文)
$ 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 网络搜索

出现这个 xmlpipe2 support NOT compiled in. To use xmlpipe2, install missing XML libra  错误

执行以下命令:

yum -y install expat expat-devel

依次安装后,从新编译coreseek,然后再生成索引,就可以通过了。

结果如下:

Coreseek Fulltext 4.1 [ Sphinx 2.0.-dev (r2922)]
Copyright (c) -,
Beijing Choice Software Technologies Inc (http://www.coreseek.com) using config file 'etc/csft.conf'...
index 'xml': query '网络搜索 ': returned matches of total in 0.000 sec displaying matches:
. document=, weight=, published=Thu Apr :: , author_id= words:
. '网络': documents, hits
. '搜索': documents, hits

下面开始sphinx与mysql的配置

创建sphinx统计表,在coreseek_test库中执行。

CREATE TABLE sph_counter
(
counter_id INTEGER PRIMARY KEY NOT NULL,
max_doc_id INTEGER NOT NULL
);

创建配置sphinx与mysql的配置文件

# vi /usr/local/coreseek/etc/csft_mysql.conf
#MySQL数据源配置,详情请查看:http://www.coreseek.cn/docs/coreseek_4.1-sphinx_2.0.1-beta.html#conf-reference

#源定义
source main
{
type = mysql sql_host = localhost
sql_user = root
sql_pass =
sql_db = coreseek_test
sql_port =
sql_query_pre = SET NAMES utf8
sql_query_pre = REPLACE INTO sph_counter SELECT ,MAX(id) FROM hr_spider_company;
sql_query = SELECT * FROM hr_spider_company WHERE id<=( SELECT max_doc_id FROM sph_counter WHERE counter_id= )
#sql_query第一列id需为整数
#title、content作为字符串/文本字段,被全文索引
sql_attr_uint = id #从SQL读取到的值必须为整数
sql_attr_uint = from_id #从SQL读取到的值必须为整数,不支持全文检索
sql_attr_uint = link_id #从SQL读取到的值必须为整数,不支持全文检索
sql_attr_uint = add_time #从SQL读取到的值必须为整数,不支持全文检索
sql_field_string = link_url #字符串字段(可全文搜索,可返回原始文本信息)
sql_field_string = company_name #字符串字段(可全文搜索,可返回原始文本信息)
sql_field_string = type_name #字符串字段(可全文搜索,可返回原始文本信息)
sql_field_string = trade_name #字符串字段(可全文搜索,可返回原始文本信息)
sql_field_string = email #字符串字段(可全文搜索,可返回原始文本信息)
sql_field_string = description #字符串字段(可全文搜索,可返回原始文本信息) sql_query_info_pre = SET NAMES utf8 #命令行查询时,设置正确的字符集
sql_query_info = SELECT id,from_id,link_id,company_name,type_name,trade_name,address,description, FROM_UNIXTIME(add_time) AS add_time FROM hr_spider_company WHERE id=$id #命令行查询时,从数据库读取原始数据信息
} source delta : main
{
sql_query_pre = SET NAMES utf8
sql_query = SELECT * FROM hr_spider_company WHERE id>( SELECT max_doc_id FROM sph_counter WHERE counter_id= )
sql_query_post_index = REPLACE INTO sph_counter SELECT ,MAX(id) FROM hr_spider_company
} #index定义
index main
{
source = main #对应的source名称
path = /usr/local/coreseek/var/data/mysql #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
docinfo = extern
mlock =
morphology = none
min_word_len =
html_strip = #中文分词配置,详情请查看:http://www.coreseek.cn/products-install/coreseek_mmseg/
charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux环境下设置,/符号结尾
charset_type = zh_cn.utf-
} index delta : main
{
source = delta
path = /usr/local/coreseek/var/data/delta
} #全局index定义
indexer
{
mem_limit = 128M
} #searchd服务定义
searchd
{
listen =
read_timeout =
max_children =
max_matches =
seamless_rotate =
preopen_indexes =
unlink_old =
pid_file = /usr/local/coreseek/var/log/searchd_mysql.pid #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
log = /usr/local/coreseek/var/log/searchd_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
query_log = /usr/local/coreseek/var/log/query_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
binlog_path = #关闭binlog日志
}

我的测试表名为hr_spider_company,你只需要根据实际需求更改为自己的表名即可。

调用命令列表:

启动后台服务(必须开启)

# /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft_mysql.conf

执行索引(查询、测试前必须执行一次)

/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf --all --rotate

执行增量索引

/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf delta --rotate

合并索引

/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf --merge main delta --rotate --merge-dst-range deleted  

(为了防止多个关键字指向同一个文档加上--merge-dst-range deleted 0 0)

后台服务测试

# /usr/local/coreseek/bin/search -c /usr/local/coreseek/etc/csft_mysql.conf  aaa

关闭后台服务

# /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft_mysql.conf --stop

自动化命令:

crontab -e
*/ * * * * /bin/sh /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf delta --rotate
*/ * * * * /bin/sh /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf --merge main delta --rotate --merge-dst-range deleted
* * * /bin/sh /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf --all --rotate

以下任务计划的意思是:每隔一分钟执行一遍增量索引,每五分钟执行一遍合并索引,每天1:30执行整体索引。

Sphinx扩展安装安装

Coreseek官方教程中建议php使用直接include一个php文件进行操作,事实上php有独立的sphinx模块可以直接操作
coreseek(coreseek就是sphinx!)已经进入了php的官方函数库,而且效率的提升不是一点点!但php模块依赖于
libsphinxclient包。

[第一步] 安装依赖libsphinxclient

# cd /var/install/coreseek-4.1-beta/csft-4.1/api/libsphinxclient/
# ./configure --prefix=/usr/local/sphinxclient configure: creating ./config.status
config.status: creating Makefile
config.status: error: cannot find input file: Makefile.in #报错configure失败 //处理configure报错
编译过程中报了一个config.status: error: cannot find input file: src/Makefile.in这个的错误,然后运行下列指令再次编译就能通过了:
# aclocal
# libtoolize --force
# automake --add-missing
# autoconf
# autoheader
# make clean //从新configure编译
# ./configure # make && make install

[第二步] 安装sphinx的PHP扩展

http://pecl.php.net/package/sphinx
# wget http://pecl.php.net/get/sphinx-1.3.0.tgz
# tar zxvf sphinx-1.3..tgz
# cd sphinx-1.3.
# phpize
# ./configure --with-php-config=/usr/bin/php-config --with-sphinx=/usr/local/sphinxclient
# make && make install
# cd /etc/php.d/
# cp gd.ini sphinx.ini
# vi sphinx.ini extension=sphinx.so # service php-fpm restart

打开phpinfo看一下是否已经支持了sphinx模块。

php调用sphinx示例:

<?php
$s = new SphinxClient;
$s->setServer("127.0.0.1", 9312); $s->setMatchMode(SPH_MATCH_PHRASE);
$s->setMaxQueryTime(30);
$res = $s->query("宝马",'main'); #[宝马]关键字,[main]数据源source
$err = $s->GetLastError();
var_dump(array_keys($res['matches']));
echo "<br>"."通过获取的ID来读取数据库中的值即可。"."<br>"; echo '<pre>';
var_dump($res);
var_dump($err);
echo '</pre>';

调用示例二:支持分页

<?php
header("Content-type: text/html; charset=utf-8");
require("./sphinxapi.php");
$s = new SphinxClient;
$s->setServer("192.168.252.132", 9312); //SPH_MATCH_ALL, 匹配所有查询词(默认模式); SPH_MATCH_ANY, 匹配查询词中的任意一个; SPH_MATCH_EXTENDED2, 支持特殊运算符查询
$s->setMatchMode(SPH_MATCH_ALL);
$s->setMaxQueryTime(30); //设置最大搜索时间
$s->SetArrayResult(false); //是否将Matches的key用ID代替
$s->SetSelect ( "*" ); //设置返回信息的内容,等同于SQL
$s->SetRankingMode(SPH_RANK_BM25); //设置评分模式,SPH_RANK_BM25可能使包含多个词的查询的结果质量下降。
//$s->SetSortMode(SPH_SORT_EXTENDED); //发现增加此参数会使结果不准确
//$s->SetSortMode(SPH_SORT_EXTENDED,"from_id asc,id desc"); //设置匹配项的排序模式, SPH_SORT_EXTENDED按一种类似SQL的方式将列组合起来,升序或降序排列。
$weights = array ('company_name' => 20);
$s->SetFieldWeights($weights); //设置字段权重
$s->SetLimits ( 0, 30, 1000, 0 ); //设置结果集偏移量 SetLimits (便宜量,匹配项数目,查询的结果集数默认1000,阀值达到后停止)
//$s->SetFilter ( $attribute, $values, $exclude=false ); //设置属性过滤
//$s->SetGroupBy ( $attribute, $func, $groupsort="@group desc" ); //设置分组的属性
$res = $s->query('@* "汽车"','main','--single-0-query--'); #[宝马]关键字,[news]数据源source //代码高亮
$tags = array();
$tags_name = array();
foreach($res['matches'] as $key=>$value){
$tags[] = $value['attrs'];
$company_name[] = $value['attrs']['company_name'];
$description[] = $value['attrs']['description'];
}
$company_name = $s->BuildExcerpts ($company_name, 'main', '汽车', $opts=array() ); //执行高亮,这里索引名字千万不能用*
$description = $s->BuildExcerpts ($description, 'main', '汽车', $opts=array() ); //执行高亮,这里索引名字千万不能用*
foreach($tags as $k=>$v)
{
$tags[$k]['company_name'] = $company_name[$k]; //高亮后覆盖
$tags[$k]['description'] = $description[$k]; //高亮后覆盖
} // 高亮后覆盖
$i = 0;
foreach($res['matches'] as $key=>$value){
$res['matches'][$key] = $tags[$i];
$i++;
} $err = $s->GetLastError(); echo '<pre>';
var_export($res);
var_export($err);
echo '</pre>';

还有很对地方需要参考:http://www.coreseek.cn/docs/coreseek_4.1-sphinx_2.0.1-beta.html#api-reference

搭建coreseek(sphinx+mmseg3)详细安装配置+php之sphinx扩展安装+php调用示例(转)的更多相关文章

  1. 阿里云服务器Linux CentOS安装配置(八)nginx安装、配置、域名绑定

    阿里云服务器Linux CentOS安装配置(八)nginx安装.配置.域名绑定 1.安装nginx yum -y install nginx 2.启动nginx service nginx star ...

  2. 阿里云服务器Linux CentOS安装配置(四)yum安装tomcat

    阿里云服务器Linux CentOS安装配置(四)yum安装tomcat 1.yum -y install tomcat  执行命令后,会帮你把jdk也安装好 2.tomcat安装目录:/var/li ...

  3. 阿里云服务器Linux CentOS安装配置(三)yum安装mysql

    阿里云服务器Linux CentOS安装配置(三)yum安装mysql 1.执行yum安装mysql命令:yum -y install mysql-server mysql-devel 2.启动mys ...

  4. 阿里云服务器Linux CentOS安装配置(二)yum安装svn

    阿里云服务器Linux CentOS安装配置(二)yum安装svn 1.secureCRT连接服务器 2.先创建一个文件夹,用来按自己的习惯来,用来存放数据 mkdir /data 3.yum安装sv ...

  5. MySQL 5.7.33 超级详细下载安装配置测试教程(可以安装成功版)

    目录 1.引言及注意事项 (1) 引言: (2) 注意: 2.MySQL下载 3.配置环境变量 4.配置my.ini文件(重点) 5.安装MySQL(重点) 6.设置密码 7.测试MySQL是否安装成 ...

  6. inux redis 安装配置, 以及redis php扩展

    一,什么是redis redis是一个key-value存储系统. 和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset ...

  7. X2安装配置keras环境(包含matplotlib安装)

    https://blog.csdn.net/jonado13/article/details/83933453 1.安装pipapt install python3-pipE: Could not o ...

  8. CDH6.2安装配置第二篇:CDH安装的前期配置

    本篇介绍cdh安装之前需要的一些必要配置,当然这些配置也可以用shell脚本来配置.在安装之前请先配置好yum源,在文中用的统一都是阿里源.在安装的时候,要确保主机的内存是4G以上,要不然会无限重启c ...

  9. Kali Linux 下安装配置MongoDB数据库 ubuntu 下安装配置MongoDB源码安装数据库

    Kali Linux 下安装配置MongoDB数据库   1.下载mongodb.tgz 压缩包: 2.解压到:tar -zxvf mongodb.tgz /usr/local/mongodb 3.创 ...

随机推荐

  1. Linux下使用SSH远程执行命令方法收集

    说明:可以做SSH免密登录之后执行,这样可以省去每次执行输入密码的提示. 对于简单的命令: 如果是简单执行几个命令,则: ssh user@remoteNode "cd /home ; ls ...

  2. Android中MVC模型(复合模式)

    mvc是model,view,controller的缩写,mvc包括三个部分: 1.模型(model)对象:是应用程序的主体部分,全部的业务逻辑都应该写在该层. 2.视图(view)对象:是应用程序中 ...

  3. Unable to load DLL 'opencv_core290'

    问题: In my winforms application I need to use some Emgu.CV libraries (I have installed Emgu 2.9). Pro ...

  4. Springboot集成Jedis + Redisson(已自测)

    原文:https://blog.csdn.net/c_zyer/article/details/79415728 本文主要跟大家分享在Springboot中集成Jedis和Redisson的方法.为什 ...

  5. FAQ:枚举和常规的值,到底哪种更符合程序使用?

    问: 枚举和常规的值,到底哪种更符合程序使用? 答: 肯定是根据不同的场景,做出不同的选择. 如果是不同的值需要不同的逻辑,肯定是枚举好一些.如果只是表示某个取值范围且这个范围会动态变化,用常规的值 ...

  6. arm交叉编译opencv

    问题:undefined reference to `pthread_spin_init'解:修改CMakeCache.txt,CMAKE_EXE_LINKER_FLAGS原来为空,加上-lpthre ...

  7. C#转义字符[转]

    C#转义字符: 一种特殊的字符常量 以反斜线"\"开头,后跟一个或几个字符 具有特定的含义,不同于字符原有的意义,故称“转义”字符. 主要用来表示那些用一般字符不便于表示的控制代码 ...

  8. Unity3d笔试题大全

    1.       [C#语言基础]请简述拆箱和装箱. 答: 装箱操作: 值类型隐式转换为object类型或由此值类型实现的任何接口类型的过程. 1.在堆中开辟内存空间. 2.将值类型的数据复制到堆中. ...

  9. Linux下Anaconda的安装使用与卸载及问题解决

    1. 安装 到官网下载对应的版本文件:Download Anaconda Now! 下载完之后,在终端输入: bash 下载好的文件 整个过程点几下回车就好了.但是到最后一步,会提示是否把anacon ...

  10. 【帧动画总结】AnimationDrawable Frame

    Drawable Animation 开发者文档 位置:/sdk/docs/guide/topics/graphics/drawable-animation.html Drawable animati ...