zoukankan      html  css  js  c++  java
  • linux环境下安装sphinx中文支持分词搜索(coreseek+mmseg)

    分类: 系统运维


    为什么要写这篇文章?

      答:通过常规的三大步(./configure,make,make install)的编译安装mmseg时,总是出现找不到src/Makefile.in文件的错误(config.status: error: cannot find input file: src/Makefile.in)。即便是依照官方教程所说的先安装好依赖软件(yum -y install glibc-common libtool autoconf automake mysql-devel expat-devel,如果是用编译安装的方法安装的Mysql就不用装mysql-devel,在编译csf时指定Mysql的安装目录即可)也无法解决问题。

    官方安装说明:(原文
    ============================================================

    依赖环境安装:
    Debina等使用deb系统:
    aptitude -y install yum glibc-common build-essential libtool autoconf automake libexpat-dev  mysql-client (如果是用编译安装的方法安装的Mysql就不用装mysql-client ,在编译csf时指定Mysql的安装目录即可)
    Centos等使用yum系统:
    yum -y install glibc-common libtool autoconf automake expat-devel  mysql-devel (如果是用编译安装的方法安装的Mysql就不用装mysql-devel,在编译csf时指定Mysql的安装目录即可)
    BSD系统:
    pkg_add -r libtool autoconf automake mysql5-client iconv
    安装升级autoconf
    coreseek需要autoconf 2.64以上版本,因此需要升级autoconf,不然会报错从http://download.chinaunix.net/download.php?id=29328&ResourceID=648下载autoconf-2.64.tar.bz2,安装方法如下:

    1. tar -jxvf autoconf-2.64.tar.bz2
    2. cd autoconf-2.64
    3. ./configure
    4. make
    5. make instal
    coresek source下载
    http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gz
    mmseg安装:
    1. tar xzvf coreseek-3.2.14.tar.gz
    2. cd coreseek-3.2.14
    3. ls
    4. csft-3.2.14 mmseg-3.2.14 README.txt testpack
    5. cd mmseg-3.2.14
    6. aclocal
    7. libtoolize --force
    8. automake --add-missing
    9. autoconf
    10. autoheader
    11. make clean #此时如有错误可忽略不管
    12. ./configure --prefix=/usr/local/mmseg3
    13. make
    14. make install
    cp -f src/*/*.h /usr/local/mmseg3/include/mmseg/

    一定要执行以上的拷贝,否则coreseek在make编译时会出错。

    coreseek安装:

    1. cd ..
    2. cd csft-3.2.14
    3. bash buildconf.sh
    4. aclocal
    5. libtoolize --force
    6. automake --add-missing
    7. autoconf
    8. autoheader
    9. perl -pi -e 's/lpthread/lpthread -liconv/g' src/Makefile*
    10. make clean #此时如有错误可忽略不管
    11. ./configure --prefix=/usr/local/coreseek --enable-id64 --without-python --with-mysql=/usr/local/mysql --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/
    12. make
    13. make install
    14. cp /usr/local/coreseek/etc/sphinx.conf.dist /usr/local/coreseek/etc/csft.conf
    测试mmseg分词和coreseek搜索:  参考 http://blog.chinaunix.net/uid-20639775-id-3261834.html
    备注:需要预先设置好字符集为zh_CN.UTF-8,确保正确显示中文,我的系统字符集为en_US.UTF-8也是可以的。

    1. cd testpack
    2. cat var/test/test.xml #此时应该正确显示中文
    3. /usr/local/mmseg3/bin/mmseg -d /usr/local/mmseg3/etc var/test/test.xml
    4. /usr/local/coreseek/bin/indexer -c etc/csft.conf --all
    5. /usr/local/coreseek/bin/search -c etc/csft.conf 网络搜索
    6. 此时正确的应该返回
    7. words:
    8. 1. '网络': 1 documents, 1 hits
    9. 2. '搜索': 2 documents, 5 hits

    **************分词库*****************

    mkdir -p /usr/local/mmseg3/etc

    cp <mmseg_soft_rootdir>/data/unigram.txt /usr/local/mmseg3/etc/

    cd  /usr/local/mmseg3/etc

    ../bin/mmseg -u unigram.txt

    生成unigram.txt.uni

    cp unigram.txt.uni uni.lib

    分词库配置说明

    vi mmseg.ini

    在里面添加内容
    [mmseg]
    merge_number_and_ascii=1;
    number_and_ascii_joint=-.;
    compress_space=0;
    seperate_number_ascii=1;
    以上解释如下
    //
    merge_number_and_ascii: 字母和数字连续出现是非切分
    number_and_ascii_joint:连接数字和字母可用的符号,如’-’ ‘.’ 等
    compress_space:暂时无效
    seperate_number_ascii:是否拆分数字,如 1988 -> 1/x 9/x 8/x 8/x
    //

    *********************************

    cd /usr/local/coreseek/

    bin/indexer --all [--config sphinx.conf] 用于创建全文索引

    bin/search <搜索关键字>   一个简单的命令行(CLI) 的测试程序,用于测试全文索引;

    bin/searchd [--config sphinx.conf] 一个守护进程,其他软件可以通过这个守护进程进行全文检索;

    附加:

    1. 如需启用python支持,请安装或者升级至python2.6,configure之中,去掉--without-python,加上--with-python,然后重新编译安装
    python data source演示:下载

    2. 如果不可以搜索中文,请首先检查英文是否正常;如英文正常,则请检查:
      a. charset_dictpath = etc/dict/   #请确保uni.lib在此目录下
          charset_type = zh_cn.utf-8
      b. charset_table的配置请注释掉
      c. 请确保从数据来源读取的数据,为utf-8字符集;
      MySQL4.1起可以通过SET NAMES设定输出字符集,即使原始数据为GBK;
      MySQL4.1以下版本,请直接联系我们解决GBK问题;
      d. 请确保你测试检索时,传送到search或者searchd的字符串尾utf-8
      e. 请查看search或者Query返回信息中,分词信息是否正确
      d. 词典的设置和构建,具体请查看mmseg
      f. 请前往 论坛 提出问题


    3. 如果提示:iniparser: cannot open /usr/local/csft/mmseg.ini
     请前往查看mmseg.ini具体设置方法

    4. 如果提示no working pthreads library found,请参考
    ===============================================================
    5. coreseek启动脚本
    coreseek源码包里没有启动程序,只能手动通过命令来启动,将下面的文件命名为shinxed,保存在/etc/ini.d/里,就可以实现相应的系统自动启过徎。

    1. #!/bin/sh
    2. # sphinx: Startup script for Sphinx search
    3. #
    4. # chkconfig: 345 86 14
    5. # description: This is a daemon for high performance full text
    6. # search of MySQL and PostgreSQL databases.
    7. # See http://www.sphinxsearch.com/ for more info.
    8. #
    9. # processname: searchd
    10. # pidfile: /var/run/coreseek.pid
    11. # Source function library.
    12. . /lib/lsb/init-functions
    13. sphinxlocation=/usr/local/coreseek
    14. name=coreseek
    15. daemon=$sphinxlocation/bin/searchd
    16. config="-c $sphinxlocation/etc/csft_mysql.conf"
    17. RETVAL=0
    18. PATH=$PATH:$sphinxlocation/bin
    19. start() {
    20. echo -n $"Starting Sphinx daemon: "
    21. start-stop-daemon --start --quiet --pidfile /var/run/$name.pid
    22. --exec $daemon -- $config || true
    23. echo "$name."
    24. }
    25. stop() {
    26. echo -n $"Stopping Sphinx daemon: "
    27. start-stop-daemon --stop --quiet --pidfile /var/run/$name.pid
    28. --exec $daemon -- $config || true
    29. }
    30. # See how we were called.
    31. case "$1" in
    32. start)
    33. start
    34. ;;
    35. stop)
    36. stop
    37. ;;
    38. status)
    39. status $processname
    40. RETVAL=$?
    41. ;;
    42. restart)
    43. stop
    44. sleep 3
    45. start
    46. ;;
    47. condrestart)
    48. if [ -f /var/run/$name.pid ]; then
    49. stop
    50. sleep 3
    51. start
    52. fi
    53. ;;
    54. *)
    55. echo $"Usage: $0 {start|stop|status|restart|condrestart}"
    56. ;;
    57. esac
    58. exit $RETVAL
    其中condrestart是conditional restart的意思,如果服务当前已经是运行的话,它可以重启这个服务,但是如果服务没有运行,condrestart是无法启动这个服务的。而restart都可以。

    然后可以使用 sudo /etc/inid.d/sphinxed start来启动

    ubuntu下开机自动启动:sudo update-rc.d sphinxed defaults

    start-stop-daemon是一个很优秀的后台运行管理程序,采用C开发。Ubuntu上使用start-stop-daemon比较方便,CetnOS就要手动安装下。

    1. yum install gcc
    2. cd /tmp
    3. wget http://developer.axis.com/download/distribution/apps-sys-utils-start-stop-daemon-IR1_9_18-2.tar.gz
    4. tar zxf apps-sys-utils-start-stop-daemon-IR1_9_18-2.tar.gz
    5. cd apps/sys-utils/start-stop-daemon-IR1_9_18-2/
    6. gcc start-stop-daemon.c -o start-stop-daemon
    7. cp start-stop-daemon /usr/local/bin/start-stop-daemon

    5. SphinxClient 安装php扩展sphinx
    或者可以直接使用$root_path/coreseek-version/testpack/api/目录中的提供的api接口文件
    1.安装

    1. 1、先安装sphinxclient
    2. #cd /usr/local/src
    3. #wget http://sphinxsearch.com/files/sphinx-0.9.9.tar.gz
    4. #tar xzvf sphinx-0.9.9.tar.gz
    5. #cd sphinx-0.9.9/api/libsphinxclient
    6. #vim sphinxclient.c
    7. 找到
    8. void sock_close ( int sock );
    9. 改为
    10. static void sock_close ( int sock );
    11. #./configure --prefix=/usr/local/sphinxclient
    12. #make
    13. #make install
    14. 2、安装sphinx扩展
    15. #wget http://pecl.php.net/get/sphinx-1.0.4.tgz
    16. #tar xvzf sphinx-1.0.4.tgz
    17. #cd sphinx-1.0.4
    18. #/usr/local/php/bin/phpize
    19. #./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx=/usr/local/sphinxclient
    20. #make
    21. #make install
    22. 修改php.ini
    23. extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
    24. [sphinx]
    25. extension=sphinx.so
    2.测试
       1、安装sphinx
          请参照文档http://linux008.blog.51cto.com/2837805/622088
    2、编写测试文件
    1. #vim sphinx.php
    2. $s = new SphinxClient;
    3. setServer("localhost", 9312);
    4. $s->setMatchMode(SPH_MATCH_ANY);
    5. $s->setMaxQueryTime(3);
    6. $result = $s->query("demo");
    7. var_dump($result);
    8. ?>
    9. #/usr/local/php/bin/php sphinx.php 运行结果
    10. array(9) {
    11. ["error"]=>
    12. string(0) ""
    13. ["warning"]=>
    14. string(0) ""
    15. ["status"]=>
    16. int(0)
    17. ["fields"]=>
    18. array(5) {
    19. [0]=>
    20. string(6) "cat_id"
    21. [1]=>
    22. string(13) "provider_name"
    23. [2]=>
    24. string(12) "goods_number"
    25. [3]=>
    26. string(18) "promote_start_date"
    27. [4]=>
    28. string(8) "keywords"
    29. }
    30. ["attrs"]=>
    31. array(8) {
    32. ["goods_sn"]=>
    33. string(1) "3"
    34. ["goods_name"]=>
    35. string(1) "3"
    36. ["brand_id"]=>
    37. string(1) "1"
    38. ["goods_weight"]=>
    39. string(1) "5"
    40. ["market_price"]=>
    41. string(1) "5"
    42. ["shop_price"]=>
    43. string(1) "5"
    44. ["promote_price"]=>
    45. string(1) "5"
    46. ["gid"]=>
    47. string(10) "1073741825"
    48. }
    49. ["total"]=>
    50. int(0)
    51. ["total_found"]=>
    52. int(0)
    53. ["time"]=>
    54. float(0)
    55. ["words"]=>
    56. array(1) {
    57. ["demo"]=>
    58. array(2) {
    59. ["docs"]=>
    60. int(0)
    61. ["hits"]=>
    62. int(0)
    63. }
    64. }
    65. }
  • 相关阅读:
    Data Base Oracle 常用命令
    ASP.NET Core ASP.NET Core+MVC搭建及部署
    Hadoop 之 MapReduce 框架演变详解
    计算机网络: IP地址,子网掩码,默认网关,DNS服务器详解
    Linux系统基本网络配置之ifconfig命令
    Linux-eth0 eth0:1 和eth0.1关系、ifconfig以及虚拟IP实现介绍
    Linux 中将用户添加到组的指令
    几种常见的Shell
    常见的Shell
    Linux(CentOS6.5)下创建新用户和组,并制定用户和组ID
  • 原文地址:https://www.cnblogs.com/brady-wang/p/5552803.html
Copyright © 2011-2022 走看看