如何在 Ubuntu 16.04 上安裝和設定 Sphinx


在本文中,我們將學習如何在 Ubunt 16.04 上安裝和設定 Sphinx。Sphinx 是一個開源搜尋引擎,它允許進行全文搜尋,並且在對大量資料進行搜尋方面表現出色,這些資料可以來自任何來源(例如:SQL 資料庫、純文字檔案等)。

Sphinx 的特性

  • 高階索引和良好的查詢工具。

  • 高搜尋效能和索引。

  • 用於後處理的高階結果。

  • 易於擴充套件,支援高階搜尋。

  • 可以與 SQL 和 XML 資料來源整合。

  • 可以擴充套件以處理包含數千個查詢的大量資料。

先決條件

在開始之前,我們需要一些先決條件。

  • 我們需要一臺 Ubuntu 機器,以及一個具有 sudo 許可權的非 root 使用者。

  • 機器上已安裝 MySQL。

在機器上安裝 Sphinx

我們可以使用 Ubuntu 的原生軟體包倉庫透過 apt-get 直接安裝 Sphinx,以下是安裝 Sphinx 的命令。

$ sudo apt-get install sphinxsearch
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libmysqlclient20 libstemmer0d
The following NEW packages will be installed:
libmysqlclient20 libstemmer0d sphinxsearch
0 upgraded, 3 newly installed, 0 to remove and 92 not upgraded.
Need to get 2,608 kB of archives.
After this operation, 20.5 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntuxenial/universe amd64 libstemmer0d amd 64 0+svn585-1 [62.1 kB]
Get:2 http://in.archive.ubuntu.com/ubuntuxenial-updates/main amd64 libmysqlclie nt20 amd64 5.7.15-0ubuntu0.16.04.1 [809 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu xenial/universe amd64 sphinxsearch amd 64 2.2.9-1build1 [1,737 kB]
Fetched 2,608 kB in 2s (986 kB/s)
Selecting previously unselected package libstemmer0d:amd64.
(Reading database ... 117542 files and directories currently installed.)
Preparing to unpack .../libstemmer0d_0+svn585-1_amd64.deb ...
Unpacking libstemmer0d:amd64 (0+svn585-1) ...
Selecting previously unselected package libmysqlclient20:amd64.
Preparing to unpack .../libmysqlclient20_5.7.15-0ubuntu0.16.04.1_amd64.deb ...
Unpacking libmysqlclient20:amd64 (5.7.15-0ubuntu0.16.04.1) ...
Selecting previously unselected package sphinxsearch.
Preparing to unpack .../sphinxsearch_2.2.9-1build1_amd64.deb ...
Unpacking sphinxsearch (2.2.9-1build1) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu4) ...
Setting up libstemmer0d:amd64 (0+svn585-1) ...
Setting up libmysqlclient20:amd64 (5.7.15-0ubuntu0.16.04.1) ...
Setting up sphinxsearch (2.2.9-1build1) ...
Adding system user `sphinxsearch' (UID 119) ...
Adding new group `sphinxsearch' (GID 125) ...
Adding new user `sphinxsearch' (UID 119) with group `sphinxsearch' ...
Not creating home directory `/var/run/sphinxsearch'.
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu4) ...

為 Sphinx 建立測試資料庫

現在,我們必須使用預設情況下隨軟體包提供的示例資料建立一個測試資料庫,這將允許您在後續步驟中測試 Sphinx 搜尋。

讓我們登入到 MySQL,在這裡我們將建立測試資料庫並匯入示例資料庫。

$ mysql –u root –p
mysql> create database test;
Query OK, 1 row affected (0.01 sec)
mysql> SOURCE /etc/sphinxsearch/example.sql;
Query OK, 0 rows affected, 1 warning (0.01 sec)
Query OK, 0 rows affected (0.03 sec)
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 10 rows affected (0.01 sec)
Records: 10 Duplicates: 0 Warnings: 0
Mysql> quit

配置 Sphinx 以進行搜尋

在 Sphinx 中,我們需要編輯和配置 3 個主要模組以適應我們的環境,其中定義了索引、搜尋和源等基本要素。這些模組位於配置檔案 sphinx.conf 中,該檔案位於 /etc/sphinxsearch/sphinx.conf.sample。為此,我們需要將現有的示例配置檔案複製到 /etc/sphinxsearch 資料夾。

$ cp /etc/sphinxsearch/sphinx.conf.sample /etc/sphinxsearch/sphinx.conf
$ sudo vi /etc/sphoxsearch/sphinx.conf

配置文 件應如下所示,包含以下模組

sphinx.conf 中的 Source 模組

source src1
{
   type = mysql
   #SQL settings (for ‘mysql’ and ‘pgsql’ types)
   sql_host = localhost
   sql_user = roo
   tsql_pass = ubuntu
   sql_db = test
   sql_port = 3306 # optional, default is 3306
   sql_query = \
   SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
   FROM documents
   sql_attr_uint = group_id
   sql_attr_timestamp = date_added
}

sphinx.conf 中的 Index 模組

index test
{
   source = src1
   path = /var/lib/sphinxsearch/data/test
   docinfo = extern
}
Searchd block in sphinx.conf
searchd
{
   listen = 9312:sphinx #SphinxAPI port
   listen = 9306:mysql41 #SphinxQL port
   log = /var/log/sphinxsearch/searchd.log
   query_log = /var/log/sphinxsearch/testquery.log
   read_timeout = 5
   max_children = 30
   pid_file = /var/run/sphinxsearch/testsearchd.pid
   seamless_rotate = 1
   preopen_indexes = 1
   unlink_old = 1
   binlog_path = /var/lib/sphinxsearch/datatest
}

編輯完配置後,我們需要對 Sphinx 進行索引。

管理 Sphinx 上的索引

在這裡,我們將使用我們在前面步驟中編輯的配置檔案進行索引。

$ sudo indexer –all
Sphinx 2.2.9-id64-release (rel22-r5006)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file '/etc/sphinxsearch/sphinx.conf'...
indexing index 'test'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.007 sec, 24319 bytes/sec, 504.03 docs/sec
total 4 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
total 12 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg

在生產環境中,我們需要使索引保持最新,因此我們將為此建立一個 cron 作業。

$ crontab –e

將以下內容新增到檔案的末尾。

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
@hourly /usr/bin/indexer --rotate --config /etc/sphinxsearch/sphinx.conf –all

啟動 Sphinx 服務

由於我們已使用配置檔案配置了索引,因此現在需要編輯 Sphinx 配置檔案。預設情況下,Sphinx 守護程式未啟動,我們需要編輯此檔案 /etc/default/sphinxsearch。

$ vi /etc/default/sphinxsearch
#
# Settings for the sphinxsearch searchd daemon
# Please read /usr/share/doc/sphinxsearch/README.Debian for details.
#
# Should sphinxsearch run automatically on startup? (default: no)
# Before doing this you might want to modify /etc/sphinxsearch/sphinx.conf
# so that it works for you.
START=yes

以下是啟動 Sphinx 守護程式的命令。

$ sudo systemctl restart sphinxsearch.services

重新啟動 sphinxsearch 服務後,我們將使用以下命令檢查狀態。

$ sudo systemctl status sphinxsearch.service
sphinxsearch.service - LSB: Fast standalone full-text SQL search engine
Loaded: loaded (/etc/init.d/sphinxsearch; bad; vendor preset: enabled)
Active: active (exited) since Mon 2016-09-19 13:00:20 IST; 1h 10min ago
Docs: man:systemd-sysv-generator(8)
Tasks: 0 (limit: 512)
Memory: 0B
CPU: 0
Sep 19 13:00:20 ubuntu-16 systemd[1]: Starting LSB: Fast standalone full-text SQL search engine...
Sep 19 13:00:20 ubuntu-16 sphinxsearch[7804]: To enable sphinxsearch, edit /etc/default/sphinxsearch and set START=ye
Sep 19 13:00:20 ubuntu-16 systemd[1]: Started LSB: Fast standalone full-text SQL search engine.

測試 Sphinx 搜尋

現在,我們將使用 MySQL 介面透過埠 9306 連線到 SphinxQL。

$ mysql -h0 -P9306
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 2.2.9-id64-release (rel22-r5006)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

在資料庫中搜索單詞“test”。

mysql> SELECT * FROM test WHERE MATCH('test '); SHOW META;
+------+----------+------------+
| id   | group_id | date_added |
+------+----------+------------+
|    1 |        1 | 1474272578 |
|    2 |        1 | 1474272578 |
|    4 |        2 | 1474272578 |
+------+----------+------------+
3 rows in set (0.00 sec)
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| total         | 3     |
| total_found   | 3     |
| time          | 0.000 |
| keyword[0]    | test  |
| docs[0]       | 3     |
| hits[0]       | 5     |
+---------------+-------+
6 rows in set (0.00 sec)

透過使用設定和配置,我們可以將 Sphinx 配置為一個功能強大的搜尋引擎,它更高效,並且可以處理海量資料。Sphinx 搜尋可以處理數十億個文件,並可以處理數 TB 的資料,每秒可以執行數千個搜尋查詢。

更新於: 2020年1月20日

356 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.