Sphinx mysql 安装和测试

参考网站
http://www.sphinxsearch.com/wiki/doku.php?id=sphinx_chinese_tutorial
http://ri0day.blogbus.com/logs/41975837.html

 

 

==========================================================1 安装
 因为我的LAMP环境已经配置好,所以只需要安装sphinx。


sphinx-0.9.8与MySQL5.1.x一起使用会出错,换成低版本的MySQL5.0.x


sudo apt-get install libmysqlclient15-dev


wget http://www.sphinxsearch.com/downloads/sphinx-0.9.8.1.tar.gz

 

tar xzvf sphinx-0.9.8.1.tar.gz

 

cd sphinx-0.9.8.1/

 

./configure --prefix /usr/local/sphinx --with-mysql
make
sudo make install

 


==========================================================2 测试sphinx自带的demo。
  1、导入数据库文件example.sql。
      该文件在test数据库新建立一个documents,并插入基本的测试数据。
  2、修改sphinx.conf
cd /usr/local/sphinx/etc
cp sphinx.conf.dist sphinx.conf
vim sphinx.conf

 

主要修改以下几个参数
第一、连接数据库的用户名和密码


        sql_user        = root
  sql_pass        = ***** #你自己的密码


第二、索引路径

path      = /usr/local/sphinx/var/data/test1


==========================================================3 建立索引
indexer是建立索引的程序,具体参数可以在命令行下查看帮助


root@ubuntu-desktop:/usr/local/sphinx/etc# /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf test1
Sphinx 0.9.8.1-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff

using config file '/usr/local/sphinx/etc/sphinx.conf'...
indexing index 'test1'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.245 sec, 788.77 bytes/sec, 16.35 docs/sec

 

 

 

 

 

 

 

==========================================================4 开启查询的守护进程

/usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf

 


E:\web\sphinx>bin\searchd --config sphinx.conf
Sphinx 0.9.8-release (r1371)
Copyright (c) 2001-2008, Andrew Aksyonoff

WARNING: forcing --console mode on Windows
using config file 'sphinx.conf'...
WARNING: index 'test1stemmed': preload: failed to open @CONFDIR@/data/test1stemm
ed.sph: No such file or directory; NOT SERVING
WARNING: index 'dist1': no such local index 'test1stemmed' - SKIPPING LOCAL INDE
X
creating server socket on 0.0.0.0:3312
accepting connections


注意开启3312端口,并修改相应的参数

 

==========================================================5 查询测试。
查询了两次,分别是ggart和another。结合数据库查看,结果是正确的。

root@ubuntu-desktop:/usr/local/sphinx/bin# /usr/local/sphinx/bin/search -c /usr/local/sphinx/etc/sphinx.conf ggart
Sphinx 0.9.8.1-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff

using config file '/usr/local/sphinx/etc/sphinx.conf'...
index 'test1': query 'ggart ': returned 0 matches of 0 total in 0.000 sec

words:
1. 'ggart': 0 documents, 0 hits

index 'test1stemmed': search error: failed to open /usr/local/sphinx/var/data/test1stemmed.sph: No such file or directory.

 

 

 

root@ubuntu-desktop:/usr/local/sphinx/bin# /usr/local/sphinx/bin/search -c /usr/local/sphinx/etc/sphinx.conf another
Sphinx 0.9.8.1-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff

using config file '/usr/local/sphinx/etc/sphinx.conf'...
index 'test1': query 'another ': returned 1 matches of 1 total in 0.000 sec

displaying matches:
1. document=3, weight=2, group_id=2, date_added=Sat Aug  8 23:34:22 2009
        id=3
        group_id=2
        group_id2=7
        date_added=2009-08-08 23:34:22
        title=another doc
        content=this is another group

words:
1. 'another': 1 documents, 2 hits

index 'test1stemmed': search error: failed to open /usr/local/sphinx/var/data/test1stemmed.sph: No such file or directory.

 

 

 

 

 

===================================调用Sphinx:未完待续

 

应用程序如果想调用Sphinx,可以从两个方面:

一是通过Sphinx官方提供的API接口(接口有Python,Java,Php三种版本)

二是通过安装SphinxSE(具体见1.2部分),然后创建一个中介sphinxSE类型的表,再通过执

行特定的SQL语句实现。

 

------------------通过官方API调用Sphinx(以PHP为例)

在sphinx解压目录有一个API目录,里面有三个PHP文件:test.php,test2.php和

sphinxapi.php。sphinxapi.php是sphinx调用接口封装文件,test.php是一个在命令行下执

行的查询例子文件,test2.php是一个生成摘要的例子文件。

在命令下行运行test.php(Linux上没有API目录,需要从源程序包中复制api目录

至/usr/local/sphinx)
cd /usr/local/sphinx
cp -rf /tmp/sphinx-0.9.8.1/api/ ./

Linux上(php在/usr/local/php目录???????????????????????????????????
,sphinx.conf在/usr/local/sphinx目录):
cd /usr/local/sphinx
/usr/local/php/bin/php api/test.php -i cgfinal CGArt

 

test2.php是一个摘要生成的例子文件,如果你的本地机器已装好sphinx,php运行环境,你

可以通过浏览器看查看test2.php的运行效果。


-------------------采用SphinxSE方式调用Sphinx
采用sphinxSE必须要求为mySQL安装sphinxSE Engine驱动
进入mysql命令行,运行show engines,看是不是有一个叫sphinx的engine,有的话就表示

sphinxSE(mysql的sphinx引擎)安装正常了


要创建一张sphinx 专用表,你可以这样建
CREATE TABLE `sphinx` (
`id` int(11) NOT NULL,
`weight` int(11) NOT NULL,
`query` varchar(255) NOT NULL,
`CATALOGID` INT NOT NULL,
`EDITUSERID` INT NOT NULL,
`HITS` INT NULL,
`ADDTIME` INT NOT NULL,
KEY `Query` (`Query`)
) ENGINE=SPHINX DEFAULT CHARSET=utf8

CONNECTION='sphinx://localhost:3312/cgfinal';
注:与一般mysql表不同的是ENGINE=SPHINX DEFAULT CHARSET=utf8

CONNECTION='sphinx://localhost:3312/cgfinal';,这里表示这个表采用SPHINXSE引擎,字

符集是utf8,与sphinx的连接串是'sphinx://localhost:3312/cgfinal,cgfinal是索引名称

 

 

 

 

你可能感兴趣的:(sql,PHP,mysql,linux,ubuntu)