coreseek4.1使用sphinx做索引的索引控制shell脚本及逻辑 及 linux安装coreseek4.1的sphinx服务及中文分词mmseg的报错解决方法

一、coreseek4.1使用sphinx做索引的索引控制shell脚本及逻辑

    sphinx做索引时索引数据来源可以有多种方式,比如数据库mysql,pgsql,mssql,odbc,也可以是python脚本,也可以是xml数据文件,xmlpipe (publish:November 1, 2017 -Wednesday)。

    一般来说,如果索引的数据比较简单,需要入索引的字段直接就在数据库表中不需要进行其它额外的处理,完全可以直接使用数据库做索引数据来源,然后写好索引的mysql和增量索引的mysql(一般数据表字段中需要有一个带索引的最后修改时间字段),即可以实现索引,就像本站的搜索,即是使用sphinx直接连接mysql执行索引。但如果需要索引的数据比较复杂,比如所有的记录依赖外部各种计算计果,甚至是查询多表聚会查询筛选处理才能得到的结果,这时就推荐使用xml数据文件作为索引数据源,一是索引数据方便可见,另外是sphinx对这种索引也是特别的支持。今天这篇笔记就是coreseek中的sphinx使用xml文件做数据索引的记录。

    我要实现的功能是有多台服务器作为索引服务器,为了避免对数据库进行大量重复查询,只考虑在一台主服务器上生成主索引文件和增量索引,然后主索引服务器索引生成后通知其它服务器也同时生成主索引和增量索引。在主服务器上定时执行shell脚本来操作处理主索引和增量索引,以生成主索引文件为例,shell中判断全量索引是否有进程在处理(防止某些原因出现索引一直未建立)而后面又跟着启动一大堆的索引进程程序至服务器卡死.完成基本判断后调用php生成xml文件,shell中得到php执行是否成功的标志。最后更新sphinx的索引,如果是增加索引,则再进行全量增量索引的合并。

1. 基本的shell脚本代码如下:

#!/bin/bash
#全量及增量索引执行脚本。
#www.linge.cn

#索引类型参数检查.
if [ ! -n "$1" ] ;then
    echo "第一个参数必须传入:全是索引(all), 增量索引(incr)";
    exit 0;
fi
if [ "$1" != "all" -a "$1" != "incr" ]; then
    echo "第一参数无效:必须是all或incr";
    exit 0;
fi

#测试模式.
if [ ! -n "$2" ] ;then
    testMode=0;
    testChar='';
else
    testMode=1;
    testChar='_test'
fi

#执行开始.
echo "Started:" $(date +"%Y-%m-%d %H:%M:%S")
cd /opt/baofeng-data/apitask/job/shell/

#检查全量索引是否正在执行
checkProcess()
{
    processPid=`ps -ef | grep "$1" | grep -v grep | awk '{print $2}' | tail -1`
    if [ "$processPid" == "" ];
    then
      return 0
    fi
    return 1
}

#检查当前是否有正在执行的全量搜索索引.
checkProcess "Mainsphinx index 0"
if [[ $? -eq 1 ]]; then
    echo '进程已存在,等待下一次执行.';
    exit 0;
fi

#准备执行
if [ "$1" = "incr" ];then
    echo '开始执行增量索引...';
    increment=1;
    incrChar='_incr';
else
    echo '开始执行全量索引...';
    increment=0;
    incrChar='';
fi
#执行索引
echo '索引日志请查看:/opt/log/Mainsphinx.log';
filename='Mainsphinx'$incrChar$testChar".xml.tmp"
cd /opt/api/; php index.php Mainsphinx index $increment $testMode >> log/sphinx.log;exitval=$?;
if [ $exitval == "99" ]; then
    echo "索引执行完成:" $(date +"%Y-%m-%d %H:%M:%S")
else
    echo '索引执行出现异常.' $(date +"%Y-%m-%d %H:%M:%S")
    exit 0;
fi

#如果是测试模式不再执行下面的操作
if [[ $testMode -eq 1 ]]; then
    echo '测试模式执行结束. 请检查生成的测试索引文件:'$filename;
    ls -lh /opt/sphinxdata/;
    exit 0;
fi

#sphinx索引更新.
if [ "$1" = "all" ];then
    sudo mv -f /opt/sphinxdata/$filename /opt/sphinxdata/base.xml;
    sudo /opt/coreseek4.1/bin/indexer -c /opt/coreseek4.1/etc/main.conf base --rotate
    echo "完成全量索引并更新searchd:" $(date +"%Y-%m-%d %H:%M:%S")
else
    sudo mv -f /opt/sphinxdata/$filename /opt/sphinxdata/incr.xml
    sudo /opt/coreseek4.1/bin/indexer -c /opt/coreseek4.1/etc/main.conf incr --rotate
    sudo /opt/coreseek4.1/bin/indexer -c /opt/coreseek4.1/etc/main.conf --merge base incr --rotate 
    echo "增量索引已完成并更新searchd:" $(date +"%Y-%m-%d %H:%M:%S")
fi

echo
echo

2. 在sphinx生成索引时其逻辑如下:

1,生成xml文件
2,使用sphinx的bin/indexer对主索引xml文件进行索引。
3,使用sphinx的bin/indexer对增量索引xml文件进行索引。
4,启动searchd服务提供外部搜索。
5,调用上面的shell脚本执行生成xml文件索引,然后更新2,3的索引。在3时并进行索引合并。
之所以把这个逻辑写下,是因为不能直接执行第5步(之前必须先执行第2,3步生成全量和增量的.sph文件),不然会导致下面的问题:

using config file '/opt/coreseek4.1/etc/main.conf'...
read 287.1 of 287.1 MB, 100.0% done
FATAL: failed to merge index 'incr' into index 'base': source index preload failed: failed to open /opt/coreseek4.1/var/data/incr.sph: No such file or directory

    通过对生成的xml文件目录查看发现生成的incr.spl,incr.sph,incr.spm等一堆文件全都叫做incr.new.spl,incr.new.sph,incr.new.spm。这是因为使用--rotate参数来索引是在原来索引的基础上来进行操作的,在rotate时,sphinx需要将原来的incr.spa重命名为incr.old.spa,然后将incr.new.spa重命名为incr.spa,再通知searchd重启完成rotate。而因为如果直接执行第5步即--rerote来追加索引,则此时没有原文件,就会导致sphinx将原来的incr.spa重命名为incr.old.spa这一步操作失败,从而incr.new.spa文件名未成功更名为incr.spa文件,最终出现找不到incr.sph文件的错误。

二、linux安装coreseek4.1的sphinx服务及中文分词mmseg的报错解决方法

    之前有过一篇安装coreseek的笔记,但是里面没有碰到什么报错,这里把一些错误列出来,原文章地址:Centos下基于sphinx的开源搜索引擎coreseek的安装 及 sphinx常见报错及解决办法_sphinx-coreseek-CSDN博客

    下载coreseek到指定目录解压后,执行bootstrap,开始有报错,报错信息显示缺少libtoolize工具,这个错误可以通过安装libtool来解决.

[onlinedev@BFG-OSER-4435 mmseg-3.2.14]$ sudo ./bootstrap 
+ aclocal -I config
config/sys_siglist.m4:20: warning: underquoted definition of SIC_VAR_SYS_SIGLIST
config/sys_siglist.m4:20:   run info '(automake)Extending aclocal'
config/sys_siglist.m4:20:   or see http://sources.redhat.com/automake/automake.html#Extending-aclocal
configure.in:26: warning: macro `AM_PROG_LIBTOOL' not found in library
+ libtoolize --force --copy
./bootstrap: line 24: libtoolize: command not found
+ autoheader
+ automake --add-missing --copy
src/Makefile.am:37: Libtool library used but `LIBTOOL' is undefined
src/Makefile.am:37:   The usual way to define `LIBTOOL' is to add `AC_PROG_LIBTOOL'
src/Makefile.am:37:   to `configure.in' and run `aclocal' and `autoconf' again.
src/Makefile.am:37:   If `AC_PROG_LIBTOOL' is in `configure.in', make sure
src/Makefile.am:37:   its definition is in aclocal's search path.
+ autoconf
configure.in:26: error: possibly undefined macro: AM_PROG_LIBTOOL
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.

安装命令:sudo yum -y install libtool
再次执行bootstrap,可见不再报libtool的错误,但有以下提示。

[onlinedev@BFG-OSER-4435 mmseg-3.2.14]$ sudo ./bootstrap        
+ aclocal -I config
config/sys_siglist.m4:20: warning: underquoted definition of SIC_VAR_SYS_SIGLIST
config/sys_siglist.m4:20:   run info '(automake)Extending aclocal'
config/sys_siglist.m4:20:   or see http://sources.redhat.com/automake/automake.html#Extending-aclocal
+ libtoolize --force --copy
libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, `config'.
libtoolize: copying file `config/ltmain.sh'
libtoolize: Consider adding `AC_CONFIG_MACRO_DIR([m4])' to configure.in and
libtoolize: rerunning libtoolize, to keep the correct libtool macros in-tree.
libtoolize: Consider adding `-I m4' to ACLOCAL_AMFLAGS in Makefile.am.
+ autoheader
+ automake --add-missing --copy
+ autoconf

我开始直接忽略上面的内容,此时执行了./configure安装,最后碰到错误:cannot find input file: src/Makefile.in,如下:

......
configure: creating ./config.status
config.status: creating Makefile
config.status: WARNING:  'Makefile.in' seems to ignore the --datarootdir setting
config.status: error: cannot find input file: src/Makefile.in

解决的方法就是执行上面列出来的三个命令, 执行如下:

[onlinedev@BFG-OSER-4435 mmseg-3.2.14]$ sudo autoheader
[onlinedev@BFG-OSER-4435 mmseg-3.2.14]$ sudo automake --add-missing --copy
[onlinedev@BFG-OSER-4435 mmseg-3.2.14]$ sudo autoconf
[onlinedev@BFG-OSER-4435 mmseg-3.2.14]$

    最后执行安装,提示成功。然后就是安装coreseek,#注意里面有一项:--without-unixodbc,这个看来是连接microsoft sqlserver那套时要用的(索引要用type=odbc时才能用到),如果我们只用mysql数据库的话,可以不用安装。

[onlinedev@BFG-OSER-4435 mmseg-3.2.14]$ sudo ./configure --prefix=/opt/modules/mmseg3
------------------------------------------------------------------------
Configuration:

  Source code location:       .
  Compiler:                   gcc
  Compiler flags:             -g -O2
  Host System Type:           x86_64-redhat-linux-gnu
  Install path:               /opt/modules/mmseg3

  See config.h for further configuration information.
------------------------------------------------------------------------

#安装coreseek
cd csft-4.1/
sudo ./buildconf.sh
sudo ./configure --prefix=/opt/modules/coreseek4.1 --without-unixodbc --with-mmseg --with-mmseg-includes=/opt/modules/mmseg3/include/mmseg/ --with-mmseg-libs=/opt/modules/mmseg3/lib/ --with-mysql 
sudo make && sudo make install
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林戈的IT生涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值