sphinx的介绍
Sphinx是一个基于SQL的全文检索引擎,可以结合MySQL,PostgreSQL做全文搜索,它可以提供比数据库本身更专业的搜索功能,使得应用 程序更容易实现专业化的全文检索。Sphinx特别为一些脚本语言设计搜索API接口,如PHP,Python,Perl,Ruby等,同时为MySQL 也设计了一个存储引擎插件。
sphinx的安装步骤
前提,你的机器上已经安装过mysql数据库。如果没有安装,可运行以下命令安装
sudo apt-get install mysql-client-5.0 mysql-server-5.0
- 下载sphinx的安装包sphinx-0.9.9.tar.gz
下载地址:http://www.sphinxsearch.com/downloads.html
- 解压 tar -zxvf sphinx-0.9.9.tar.gz
$ tar -zxvf sphinx-0.9.9.tar.gz
- 编译安装
$ cd sphinx-0.9.9
$ ./configure --prefix=/usr/local/sphinx
$ make
$ sudo make install
用Sphinx自带的示例测试是否安装成功
- 创建test数据库,并创建sphinx用户
mysql> create database test;
mysql> create user 'sphinx'@'localhost' identified by 'sphinx';
mysql> grant all privileges on test.* to 'sphinx'@'localhost';
- 指定sphinx配置文件
$ cd /usr/local/sphinx/etc
$ sudo cp sphinx.conf.dist sphinx.conf
- 编辑该配置文件
vi sphinx.conf
改动内容如下:
sql_host = localhost
sql_user = sphinx
sql_pass = sphinx
sql_db = test
sql_port = 3306 # optional, default is 3306
说明:加粗部分是修改的内容
- 生成sphinx自带示例所需的数据表及测试数据
mysql -uroot -p test < /usr/local/etc/example.sql
- 生成索引
$ sudo /usr/local/sphinx/bin/indexer --all
- 查询关键字"test",测试是否成功安装
$ /usr/local/sphinx/bin/search test
Sphinx 0.9.9-release (r2117)
Copyright (c) 2001-2009, Andrew Aksyonoff
using config file '/usr/local/etc/sphinx.conf'...
index 'test1': query 'test ': returned 3 matches of 3 total in 0.000 sec
displaying matches:
1. document=1, weight=2, group_id=1, date_added=Thu Feb 25 18:24:46 2010
id=1
group_id=1
group_id2=5
date_added=2010-02-25 18:24:46
title=test one
content=this is my test document number one. also checking search within phrases.
2. document=2, weight=2, group_id=1, date_added=Thu Feb 25 18:24:46 2010
id=2
group_id=1
group_id2=6
date_added=2010-02-25 18:24:46
title=test two
content=this is my test document number two
3. document=4, weight=1, group_id=2, date_added=Thu Feb 25 18:24:46 2010
id=4
group_id=2
group_id2=8
date_added=2010-02-25 18:24:46
title=doc number four
content=this is to test groups
words:
1. 'test': 3 documents, 5 hits
index 'test1stemmed': query 'test ': returned 3 matches of 3 total in 0.000 sec
displaying matches:
1. document=1, weight=2, group_id=1, date_added=Thu Feb 25 18:24:46 2010
id=1
group_id=1
group_id2=5
date_added=2010-02-25 18:24:46
title=test one
content=this is my test document number one. also checking search within phrases.
2. document=2, weight=2, group_id=1, date_added=Thu Feb 25 18:24:46 2010
id=2
group_id=1
group_id2=6
date_added=2010-02-25 18:24:46
title=test two
content=this is my test document number two
3. document=4, weight=1, group_id=2, date_added=Thu Feb 25 18:24:46 2010
id=4
group_id=2
group_id2=8
date_added=2010-02-25 18:24:46
title=doc number four
content=this is to test groups
words:
1. 'test': 3 documents, 5 hits
至此,你已经成功安装了sphinx,并通过了测试。