如何在Ubuntu 16.04和14.04上安装和配置Sphinx
时间:2019-05-19 01:26:33 来源:igfitidea点击:
Sphinx是一个简单、实用、开源的全文搜索服务器。
它是用c++编程语言编写的,可以与Linux和其他流行的操作系统一起工作。
本教程将在Ubuntu 16.04, 14.04 LTS操作系统上安装和配置Sphinx全文搜索服务器。
准备工作
在Ubuntu上安装MySQL服务器
步骤1 -安装Sphinx
在Ubuntu上安装Sphinx很容易,因为它在本地包存储库中。
在Ubuntu系统上使用apt-get包管理器安装它。
sudo add-apt-repository ppa:builds/sphinxsearch-rel22 sudo apt-get update sudo apt-get install sphinxsearch
步骤2 -导入MySQL数据库
让我们将示例SQL文件导入数据库。
首先,在MySQL服务器中创建一个名为test的数据库,然后恢复sphinx搜索包提供的数据库。
sudo mysqladmin -u root -p create test sudo mysql -u root -p test < /usr/share/doc/sphinxsearch/example-conf/example.sql
步骤3 -配置Sphinx
编辑sphinx配置文件,设置MySQL连接。
sudo vi /etc/sphinxsearch/sphinx.conf
source src1
{
# data source type. mandatory, no default value
# known types are mysql, pgsql, mssql, xmlpipe, xmlpipe2, odbc
type = mysql
#####################################################################
## SQL settings (for 'mysql' and 'pgsql' types)
#####################################################################
# some straightforward parameters for SQL source types
sql_host = localhost
sql_user = root
sql_pass = secret
sql_db = test
sql_port = 3306 # optional, default is 3306
步骤4 -运行索引器
运行索引器从数据创建全文索引。
索引器是Sphinx的两个主要工具中的第一个。
它用于收集可搜索的数据。
$ sudo indexer --all Sphinx 2.2.11-id64-release (95ae9a6) Copyright (c) 2001-2016, Andrew Aksyonoff Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com) using config file '/etc/sphinxsearch/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.006 sec, 30791 bytes/sec, 638.16 docs/sec indexing index 'test1stemmed'...
步骤5 -启动Sphinx
另外,将Sphinx服务器配置为在系统引导时自动启动。
使用下面的命令将START设置为yes。
sudo sed -i 's/START=no/START=yes/g' /etc/default/sphinxsearch
现在也第一次启动服务并检查状态。
service sphinxsearch start service sphinxsearch status
我们还可以在crontab中配置索引器,使其定期运行。
下面的crontab将每小时运行一次。
0 * * * * /usr/bin/indexer --rotate --all
步骤6 -使用Sphinx
让我们在Sphinx服务器上执行一些查询。
首先使用下面命令连接到Sphinx MySQL服务器。
mysql -h0 -P9306
现在逐个运行下面的命令并查看更改。
mysql> SELECT * FROM test1 WHERE MATCH('document');
mysql> INSERT INTO rt VALUES (1, 'adding', 'sample text here', 11);
mysql> INSERT INTO rt VALUES (2, 'adding some more', 'sample text here', 22);
mysql> SELECT gid/11 FROM rt WHERE MATCH('sample') GROUP BY gid;
mysql> SELECT * FROM rt ORDER BY gid DESC;
mysql> SELECT *, WEIGHT() FROM test1 WHERE MATCH('"document one"/1');
mysql> SHOW META;
mysql> SET profiling=1;
mysql> SELECT * FROM test1 WHERE id IN (1,2,4);
mysql> SHOW PROFILE;
mysql> SELECT id, id%3 idd FROM test1 WHERE MATCH('this is | nothing') GROUP BY idd;
mysql> SHOW PROFILE;
mysql> SELECT id FROM test1 WHERE MATCH('is this a good plan?');
mysql> SHOW PLAN;
mysql> SELECT COUNT(*) FROM test1;
mysql> CALL KEYWORDS ('one two three', 'test1');
mysql> CALL KEYWORDS ('one two three', 'test1', 1);
mysql> SHOW TABLES;

