sphinx 简介和安装

󰃭 2016-04-21

简介

sphinx是一个全文搜索引擎, 可以为应用程序提供快速的全文搜索功能, 并且能够与数据库完美整合, 其服务本身也支持mysql 协议, 所以也能以sql 的方式执行查询(但并不完全支持所有的sql语句)。

应用程序可以通过以下三个方式访问sphinx 索引服务

  1. 通过mysql 协议访问, 这也是被官方推荐的方式
  2. 通过sphinx 的客户端API访问, sphinx 提供了各种语言的接口库, 包括PHP,perl,python,Ruby和java
  3. sphinx 还可以作为mysql的一个存储引擎, 即sphinxSE, 类似于mysql的myisam和innodb, 这样, 通过mysql 服务也能访问到索引数据。实际上, 这种存储引擎并不会真的存储任何数据, 他也仅仅是sphinx 内置在mysql服务中的一个客户端而已, 让用户能够通过mysql服务搜索索引

特性

  1. 高速的建立索引(在当代CPU上,峰值性能可达到10-15 MB/秒);
  2. 高性能的搜索(单核支持150-250的qps(queries/s),基于1,000,000 个文档或者1.2G的文本数据);
  3. 可处理海量数据(目前已知最大的单集群超过30亿个的文档数据,峰值时达到5000W/day的搜索请求);
  4. 提供了优秀的相关度算法,基于短语相似度和统计(BM25)的复合Ranking方法;
  5. 支持分布式搜索;
  6. 支持短语搜索
  7. 提供文档摘要生成
  8. 可作为MySQL的存储引擎提供搜索服务;
  9. 支持布尔、短语、词语相似度等多种检索模式;
  10. 文档支持多个全文检索字段(最大不超过32个);
  11. 文档支持多个额外的属性信息(例如:分组信息,时间戳等);

如何获取sphinx

sphinx的官网: http://sphinxsearch.com/ github: https://github.com/sphinxsearch/sphinx

sphinx 发布的安装包里包含了以下软件(主要的三个予以说明)

indexer

用于索引创建的命令

searchd

启动索引服务的命令

sphinxapi

各种流行的web脚本语言的客户端的库文件(PHP,Python,Perl, Ruby)

安装

sphinx 服务兼容大部分的带C++ 编译器的类Unix系统

我的系统

Linux ubuntu 3.16.0-30-generic #40~14.04.1-Ubuntu SMP Thu Jan 15 17:43:14 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

安装libmysqlclient-dev

sphinx 的服务要依赖 libmysqlclient-dev

sudo apt-get install libmysqlclient-dev

安装re2

再安装一个re2 库, 这是一个正则表达式引擎, 用于在编译的时候替换 sphinx 默认的正则引擎

wget https://github.com/google/re2/archive/2016-04-01.zip #下载最新的稳定版
unzip 2016-04-01.zip
cd re2-2016-04-01/
make
make install

sphinx 服务安装

wget https://github.com/sphinxsearch/sphinx/archive/2.2.10-release.zip #最新的稳定版本
unzip 2.2.10-release.zip
cd sphinx-2.2.10-release
./configure --with-re2  #with-re2 替换默认的正则引擎

当打印出

Thank you for choosing Sphinx!

就可以继续了, 否则会打印出各种检测状态,一般最后几行会报错 比如我安装的时候就出现了

checking whether to compile with RE2 library support... configure: error: missing re2 sources

是我的re2 没有安装成功, 检查之后才发现我make 后没有执行make install

继续

make
sudo make install

make的时候可能出现错误, 我在运行的时候就出现了

/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
In file included from sphinx.cpp:72:0:
/usr/local/include/re2/re2.h:734:11: error: ‘once_flag’ in namespace ‘std’ does not name a type
   mutable std::once_flag rprog_once_;

初步认为是gcc的版本问题. 遇到这个问题后, 重新执行./configure, 不过要带一些其他参数

./configure --with-re2 CXXFLAGS="-std=c++11"  

然后执行makemake install

至此, sphinx 的安装就完成了, 包括indexer,indextool, searchd几个命令就能执行了. 比如, 执行

searchd --help

就可以看到sphinx的版本信息和searchd服务启动脚本的其他帮助信息

sphinx API 在源码包的api 目录下, 后面的篇章我会说明对PHP和Python版本的sphinx api 使用

下一篇我们即将说明sphinx的索引是如何创建的