发布于 2015-06-13 21:10:17 | 739 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Coreseek 中文全文检索引擎

Coreseek 是一款中文全文检索/搜索软件,以GPLv2许可协议开源发布,基于Sphinx研发并独立发布,专攻中文搜索和信息处理领域,适用于行业/垂直搜索、论坛/站内搜索、数据库搜索、文档/文献检索、信息检索、数据挖掘等应用场景,用户可以免费下载使用


##################### 一  mariadb 安装 ##############
                    使用的是yum安装 略
#####################################################

##################### 二  php 环境搭建 ##############
                    使用的是yum安装 略
#####################################################

##################### 三 mmseg3  安装  ##############
 

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

################################################################
 
#################### 四 安装coreseek #################
  

[root@D1 mmseg-3.2.14]# cd ../csft-3.2.14/
   [root@D1 csft-3.2.14]# sh buildconf.sh   # warning 信息可以忽略 error信息需要解决
   [root@D1 csft-3.2.14]# ./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
   [root@D1 csft-3.2.14]# make && make install

###########################################################  

#################### 五 测试coreseek 分词 #################
  

 [root@D1 coreseek-3.2.14]# cd testpack/
   [root@D1 testpack]# cat  var/test/test.xml  # 此时应该正确显示中文
   [root@D1 testpack]# /usr/local/mmseg3/bin/mmseg -d /usr/local/mmseg3/etc var/test/test.xml
   [root@D1 testpack]# /usr/local/coreseek/bin/indexer -c etc/csft.conf --all
   [root@D1 testpack]# /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 [test]> source /root/v9_news.sql;  # 其中有两万多条数据 供测试
   MariaDB [test]> show create table v9_newsG;
*************************** 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 的配置文件
 

[root@D1 testpack]# 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 建立索引
  [root@D1 testpack]# /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf --all --rotate
  
  2 启动后台服务
  [root@D1 testpack]# /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft_mysql.conf  
  
  3 执行增量索引
  [root@D1 testpack]# /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf delta --rotate  
  
  4 合并索引
  [root@D1 testpack]# /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
  [root@D1 sphinx-1.0.4]# cd /data/software/coreseek-3.2.14/csft-3.2.14/api/libsphinxclient/
  [root@D1 libsphinxclient]#  ./configure  --prefix=/usr/local/sphinxclient 
  [root@D1 libsphinxclient]# make
  [root@D1 libsphinxclient]# make install
 2 安装 sphinx-1.0.4.tgz
  [root@D1 sphinx]# cd /data/software/
  [root@D1 software]# tar xvzf sphinx-1.0.4.tgz
  [root@D1 software]# cd sphinx-1.0.4
  [root@D1 sphinx-1.0.4]# yum -y install php-devel # 如果没有phpize则 安装phpize
 [root@D1 sphinx-1.0.4]# phpize
 [root@D1 sphinx-1.0.4]# ./configure --with-php-config=/usr/bin/php-config --with-sphinx=/usr/local/sphinxclient
 [root@D1 sphinx-1.0.4]# 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去除即可  其他出现的错误信息则把相应的函数的禁止去除即可 
  [root@D1 sphinx-1.0.4]# make test
  [root@D1 sphinx-1.0.4]# make install
  Installing shared extensions:     /usr/lib64/php/modules/ # 这一行显示sphinx.so模块已经被安装到/usr/lib64/php/modules/ 目录下
  [root@D1 sphinx-1.0.4]# vi /data/conf/php.ini
  添加
   extension=/usr/lib64/php/modules/sphinx.so

   
 

 [root@D1 sphinx-1.0.4]# service httpd restart  (重启apache)

###################################################################

############# 九 php程序实现分词 #################################
  1 查看当前环境是否已经支持sphinx
  [root@D1 sphinx-1.0.4]# vim sphinx.php
    <?php
        phpinfo();
    
    ?>
  通过浏览器访问
  是否出现以下信息 (如果出现则说明sphinx的扩展已经安装成功)
  sphinx
  sphinx support    enabled
  Version    1.0.3
  Revision    $Revision: 294591 $
  2) 编写测试代码测试
  [root@D1 sphinx-1.0.4]# vim sphinx.php

   <?php
     $sphinx = new SphinxClient();
     $sphinx->setServer("localhost", 9312);
     $sphinx->setMatchMode(SPH_MATCH_ANY);
     $sphinx->setMaxQueryTime(100);
     $result = $sphinx->query("北京");
     echo "<pre>";
     var_dump($result);
     echo "</pre>";
    ?>  

    访问后可得到数据
    其中["matches"]字段中出现的内容就是在数据库中查找到的内容 查找的数据项如catid是在coreseek的配置文件中定义的。具体如下
    [root@D1 testpack]# 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

 <?php
 $sphinx = new SphinxClient();
 $sphinx->setServer( ‘localhost‘, 9312 );
 $sphinx->setArrayResult( true );
 $sphinx->setLimits(0, 10, 1000);  // 限制数据的条数10和起始位置0,最大条数1000 如果这一行不设置的话默认显示的是前20条
 $sphinx->SetMaxQueryTime(10);
 $index = ‘news‘; //索引源是配置文件中的 index 类,如果有多个索引源可使用,号隔开:‘email,diary‘ 或者使用‘*‘  我在上面使用的是news,所以这里也使用news
 $result = $sphinx->query (‘北京‘, $index);
 echo "<pre>";
 var_dump($result);
 echo "</pre>";

 
 2) 限制id范围
 $sphinx->setIdRange(100, 3000);  // 限制id的范围
 
 3) 属性过滤
 $sphinx->SetFilter(‘catid‘, array(60,65));  // 此时查出来的数据catid全部是60或者65。如果使用$sphinx->SetFilter(‘catid‘, array(60,65), false); 则说明查询catid不为60和65的 。如果使用$sphinx->SetFilterRange(‘catid‘, 60, 65); 则说明catid在60到65这个范围里面的。 如果使用 $sphinx->SetFilterRange(‘catid‘, 60, 65,false);  说明catid不在60到65的范围
 
 4) 排序模式
  $sphinx->setSortMode ( "SPH_SORT_ATTR_DESC", ‘id‘); // 排序方式,按照id升序排列。
 注:可使用的排序模式有:
 SPH_SORT_RELEVANCE 模式, 按相关度降序排列(最好的匹配排在最前面)
 SPH_SORT_ATTR_DESC 模式, 按属性降序排列 (属性值越大的越是排在前面)
 SPH_SORT_ATTR_ASC 模式, 按属性升序排列(属性值越小的越是排在前面)
 SPH_SORT_TIME_SEGMENTS 模式, 先按时间段(最近一小时/天/周/月)降序,再按相关度降序
 SPH_SORT_EXTENDED 模式, 按一种类似SQL的方式将列组合起来,升序或降序排列。
SPH_SORT_EXPR 模式,按某个算术表达式排序
 6)匹配模式
 $sphinx->setMatchMode(SPH_MATCH_ANY); // 采用这一种模式的话会把超级可爱分成两个词 ‘超级’ ‘可爱’ 并分别把相应的数据查询出来达到拆词的效果
 $result = $sphinx->query (‘超级可爱‘, $index);  
 注:有如下可选的匹配模式:
 
 SPH_MATCH_ALL, 匹配所有查询词(默认模式);
 
 SPH_MATCH_ANY, 匹配查询词中的任意一个;
 
 SPH_MATCH_PHRASE, 将整个查询看作一个词组,要求按顺序完整匹配;
 
 SPH_MATCH_BOOLEAN, 将查询看作一个布尔表达式 
 
 SPH_MATCH_EXTENDED, 将查询看作一个CoreSeek/Sphinx内部查询语言的表达式 . 从版本Coreseek 3/Sphinx 0.9.9开始, 这个选项被选项SPH_MATCH_EXTENDED2代替,它提供了更多功能和更佳的性能。保留这个选项是为了与遗留的旧代码兼容——这样即使Sphinx及其组件包括API升级的时候,旧的应用程序代码还能够继续工作。
 
 SPH_MATCH_EXTENDED2, 使用第二版的“扩展匹配模式”对查询进行匹配.
SPH_MATCH_FULLSCAN, 强制使用下文所述的“完整扫描”模式来对查询进行匹配。注意,在此模式下,所有的查询词都被忽略,尽管过滤器、过滤器范围以及分组仍然起作用,但任何文本匹配都不会发生.
我们要关注的主要是SPH_MATCH_EXTENDED2扩展匹配模式,扩展匹配模式允许使用一些像mysql的条件语句

#####################################################################

############### 十一 注意事项 #######################################
 

sphinx是查询数据库中相印数据的id值,具体的数据还应该根据id值到数据库中去查找


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务