linux環境下安裝sphinx中文支持分詞搜索(coreseek+mmseg)

分類: 系統運維php

 


爲何要寫這篇文章?css

  答:經過常規的三大步(./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的安裝目錄便可)也沒法解決問題。html

 

官方安裝說明:(原文
============================================================

依賴環境安裝:
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,安裝方法以下:python

  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安裝:mysql

  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/etclinux

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

cd  /usr/local/mmseg3/etcubuntu

../bin/mmseg -u unigram.txtvim

生成unigram.txt.uniapi

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. 一、先安裝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. 二、安裝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.測試
   一、安裝sphinx
      請參照文檔 http://linux008.blog.51cto.com/2837805/622088
二、編寫測試文件
  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. }
相關文章
相關標籤/搜索