sphinx 是模拟mysql数据库协议,所以在安装sphinx之前要先装好mysql相关服务。
1.去sphinx官网下载需要的包 地址: http://sphinxsearch.com/downloads/
然后编译指定安装路径
./configure --prefix=/usr/local/sphinx --with-mysql=/usr/local/mysql make make install
其中--prefix是指向sphinx的安装路径,--with-mysql是指向mysql的安装路径。如果上面都没有报错,那sphinx就成功安装了。
sphinx的配置文件为sphinx.conf,下面进行配置:
进入/usr/local/sphinx/etc文件夹下,看到该文件夹下有下面这些文件:
-rw-r--r-- 1 root root 905 11-04 13:32 example.sql -rw-r--r-- 1 root root 19003 11-04 13:32 sphinx.conf.dist -rw-r--r-- 1 root root 948 11-04 13:32 sphinx-min.conf.dist
把sphinx.conf.dist复制出来成sphinx.conf,并进入修改它的一些数据库的配置,主要是修改数据库地址,数据库用户、密码,还有数据库名这些,这里我们用安装mysql自带的test库进行测试。
运行该目录下的example.sql脚本,把数据导到数据库中:
mysql -u mysql < /usr/local/sphinx/etc/example.sql
然后进入mysql中查看添加的数据:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->mysql #进入mysql show databases;# 查看到有下面这些库 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ rows in set (0.00 sec) #进入test库,查看到有下面这些表,其中documents表是自动导进来的: mysql> use test Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | documents | | eht_articles | | tags | +----------------+ rows in set (0.01 sec) 查询documents表就能看到下面记录: mysql> SELECT * FROM documents; +----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+ | id | group_id | group_id2 | date_added | title | content | +----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+ | 1 | 1 | 5 | 2010-11-04 19:22:13 | test one | this is my test document number one. also checking search within phrases. | | 2 | 1 | 6 | 2010-11-04 19:22:13 | test two | this is my test document number two | | 3 | 2 | 7 | 2010-11-04 19:22:13 | another doc | this is another group | | 4 | 2 | 8 | 2010-11-04 19:22:13 | doc number four | this is to test groups | +----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+ rows in set (0.00 sec)
sphinx的配置文件也创建完了,数据也导进去了,接下来就用下面命令来创建索引:
/usr/local/sphinx/bin/indexer --all
然后用下面命令进行搜索测试:
/usr/local/sphinx/bin/search test #test是搜索的关键字
搜索成功会出现下面的内容:
代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->Sphinx 0.9.9-release (r2117) Copyright (c) 2001-2009, Andrew Aksyonoff using config file '/usr/local/sphinx/etc/sphinx.conf'... index 'test1': query 'test ': returned 3 matches of 3 total in 0.001 sec displaying matches: 1. document=1, weight=2, group_id=1, date_added=Thu Nov 4 19:22:13 2010 id=1 group_id=1 group_id2=5 date_added=2010-11-04 19:22:13 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 Nov 4 19:22:13 2010 id=2 group_id=1 group_id2=6 date_added=2010-11-04 19:22:13 title=test two content=this is my test document number two 3. document=4, weight=1, group_id=2, date_added=Thu Nov 4 19:22:13 2010 id=4 group_id=2 group_id2=8 date_added=2010-11-04 19:22:13 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 Nov 4 19:22:13 2010 id=1 group_id=1 group_id2=5 date_added=2010-11-04 19:22:13 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 Nov 4 19:22:13 2010 id=2 group_id=1 group_id2=6 date_added=2010-11-04 19:22:13 title=test two content=this is my test document number two 3. document=4, weight=1, group_id=2, date_added=Thu Nov 4 19:22:13 2010 id=4 group_id=2 group_id2=8 date_added=2010-11-04 19:22:13 title=doc number four content=this is to test groups words: 1. 'test': 3 documents, 5 hits
到此安装成功并且测试成功