安装中文全文检索 CoreSeek

 
http://www.wapm.cn/uploads/csft/4.0/coreseek-4.1-beta.tar.gz
tar xzvf coreseek-4.1-beta.tar.gz

1.  安装mmseg
$ cd /usr/local/src/coreseek-4.1-beta/mmseg-3.2.14
$ ./bootstrap    #输出的warning信息可以忽略,如果出现error则需要解决
$ ./configure --prefix=/usr/local/mmseg3
$ make && make install

2.  安装coreseek
$ cd /usr/local/src/coreseek-4.1-beta/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
$ make && make install

3.  测试mmseg分词,coreseek搜索(需要预先设置好字符集为zh_cn.utf-8,确保正确显示中文)
$ 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 csft.conf test1     // 其中 test1 是配置文件中的索引名
$ /usr/local/coreseek/bin/indexer -c etc/csft.conf --all
$ /usr/local/coreseek/bin/search -c etc/csft.conf 网络搜索

4.  coreseek 命令测试
$ /usr/local/coreseek/bin/indexer -c etc/csft.conf  测试配置是否正确
$ /usr/local/coreseek/bin/indexer -c etc/csft.conf xx  或者 --all为文件中全部索引项建立索引     加载索引

$ /usr/local/coreseek/bin/search -c etc/csft.conf  "测试搜索"     搜索“测试搜索”
$ /usr/local/coreseek/bin/search -c etc/csft.conf "test"      搜索"test"
 
5.  启动为服务
$ /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft.conf     开始监听 默认端口为9312
 
6.  停止搜索服务
$ /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft.conf --stop
 
7.  如果启动服务,要更新索引
$ /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft.conf --all --rotate

8.  安装 sphinx 的 php 插件
首先安装 sphinxcient 客户端
$ cd /usr/local/src/coreseek-4.1-beta/csft-4.1/api/libsphinxclient
$ ./configure && make && make install
 
接着安装 sphinx 插件
$ wget sphinx-1.2.0.tgz
$ tar zxvf sphinx-1.2.0.tgz
$ cd sphinx-1.2.0
$ ./configure
$ make && make install

可能遇到的问题:
     1. 如果出现undefined reference to `libiconv'的类似错误,可以按照如下方法处理:
     ##方法一:(Linux使用)
     ## 直接执行:export LIBS="-liconv"
     ##然后make clean,再次configure后,进行编译安装make && make install
     ## 方法二:
     ## 首先configure,然后vim src/makefile
     ## 在其中搜索lexpat,在其后加上 -liconv
     ## 修改后该行应该为:-lexpat -liconv -L/usr/local/lib
     ## 然后再次make && make install
     ## 方法三:
     ## 首先configure,然后vim config/config.h
     ## 在其中搜索USE_LIBICONV,将其后的1修改为0
     ## 然后再次make && make install
 
     2. 英文字符可以正常检索,中文字符不能检索
     ##原因:由于配置文件错误 csft.conf 没有正确配置 缺少关键的中文分词支持
     ##解决办法:在配置文件中记得加上 sql_query_pre = SET NAMES utf8
                          在 index 段这里记得加上 charset_dictpath = /usr/local/mmseg3/etc/     charset_type = zh_cn.utf-8     ngram_len = 0
 
 
9.  sphinxSE 引擎安装
mysql支持引擎的在线插拔,所以我们只需要将引擎文件添加进去即可
 
cmake安装
$ wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.gz
$ tar zxvf cmake-2.8.10.2
$ ./configure
$ make && make install
 
mysql安装
$ wget http://www.wapm.cn/uploads/csft/4.0/coreseek-4.1-beta.tar.gz   // 注意 coreseek-3.2.14 是不支持mysql-5.5.x 系列的 coreseek-4.0.1 开始才支持
$ tar zxvf mysql-5.5.27.tar.gz
$ tar zxvf /usr/local/src/coreseek-4.1-beta.tar.gz
$ cp -rf coreseek-4.1-beta/csft-4.1/mysqlse mysql mysql-5.5.27/storage/sphinx
$ cd mysql-5.5.27
$ cmake . -DCMAKE_BUILD_TYPE=Release -DWITH_SPHINX_STORAGE_ENGINE=1
$ make
$ cp storage/sphinx/ha_sphinx.so /usr/local/mysql/lib/plugin/
$ mysql     ->     install plugin sphinx soname "ha_sphinx.so";     ->     show engines;     // 出现SPHINX引擎 安装成功
 
可能遇到的问题:
     1.如果出现 show engines 中已经有sphinxSE引擎,但是无法添加该引擎的数据库
     ##方法:重新编译安装mysql
 
10.  测试
配置好 /usr/local/coreseek/etc/csft.conf 配置文件
$ cat /usr/local/coreseek/etc/csft.conf
source src1
{
  type            = mysql
  sql_host        = 192.168.158.145
  sql_user        = test
  sql_pass        = test@ptah
  sql_db          = test
  sql_port        = 3306  # optional, default is 3306

  sql_query_pre   = SET NAMES utf8
  sql_query       = \
    SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
    FROM documents

  sql_attr_uint   = group_id
  sql_attr_timestamp  = date_added

  sql_query_info  = SELECT * FROM documents WHERE id=$id
}

index test1
{
  source          = src1
  path            = /usr/local/coreseek/var/data/test1
  docinfo         = extern
  charset_dictpath = /usr/local/mmseg3/etc/
  charset_type    = zh_cn.utf-8
  ngram_len       =  0
}
 
indexer
{
  mem_limit       = 32M
}

searchd
{
  port            = 9312
  log             = /usr/local/coreseek/var/log/searchd.log
  query_log       = /usr/local/coreseek/var/log/query.log
  read_timeout    = 5
  max_children    = 30
  pid_file        = /usr/local/coreseek/var/log/searchd.pid
  max_matches     = 1000
  seamless_rotate = 1
  preopen_indexes = 0
  unlink_old      = 1
}
 
建立好SphinxSE查询数据表,从/usr/local/src/coreseek-4.1-beta/testpack/var/test/documents.sql 导入到 test 数据库中
$ mysql> grant all on test.* to test@'%' identified by 'test@ptah';
 
开启服务
$ searchd -c /usr/local/coreseek/etc/csft.conf
 
建立查询表
$ mysql> create table sphinx( id integer unsigned not null,     weight integer not null,     query varchar(255) not null,     group_id integer,     index(query)) engine=sphinx connection="sphinx://192.168.158.145:9312";
我们这里可以直接使用sphinx://192.168.158.145:9312 而不用使用 sphinx://192.168.158.145:9312/test1 不需要在后面加索引名 这样就能搜索sphinx中的全部数据,在实际应用中 如果数据库进行了分表操作同时数据源进行定时增量操作,我们需要为每一个表建立两个数据源(分别是全部数据索引和增量数据索引),为多个数据源建立两个索引(全部索引和增量索引)
例如:有两个test1,test2表 只要有 source test1;     source test2;     source test1_incre:test1;     source test2_incre:test2;     index test(source test1; source      test2);     index test_incre:test(source test1_incre; source test2_incre;);    
 
执行 SphinxSE 查询
$ select * from sphinx where query="网络搜索;mode=any";