centos6操作系统

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

coreseek安装需要预装的软件:

yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2-devel expat-devel

cd /usr/local/src
wget http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gz
tar -xzvf coreseek-3.2.14.tar.gz
cd coreseek-3.2.14

##安装mmseg
cd mmseg-3.2.14
./bootstrap #输出的warning信息可以忽略,如果出现error则需要解决
./configure --prefix=/usr/local/mmseg3
make && make install
cd ..
## 安装完成后,mmseg使用的词典和配置文件将自动安装到/usr/local/mmseg3/etc中

##安装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 ##如果提示mysql问题,可以查看MySQL数据源安装说明
make && make install
cd ..

cd /usr/local/coreseek/etc
cp sphinx-min.conf.dist sphinx.conf
vi sphinx.conf
内容示例如下(localhost,DB_USER,DB_PASSWORD,DB_NAME自行修改)
#
# Minimal Sphinx configuration sample (clean, simple, functional)
#

source content
{
        type                                    = mysql

        sql_host                                = localhost
        sql_user                                = DB_USER
        sql_pass                                = DB_PASSWORD
        sql_db                                  = DB_NAME
        sql_port                                = 3306  # optional, default is 3306
        sql_query_pre                           = SET NAMES utf8

        sql_query                               = \
                SELECT id, title, pub_time, group_id, content FROM contents where status = '1'

        sql_attr_uint                   = group_id
        sql_attr_timestamp              = pub_time

        sql_query_info                  = SELECT * FROM contents WHERE id=$id
}
index content
{
        source                                  = content
        path                                    = /usr/local/coreseek/var/data/content
        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                         = 1
        unlink_old                              = 1
}

然后根据以上配置建立索引文件

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

启动命令
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf

然后在coreseek目录下,新建3个sh脚本,以便操作
停止服务stop.sh

#!/bin/bash
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf --stop

建立索引build.sh

#!/bin/bash
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/sphinx.conf --all --rotate

启动服务start.sh

#!/bin/bash
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf

添加可执行权限

chmod +x start.sh
chmod +x stop.sh
chmod +x build.sh

运行start.sh后,使用crontab定时执行build.sh,就可更新索引。(注:因为数据量小且更新不算很频繁,未使用增量索引,只是定时重建主索引,新版本CoreSeek全文搜索 4.1 支持实时索引)

crontab -e
0 2 * * * sh /usr/local/coreseek/build.sh >/dev/null 2>&1

每天凌晨2点重建一次索引,忽略日志输出。

在/usr/local/src/coreseek.3.2.14/csft-3.2.14/api目录下提供了PHP的接口文件 sphinxapi.php,这个文件包含一个SphinxClient的类,copy到自己的web目录下
通过如下方式进行搜索

$s_key = trim($s_key);
if(strpos($s_key,'\'') || strpos($s_key,'\"') || strpos($s_key,'\;')) {
    exit('非法字符');
}
require("sphinxapi.php");

$page_nums = 20;
$offset_start = ($page_index-1)*$page_nums;
$offset_end = $offset_start + $page_nums;
$cl = new SphinxClient();
$cl->SetServer('localhost', 9312);
$cl->SetArrayResult(true);
$cl->SetMatchMode(SPH_MATCH_ALL);
$cl->SetLimits($offset_start,$offset_end);
$cl->SetSortMode(SPH_SORT_RELEVANCE);
$res = $cl->Query($s_key,"content");

安装包括两个部分,mmseg和csft

安装成功会在/usr/local文件夹下面出现coreseek文件夹

source bt
{
 sql_pass                = ****  #如果密码里面有#号需要使用转意字符,否则连接不了数据库  
 sql_query_pre  = SET NAMES utf8 #要根据你自己数据库的编码改变,比如如果编码是utf8mb4而编码写的是utf8 会出现没有搜索结果的问题
 
}
 
index bt
{
    source                    = bt  #这个地方的值要和前面配置的source名对应
}

/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf --stop  停止服务
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/sphinx.conf --all --rotate  建立索引

/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf   开启服务

默认配置文件是csft.conf 如果配置文件是其他名字的话,需要-c 来制定配置文件路径
---------------

配置文件中

sql_query                =   xxxx

xxxx代表一个sql语句,sql语句select的第一个字段将被sphinx认作表的主键来进行索引,所以数据表的主键字段不是int类型也没有关系,选一个是int类型的字段排在select语句的第一个就行了,但是这个字段要保证唯一性,否则会导致搜索结果不完整,计算出来的值也可以被当做主键来进行索引 比如SELECT unix_timestamp(time),name, age .......unix_timestamp(time)是计算出来的,它排在第一个的时候,就会被sphinx当做表的主键来进行索引。
--------------------- 
 

 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐