Sphinx是一个基于SQL的全文检索引擎,可以结合MySQL,PostgreSQL做全文搜索,它可以提供比数据库本身更专业的搜索功能,使得应用程序更容易实现专业化的全文检索。Sphinx特别为一些脚本语言设计搜索API接口,如PHP,Python,Perl,Ruby等,同时为MySQL也设计了一个存储引擎插件。
Sphinx 单一索引最大可包含1亿条记录,在1千万条记录情况下的查询速度为0.x秒(毫秒级)。Sphinx创建索引的速度为:创建100万条记录的索引只需 3~4分钟,创建1000万条记录的索引可以在50分钟内完成,而只包含最新10万条记录的增量索引,重建一次只需几十秒。
安装
wget http://sphinxsearch.com/files/archive/sphinx-0.9.9.tar.gz
tar -zxvf sphinx-0.9.9.tar.gz
cd sphinx-0.9.9/
./configure --prefix=/usr/local/sphinx --with-mysql --with-python --enable-id64
make
make install
可能出现的问题:
1、./configure时出错
# 1、安装libmysqlclient-dev
sudo apt-get install libmysqlclient-dev
# 2、查看mysql_config位置,我的在/usr/bin/mysql_config
which mysql_config
./configure --prefix=/usr/local/sphinx --with-mysql=/usr --with-python --enable-id64
make && make install
2、make时出错
cd sphinx-0.9.9/src
vim sphinxexpr.cpp
快速跳到1047行修改(:行号,另外显示行号:set nu)
还有两行也需要修改,具体哪一行看报错信息。
配置
sphinx.conf.dist是配置模板,可以使用cp sphinx.conf.dist sphinx.conf然后在sphinx.conf上修改配置,或者直接新建sphinx.conf文件。
cd /usr/local/sphinx/etc
touch sphinx.conf
如何配置?
更多配置请看https://blog.youkuaiyun.com/phachon/article/details/52450970
配置完之后:
# 生成索引
/usr/local/sphinx/bin/indexer --all
# 打开 sphinx 进程
/usr/local/sphinx/bin/searchd
实例
配置文件:
source document
{
type = mysql
sql_host = 127.0.0.1
sql_user = root
sql_pass =
sql_db = blog
sql_port = 3306
sql_query_pre = SET NAMES UTF8
sql_query = SELECT art_id,title,content FORM