Sphinx 安装记录

如果你想支持中文全文检索的话,请参考 coreseek 安装记录
如果你已经安装 sphinx 或者 coreseek, 只是想查找怎么配置和使用 sphinx 和 coreseek 的话,请参考 coreeek 和 sphinx 的配置与使用

下面的表纯属文章虚构,由于配置内容较多,部分省略,具体可以参考官方文档。

想吐槽一句:编译安装真浪费时间, configure && make && make install 一个软件就需要几十分钟。我这安装四五个,几个小时就没了。

安装前需要先去官网下载源码.
目前最新版本是 2.2.5-release, 点击下载即可。

当然,如果你想直接在命令行下载,直接下载我这个版本也行,就是不知道会不会版本太久。

  
  
  1. tiankonguse:~ $ cd /usr/local/src
  2. tiankonguse:src $ su root -
  3. tiankonguse:src # wget http://sphinxsearch.com/files/sphinx-2.2.5-release.tar.gz

然后解压缩,命令就不用说了吧

  
  
  1. tiankonguse:src # tar zxvf filename.tar.gz

后来听说 sphinx 有两种安装方式

  1. 单独安装,查询时采用API调用。
  2. 使用插件方式把sphinx编译成一个mysql插件并使用特定的sql语句进行检索。

这里我选择第一种方式,毕竟把 sphinx 和 mysql 耦合在一起的话, 将来将成为一个很大的坑。

sphinx 查询出来的是 id, 然后会进行二次查询得到想要的数据。

下面的命令都是在 root 权限下操作的。

  
  
  1. tiankonguse:sphinx-2.2.5-release # ./configure –prefix=/usr/local/sphinx
  2. tiankonguse:sphinx-2.2.5-release # make && make install

–prefix 指向sphinx的安装路径
–with-mysql 指向mysql的安装路径
不指定时按默认路径选择

安装完毕后查看一下 /usr/local/sphinx 下是否有 三个目录 bin etc var,如有,则安装无误!

  
  
  1. tiankonguse:sphinx-2.2.5-release # cd /usr/local/sphinx/
  2. tiankonguse:sphinx # ls
  3. bin/ etc/ share/ var/

由于我使用的是 mysql, 所以需要为 sphinx 创建对应的db。

  
  
  1. # server:127.0.0.1
  2. # database : d_sphinx_testdb
  3. # table: t_sphinx_article
  4. CREATE SCHEMA IF NOT EXISTS `d_sphinx_testdb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
  5. USE `d_sphinx_testdb` ;
  6. CREATE TABLE `d_sphinx_testdb`.`t_sphinx_article` (
  7. `c_id` INT NOT NULL AUTO_INCREMENT,
  8. `c_title` VARCHAR(45) NOT NULL DEFAULT '',
  9. `c_content` VARCHAR(45) NOT NULL DEFAULT '',
  10. `c_comment_num` VARCHAR(45) NOT NULL DEFAULT 0,
  11. PRIMARY KEY (`c_id`))
  12. ENGINE = InnoDB
  13. DEFAULT CHARACTER SET = utf8;

首先需要找到需要配置的文件以及需要配置的内容。

我们需要配置的是 /usr/local/sphinx/sphinx.conf 文件里面的数据库的信息。

  
  
  1. tiankonguse:sphinx # cd etc
  2. tiankonguse:etc #
  3. tiankonguse:etc # ls
  4. example.sql sphinx-min.conf.dist sphinx.conf.dist
  5. tiankonguse:etc # cp sphinx.conf.dist sphinx.conf
  6. tiankonguse:etc # ls
  7. example.sql sphinx-min.conf.dist sphinx.conf sphinx.conf.dist
  8. tiankonguse:etc $ vi sphinx.conf

可以看到下面的内容设置数据源 source

  
  
  1. #############################################################################
  2. ## data source definition
  3. #############################################################################
  4. source d_sphinx_testdb
  5. {
  6. # data source type. mandatory, no default value
  7. # known types are mysql, pgsql, mssql, xmlpipe, xmlpipe2, odbc
  8. type = mysql # 数据库类型
  9. # some straightforward parameters for SQL source types
  10. #数据库主机地址
  11. sql_host = 127.0.0.1
  12. #数据库用户名
  13. sql_user = root
  14. #数据库密码
  15. sql_pass = pwd
  16. #数据库名称
  17. sql_db = d_sphinx_testdb
  18. # 数据库采用的端口
  19. sql_port = 3306
  20. # pre-query, executed before the main fetch query
  21. # multi-value, optional, default is empty list of queries
  22. #执行sql前要设置的字符集
  23. sql_query_pre = SET NAMES UTF8
  24. # main document fetch query mandatory, integer document ID field MUST be the first selected column
  25. # 全文检索要显示的内容,在这里尽可能不使用where或group by,将where与groupby的内容交给sphinx,由sphinx进行条件过滤与groupby效率会更高
  26. # select 出来的字段必须至少包括一个唯一主键(ARTICLESID)以及要全文检索的字段,你计划原本在where中要用到的字段也要select出来,这里不需要使用orderby
  27. sql_query = SELECT c_id,c_title,c_content,c_comment_num FROM t_sphinx_article
  28. #####以下是用来过滤或条件查询的属性############
  29. #sql_attr_ 开头的表示一些属性字段,你原计划要用在where,orderby,groupby中的字段要在这里定义
  30. # unsigned integer attribute declaration
  31. sql_attr_uint = c_comment_num # 无符号整数属性
  32. sql_attr_uint = c_id # 无符号整数属性
  33. # boolean attribute declaration
  34. # sql_attr_bool = is_deleted
  35. # bigint attribute declaration
  36. # sql_attr_bigint = my_bigint_id
  37. # UNIX timestamp attribute declaration
  38. # sql_attr_timestamp = posted_ts
  39. # floating point attribute declaration
  40. # sql_attr_float = lat_radians
  41. # string attribute declaration
  42. sql_field_string = c_title
  43. sql_field_string = c_content
  44. # JSON attribute declaration
  45. # sql_attr_json = properties
  46. # combined field plus attribute declaration (from a single column)
  47. # stores column as an attribute, but also indexes it as a full-text field
  48. #
  49. # sql_field_string = author
  50. }

然后设置数据源的索引

  
  
  1. index d_sphinx_testdb_index
  2. {
  3. #数据源名
  4. source = d_sphinx_testdb
  5. # 索引记录存放目录
  6. path = /usr/local/sphinx/var/data/d_sphinx_testdb_index
  7. # 文档信息存储方式
  8. docinfo = extern
  9. #缓存数据内存锁定
  10. mlock = 0
  11. # 形态学
  12. morphology = none
  13. # 索引的词最小长度
  14. min_word_len = 1
  15. #数据编码
  16. charset_type = utf-8
  17. #最小前缀
  18. min_prefix_len = 0
  19. #最小中缀
  20. min_infix_len = 1
  21. }
  22. indexer
  23. {
  24. # 内存限制
  25. mem_limit = 32M
  26. }
  27. searchd
  28. {
  29. # 监听端口
  30. listen = 9312
  31. # 服务进程日志
  32. log = /usr/local/sphinx/log/searchd.log
  33. # 客户端查询日志
  34. query_log = /usr/local/sphinx/log/query.log
  35. # 请求超时
  36. read_timeout = 5
  37. # 同时可执行的最大searchd 进程数
  38. max_children = 30
  39. #进程ID文件
  40. pid_file = /usr/local/sphinx/log/searchd.pid
  41. # 查询结果的最大返回数
  42. max_matches = 1000
  43. # 是否支持无缝切换,做增量索引时通常需要
  44. seamless_rotate = 1
  45. }

进入 bin 目录,执行

  
  
  1. ./indexer 索引名
  2. Sphinx 2.2.5-id64-release (r4825)
  3. Copyright (c) 2001-2014, Andrew Aksyonoff
  4. Copyright (c) 2008-2014, Sphinx Technologies Inc (http://sphinxsearch.com)
  5. using config file '/usr/local/sphinx/etc/sphinx.conf'...
  6. indexing index 'd_sphinx_testdb_index'...
  7. collected 1000 docs, 0.4 MB
  8. sorted 0.0 Mhits, 100.0% done
  9. total 1000 docs, 408329 bytes
  10. total 0.041 sec, 9739278 bytes/sec, 23851.54 docs/sec
  11. total 1006 reads, 0.002 sec, 1.0 kb/call avg, 0.0 msec/call avg
  12. total 14 writes, 0.001 sec, 106.9 kb/call avg, 0.1 msec/call avg
  
  
  1. # 启动
  2. /usr/local/sphinx/bin/searchd
  3. #停止
  4. /usr/local/sphinx/bin/searchd --stop

测试前需要安装测试环境,以前 sphinx 的 bin 目录里面有个自带 search 程序,新版本没有了,所以只好使用api方式调用了。

这里我采用 linux + apache + php + sphinx 的方式来完整测试吧。

Apache 的最新版本请参考 官网下载页面.
我这里下载的是 httpd-2.4.10 最新的 apache 源码版本。

  
  
  1. wget http://apache.dataguru.cn//httpd/httpd-2.4.10.tar.gz
  2. tar zxvf httpd-2.4.10.tar.gz
  3. cd httpd-2.4.10/
  4. ./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so
  5. make && make install
  6. lsof -i # 查看端口占用情况
  7. /usr/local/apache/bin/apachectl -k start

然后访问 127.0.0.1:8080 就可以看到 It works! 了。

php 目前的最新版本 5.6.2, 建议去官网下载页下载最新版本

  
  
  1. wget http://cn2.php.net/distributions/php-5.6.2.tar.gz
  2. tar zxvf php-5.6.2.tar.gz
  3. cd php-5.6.2
  4. ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-mysql --enable-sockets --enable-shmop
  5. make && make install
  
  
  1. DirectoryIndex index.php index.html
  2. AddType application/x-httpd-php .php
  3. #调用 PHP 模块
  4. LoadModule php5_module modules/libphp5.so
  5. #特定的扩展名解析成 PHP
  6. <FilesMatch \.php$>
  7. SetHandler application/x-httpd-php
  8. </FilesMatch>
  
  
  1. <?php
  2. include('api/sphinxapi.php');
  3. $sphinx = new SphinxClient();
  4. $sphinx->setServer('127.0.0.1', 9312);
  5. $query =$_GET['query'];
  6. $res = $sphinx->query($query, 'd_sphinx_testdb_index');
  7. echo "<p>
  8. query = $query
  9. </p>";
  10. echo "<PRE>";
  11. print_r($res);
  12. echo "</pre> ";
  13. ?>

但是我报下面的错误

  
  
  1. ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory

原因:这主要是因为你安装库后,没有配置相应的环境变量.可以通过连接修正这个问题

  
  
  1. sudo ln /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18

但是还是报错,原来添加一个动态库后需要重新加载动态库。

  
  
  1. tiankonguse:bin # ldconfig

但是我又报错了

  
  
  1. ln: creating hard link `/usr/lib/libmysqlclient.so.18 ' => `/usr/local/mysql/lib/libmysqlclient.so.18': Invalid cross-device link

于是我只好创建软连接了。

  
  
  1. sudo ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
  
  
  1. tiankonguse:bin # ps -ef | grep search
  2. tiankonguse 9601 1 0 Oct28 ? 00:00:00 xs-searchd: master
  3. tiankonguse 9602 9601 0 Oct28 ? 00:00:00 xs-searchd: worker[1]
  4. tiankonguse 9603 9601 0 Oct28 ? 00:00:00 xs-searchd: worker[2]
  5. tiankonguse 9604 9601 0 Oct28 ? 00:00:00 xs-searchd: worker[3]
  6. root 32637 18048 0 21:12 pts/0 00:00:00 grep search

执行索引的时候,看到这个错误,搜索了一下,原来主键不能加入到属性中去。

  
  
  1. WARNING: attribute 'c_id' not found - IGNORING

参考文档 数据源配置:mysql数据源 WARNING: zero/NULL document_id, skipping .

  
  
  1. ERROR: index 'd_sphinx_testdb_index': No fields in schema - will not index

还是在这里找到了原因。

使用sql_attr设置的字段,只能作为属性,使用SphinxClient::SetFilter()进行过滤;
未被设置的字段,自动作为全文检索的字段,使用SphinxClient::Query(“搜索字符串”)进行全文搜索

而我把所有字段都设置为 sql_attr 了,于是把需要全文索引的字段去掉。终于跑出一些接过来。

但是还有一些问题。

  
  
  1. WARNING: key 'sql_query_info' was permanently removed from Sphinx configuration. Refer to documentation for details.
  2. WARNING: key 'charset_type' was permanently removed from Sphinx configuration. Refer to documentation for details.

好吧,我说怎么没有在配置文件中看到 sql_query_info 的说明呢,原来已经删除了,那就注释掉吧。

还是搜主键搜到的原因是我的主键不是一个整数,而 sphinx 要求必须是一个整数。
但是添加整数主键后还是出现这个问题,于是继续查找,最后找到是 sphinx 的一个 bug.但是 google 搜索,又没有搜索出来,于是猜想可能哪里配置不对。

后来启动 ngram, 设置ngram_chars后,这个警告就没有了。

  
  
  1. WARNING: source : skipped 300 document(s) with zero/NULL ids
  2. WARNING: word overrun buffer, clipped!!!
  3. WARNING: 601 duplicate document id pairs found

在 config apache 的时候,提示下面的错误。

  
  
  1. configure: error: APR not found. Please read the documentation.

官网的安装页面 可以看到有个 Requirements 类表。

apache 需要依赖于下面的一些东西。

  • APR and APR-Util
  • Perl-Compatible Regular Expressions Library (PCRE)
  • Disk Space
  • ANSI-C Compiler and Build System
  • Accurate time keeping
  • Perl 5 [OPTIONAL]

然后我们可以 apr 的官网下载对应的东西即可。

其实就是在 apr下载页面 找到下载链接 .

当然我们还要下载 apr-util .

安装apr
  
  
  1. wget http://apache.fayea.com/apache-mirror//apr/apr-1.5.1.tar.gz
  2. tar zxvf apr-1.5.1.tar.gz
  3. cd apr-1.5.1
  4. ./configure --prefix=/usr/local/apr
  5. make && make install
  6. #提示
  7. Libraries have been installed in:
  8. /usr/local/apr/lib
  9. If you ever happen to want to link against installed libraries
  10. in a given directory, LIBDIR, you must either use libtool, and
  11. specify the full pathname of the library, or use the `-LLIBDIR'
  12. flag during linking and do at least one of the following:
  13. - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
  14. during execution
  15. - add LIBDIR to the `LD_RUN_PATH' environment variable
  16. during linking
  17. - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
  18. - have your system administrator add LIBDIR to `/etc/ld.so.conf'

#### 安装 apr-util

  
  
  1. wget http://mirrors.cnnic.cn/apache//apr/apr-util-1.5.4.tar.gz
  2. tar zxvf apr-util-1.5.4.tar.gz
  3. cd apr-util-1.5.4
  4. ./configure --prefix=/usr/local/apr-util
  5. #提示不能找到 APR, 于是指定 APR 的位置
  6. configure: error: APR could not be located. Please use the --with-apr option.
  7. ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
  8. make && make install
  9. # 运行完后得到下面的提示
  10. Libraries have been installed in:
  11. /usr/local/apr-util/lib
  12. If you ever happen to want to link against installed libraries
  13. in a given directory, LIBDIR, you must either use libtool, and
  14. specify the full pathname of the library, or use the `-LLIBDIR'
  15. flag during linking and do at least one of the following:
  16. - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
  17. during execution
  18. - add LIBDIR to the `LD_RUN_PATH' environment variable
  19. during linking
  20. - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
  21. - have your system administrator add LIBDIR to `/etc/ld.so.conf'
  22. See any operating system documentation about shared libraries for
  23. more information, such as the ld(1) and ld.so(8) manual pages.
  
  
  1. /usr/local/apache/bin/apachectl -k start
  2. AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
  3. (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
  4. no listening sockets available, shutting down
  5. AH00015: Unable to open logs

不管那么多,直接修改 ServerName 和监听端口。

  
  
  1. Listen 8080
  2. ServerName 127.0.0.1:8080

然后再启动就成功了。

php和 apache 都安装完之后,访问写的一个php页面,浏览器却要下载页面。

于是查看 php 的官方文档,还真找到了。

简单的说就是编译 apache 的时候,需要添加启动so选项

  
  
  1. ./configure --enable-so

编译php的时候,指定加载为 apache 模块,并使用 mysql.

  
  
  1. ./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql

编写了一个php测试 sphinx ,但是提示没有这个文件,那就需要去网上找一个。

  
  
  1. PHP Warning: include(sphinxclient.php): failed to open stream: No such file or directory in /usr/local/apache/htdocs/index.php on line 2

下载 sphinx api 文件后,提示下面的错误

  
  
  1. PHP Parse error: syntax error, unexpected T_STRING in /usr/local/apache/htdocs/api/sphinxapi.php on line 28

这里找到解决方案的,原来下载的文件中由特殊字符,与我处理了一下特殊字符就ok了。

在配置中文的时候,提示下面的错误,后来发现是由于粘贴的字符串中反斜杠有问题,反斜杠后面不能有空白。

  
  
  1. ERROR: unknown key name 'U' in /usr/local/sphinx/etc/sphinx.conf line 515 col 6.
  2. FATAL: failed to parse config file '/usr/local/sphinx/etc/sphinx.conf'

这里找到了答案,原来默认编译安装的 php不支持 socket,需要加上支持socket的参数 --enable-sockets

  
  
  1. Fatal error: Call to undefined function socket_create()

和上面的问题一样,编译php时没有开启shmop_open,加上 ` –enable-shmop`参数即可。参考这里

  
  
  1. Fatal error: Call to undefined function shmop_open() in

得到下面的错误的原因是配错ip和host了。默认 sphinx的 post是9312,而我配成8080了.

  
  
  1. expected searchd protocol version 1+, got version '0'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值