在上一篇文章中说到安装mysql和sphinx的mysql引擎sphinxSE的安装,这一篇说的是sphinx的安装和运行测试,关于下载和解压缩sphinx都用上一篇文章里说的。
进入到sphinx的源码文件夹里,运行下列命令就可以安装sphinx了:
ldconfig /usr/local/mysql/include/mysql
./configure --prefix=/usr/local/sphinx --with-mysql=/usr/local/mysql
make && make install
其中--prefix是指向sphinx的安装路径,--with-mysql是指向mysql的安装路径。如果上面都没有报错,那sphinx就成功安装了。
sphinx的配置文件为sphinx.conf,下面进行配置:
cp sphinx.conf.dist sphinx.conf
vim sphinx.conf
进入/usr/local/sphinx/etc文件夹下,看到该文件夹下有下面这些文件:
-rw-r--r--1 root root 1900311-0413:32 sphinx.conf.dist
-rw-r--r--1 root root 94811-0413:32 sphinx-min.conf.dist
把sphinx.conf.dist复制出来成sphinx.conf,并进入修改它的一些数据库的配置,主要是修改数据库地址,数据库用户、密码,还有数据库名这些,这里我们用安装mysql自带的test库进行测试。
运行该目录下的example.sql脚本,把数据导到数据库中:
然后进入mysql中查看添加的数据:
show databases;# 查看到有下面这些库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
#进入test库,查看到有下面这些表,其中documents表是自动导进来的:
mysql> use test
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| documents |
| eht_articles |
| tags |
+----------------+
3 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-0419:22:13| test one | this is my test document number one. also checking search within phrases. |
|2|1|6|2010-11-0419:22:13| test two | this is my test document number two |
|3|2|7|2010-11-0419:22:13| another doc | this is another group |
|4|2|8|2010-11-0419:22:13| doc number four | this is to test groups |
+----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+
4 rows in set (0.00 sec)
sphinx的配置文件也创建完了,数据也导进去了,接下来就用下面命令来创建索引:
创建索引是报了一个这样的错误:/usr/local/sphinx/bin/indexer: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or directory,主要原因是在上一篇中安装完mysql后没有设置环境变量,可以用下面方法解决:
cp /usr/local/mysql/lib/mysql/libmysqlclient.so.16/usr/lib/libmysqlclient.so.16 #然后把该文件的一个连接复制到在环境变量的文件夹/usr/lib/下
再次运行创建索引命令就能完成索引的创建了。
然后用下面命令进行搜索测试:
但此时报了一个search error: failed to open /usr/local/sphinx/var/data/test1.sph: No such file or directory.这样的错误,网上找了一下解决方法是在创建索引是加上--all参数,也就是:
--all参数官方文档的说明是:
--all
tells indexer
to update every index listed in sphinx.conf
, instead of listing individual indexes. This would be useful in small configurations, or cron
-type or maintenance jobs where the entire index set will get rebuilt each day, or week, or whatever period is best. Example usage
重新创建索引后再次运行测试 /usr/local/sphinx/bin/search test 就会出现下列结果:
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 in0.001 sec
displaying matches:
1. document=1, weight=2, group_id=1, date_added=Thu Nov 419:22:132010
id=1
group_id=1
group_id2=5
date_added=2010-11-0419: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 419:22:132010
id=2
group_id=1
group_id2=6
date_added=2010-11-0419: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 419:22:132010
id=4
group_id=2
group_id2=8
date_added=2010-11-0419: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 in0.000 sec
displaying matches:
1. document=1, weight=2, group_id=1, date_added=Thu Nov 419:22:132010
id=1
group_id=1
group_id2=5
date_added=2010-11-0419: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 419:22:132010
id=2
group_id=1
group_id2=6
date_added=2010-11-0419: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 419:22:132010
id=4
group_id=2
group_id2=8
date_added=2010-11-0419:22:13
title=doc number four
content=this is to test groups
words:
1. 'test': 3 documents, 5 hits
sphinx的安装和运行测试已完,下次讲sphinx的中文分词和python调用。