filts 发表于 2018-12-19 12:56:31

sphinx+coreseek+php扩展

  ##################### 一mariadb 安装 ##############
                  使用的是yum安装 略
#####################################################
##################### 二php 环境搭建 ##############
                  使用的是yum安装 略
#####################################################
##################### 三 mmseg3安装##############

1) 安装autoconf
   # tar -jxvf autoconf-2.64.tar.bz2
   # cd autoconf-2.64
   # ./configure
   # make
   # make install
2) 安装mmseg
   # tar -zxvf coreseek-3.2.14.tar.gz
   # cd coreseek-3.2.14
   # cd mmseg-3.2.14
   # ./bootstrap# 输出warning信息可以忽略 如果输出error信息则需要解决
   # ./configure --prefix=/usr/local/mmseg3   # 出现一个错误 config.status: error: cannot find input file: 'src/Makefile.in' 则 升级安装一些软件
   # yum -y install gcc gcc-c++ libtool autoconf automake imake libxml2-devel expat-devel # 升级安装一些软件
   # aclocal
   # libtoolize --force
   # automake --add-missing
   # autoconf
   # autoheader
   # make clean
   # ./configure --prefix=/usr/local/mmseg3
   # 重新编译
   # make
   # make install  ################################################################
#################### 四 安装coreseek #################

# cd ../csft-3.2.14/
   # 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
   # make && make install  ###########################################################
#################### 五 测试coreseek 分词 #################

# cd testpack/
   # catvar/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 网络搜索
   words:
   1. '网络': 1 documents, 1 hits
   2. '搜索': 2 documents, 5 hits
   # 返回上面的内容说明拆词成功  ############################################################
################## 六 配置coreseek数据源mysql ######################

1) 创建测试用数据库和数据表,做为coreseek的测试用的数据源
   MariaDB [(none)]> create database test;
    Query OK, 1 row affected (0.10 sec)
   MariaDB [(none)]> use test;
   MariaDB > source /root/v9_news.sql;# 其中有两万多条数据 供测试
   MariaDB > show create table v9_news\G;
*************************** 1. row ***************************
       Table: v9_news
Create Table: CREATE TABLE `v9_news` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`catid` smallint(5) unsigned NOT NULL DEFAULT '0',
`typeid` smallint(5) unsigned NOT NULL,
`title` varchar(120) NOT NULL DEFAULT '',
`style` char(24) NOT NULL DEFAULT '',
`thumb` varchar(100) NOT NULL DEFAULT '',
`keywords` varchar(60) NOT NULL DEFAULT '',
`description` mediumtext NOT NULL,
`posids` tinyint(1) unsigned NOT NULL DEFAULT '0',
`url` char(100) NOT NULL,
`listorder` tinyint(3) unsigned NOT NULL DEFAULT '0',
`status` tinyint(2) unsigned NOT NULL DEFAULT '1',
`sysadd` tinyint(1) unsigned NOT NULL DEFAULT '0',
`islink` tinyint(1) unsigned NOT NULL DEFAULT '0',
`username` char(20) NOT NULL,
`inputtime` int(10) unsigned NOT NULL DEFAULT '0',
`updatetime` int(10) unsigned NOT NULL DEFAULT '0',
`subtitle` varchar(120) NOT NULL DEFAULT '',
`lable` varchar(150) NOT NULL DEFAULT '',
`userid` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `status` (`status`,`listorder`,`id`),
KEY `listorder` (`catid`,`status`,`listorder`,`id`),
KEY `catid` (`catid`,`status`,`id`)
) ENGINE=MyISAM AUTO_INCREMENT=29471 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)  2) 修改coreseek 的配置文件

# vim /usr/local/coreseek/etc/csft_mysql.conf
   # 源定义
source news_src
{
          type                                    = mysql #####数据源类型
          sql_host                              = localhost ######mysql主机
          sql_user                              = root ########mysql用户名
          sql_pass                              = 123456 ############mysql密码
          sql_db                                  = test #########mysql数据库名
          sql_port                              = 3306###########mysql端口
          sql_query_pre   = SET NAMES UTF8###mysql检索编码,特别要注意这点,很多人中文检索不到是数据库的编码是GBK或其他非UTF8
          sql_query                               = \
                  SELECT id, catid, keywords, title, description \
                  FROM v9_news ####### 获取数据的sql
          sql_attr_uint                   = id ######## 无符号整数属性
          sql_attr_uint                   = catid
          sql_query_info          = SELECT * FROM v9_news WHERE id=$id ######### 用于命令界面端(CLI)调用的测试
}
####### 索引定义 ##########
index news
{
source = news_src ####声明索引源
path                  = /usr/local/coreseek/var/data/news #######索引文件存放路径及索引的文件名
docinfo = extern ##### 文档信息存储方式
mlock = 0 ###缓存数据内存锁定
morphology = none#### 形态学(对中文无效)
min_word_len = 1#### 索引的词最小长度
html_strip            = 0
charset_dictpath   = /usr/local/mmseg3/etc/#BSD、Linux环境下设置,/符号结尾 分词库路径定义
charset_type      = zh_cn.utf-8 #####数据编码
}
######### 全局index定义 ##############
indexer
{
   mem_limit = 128M ####### 内存限制
}
###### searchd服务定义 ###
searchd
{
   # listen            = 9312   ### 监听端口,在此版本开始,官方已在IANA获得正式授权的9312端口,以前版本默认的是3312
   log = /usr/local/coreseek/var/log/searchd.log
   query_log = /usr/local/coreseek/var/log/query.log ### 客户端查询日志,笔者注:若欲对一些关键词进行统计,可以分析此日志文件
   read_timeout = 5 ## 请求超时
   max_children = 30 ### 同时可执行的最大searchd 进程数
   pid_file = /usr/local/coreseek/var/log/searchd.pid #######进程ID文件
   max_matches = 1000### 查询结果的最大返回数
   seamless_rotate = 1### 是否支持无缝切换,做增量索引时通常需要
}
# 注 配置文件中源和索引都可以针对不同的数据库或不同的查询条件配置多个  #############################################################
######################## 七coreseek服务管理 ###############

1 建立索引
# /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf --all --rotate
2 启动后台服务
# /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft_mysql.conf
3 执行增量索引
# /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf delta --rotate
4 合并索引
# /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf --merge main delta --rotate --merge-dst-range deleted 0 0
5 后台服务测试
/usr/local/coreseek/bin/search -c /usr/local/coreseek/etc/csft_mysql.conf北京
6 关闭后台服务
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft_mysql.conf --stop
7 自动化管理
crontab -e
   */1 * * * * /bin/sh /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf delta --rotate
   */5 * * * * /bin/sh /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf --merge main delta --rotate --merge-dst-range deleted 0 0
   30 1 * * */bin/sh /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf --all --rotate  ############ 八 安装php的sphinx扩展 #####################

1 安装libsphinxclient
# cd /data/software/coreseek-3.2.14/csft-3.2.14/api/libsphinxclient/
#./configure--prefix=/usr/local/sphinxclient
# make
# make install
2 安装 sphinx-1.0.4.tgz
# cd /data/software/
# tar xvzf sphinx-1.0.4.tgz
# cd sphinx-1.0.4
# yum -y install php-devel # 如果没有phpize则 安装phpize
# phpize
# ./configure --with-php-config=/usr/bin/php-config --with-sphinx=/usr/local/sphinxclient
# make
# 如果出现错误信息The test-suite requires that proc_open() is available.    |
| Please check if you disabled it in php.ini.
出现上述错误信息,解决方法vi /data/conf/php.ini(即php的配置文件)找到disable_functions这一行 把proc_open去除即可其他出现的错误信息则把相应的函数的禁止去除即可
# make test
# make install
Installing shared extensions:   /usr/lib64/php/modules/ # 这一行显示sphinx.so模块已经被安装到/usr/lib64/php/modules/ 目录下
# vi /data/conf/php.ini
添加
   extension=/usr/lib64/php/modules/sphinx.so  

# service httpd restart(重启apache)  ###################################################################
############# 九 php程序实现分词 #################################
1 查看当前环境是否已经支持sphinx
# vim sphinx.php
   
通过浏览器访问
是否出现以下信息 (如果出现则说明sphinx的扩展已经安装成功)
sphinx
sphinx support    enabled
Version    1.0.3
Revision    $Revision: 294591 $
2) 编写测试代码测试
# vim sphinx.php


   
    访问后可得到数据
    其中["matches"]字段中出现的内容就是在数据库中查找到的内容 查找的数据项如catid是在coreseek的配置文件中定义的。具体如下
    # vim /usr/local/coreseek/etc/csft_mysql.conf
    sql_attr_uint                   = id
    sql_attr_uint                   = catid
    其中matches 中每一条数据的下标志为id 是
    sql_query                               = \
    SELECT id, catid, keywords, title, description \
    FROM v9_news
    中select 中后面的第一个值
    # 注 如果此时我们想重新建立索引会出现一个问题
    FATAL: failed to lock /usr/local/sphinx/var/data/news.spl: Resource temporarily unavailable, will not index. Try --rotate option.
    导致这个问题的原因是我在后台启动了coreseek,所以要先停掉  ################################################################
################## 十 sphinx 中类似mysql的一些特性 ##########

1) 限制条数,起始位置和索引源
vim sphinx2.php
页: [1]
查看完整版本: sphinx+coreseek+php扩展