Sphinx is an open source search engine specially designed to provide search functionality to client applications. Sphinx can also be used for performing searches over large volumes of data easily. Sphinx can be easily integrated with SQL and XML sources. It is very good tool for querying and advanced indexes.
In this tutorial, we will show you how to install and configure sphinx on Ubuntu 16.04.
Requirements
- A server runing Ubuntu-16.04.
- A non-root user with sudo privileges setup on your server.
Installing Sphinx
Before starting, update your system to the latest version with the following command:
sudo apt-get update -y
sudo apt-get upgrade -y
Once your system is up-to-date, you can easily install it with the following command:
sudo apt-get install sphinxsearch
Output:
Reading state information... Done
The following packages were automatically installed and are no longer required:
libc-ares2 libcitadel4 libev4 libical1a libsieve2-1
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
libstemmer0d
The following NEW packages will be installed:
libstemmer0d sphinxsearch
(truncated...)
Installing MariaDB Database
Next, you will need to install MariaDB for database functionality.
You can install it with the following command:
sudo apt-get install mariadb-server
The MariaDB default installation is not secure, so you will need to secure it first. You can secure it with the mysql_secure_installation script:
sudo mysql_secure_installation
Answer all the questions as shown below:
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
(truncated..)
Creating the Test Database
Next, you will need to create a test database using the sample data in the SQL file provided with the package.
First, login to the database server with the following command:
mysql -u root -p
Enter the password for MariaDB root user, you should see the following output:
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 39
Server version: 10.0.27-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MariaDB [(none)]>
Next, create a test database and import the example SQL file.
MariaDB [(none)]> CREATE DATABASE test;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> SOURCE /etc/sphinxsearch/example.sql;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected (0.04 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.03 sec)
Query OK, 10 rows affected (0.01 sec)
Records: 10 Duplicates: 0 Warnings: 0
MariaDB [(none)]> quit;
Bye
Configuring Sphinx
Next, you will need to copy the sample sphinx configuration file to /etc/sphinxsearch/ directory and configure 3 main blocks to suit your environment:
sudo cp /etc/sphinxsearch/sphinx.conf.sample /etc/sphinxsearch/sphinx.conf
sudo nano /etc/sphinxsearch/sphinx.conf
Change the file as shown below:
source src1
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = root-password
sql_db = test
sql_port = 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
}
index test1
{
source = src1
path = /var/lib/sphinxsearch/data/test1
docinfo = extern
}
searchd
{
listen = 9306:mysql41
log = /var/log/sphinxsearch/searchd.log
query_log = /var/log/sphinxsearch/query.log
read_timeout = 5
max_children = 30
pid_file = /var/run/sphinxsearch/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
binlog_path = /var/lib/sphinxsearch/data
}
Once you are finished, you will need to Index the Sphinx.
You can add data to the index using the configuration you created earlier with the following command:
sudo indexer --all
You should see the following output:
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 'test1'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.043 sec, 4474 bytes/sec, 92.73 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
It is recommended to keep the index up to date in production environment.
You must create a cron job for this:
sudo nano /etc/crontab
Add the following line to the end of the file:
@hourly /usr/bin/indexer --rotate --config /etc/sphinxsearch/sphinx.conf –all
By default the Sphinx daemon is not started at boot, so you will need to enable it to start at boot.
You can do this by editing /etc/default/sphinxsearch file:
sudo nano /etc/default/sphinxsearch
Change the line from START=no to START=yes:
START=yes
Save the file and restart the sphinxsearch service with the following command:
sudo systemctl restart sphinxsearch
You can check the status of sphinxsearch service with the following command:
sudo systemctl status sphinxsearch
Output:
● sphinxsearch.service - LSB: Fast standalone full-text SQL search engine
Loaded: loaded (/etc/init.d/sphinxsearch; bad; vendor preset: enabled)
Active: active (exited) since Wed 2016-11-09 23:32:39 IST; 24s ago
Docs: man:systemd-sysv-generator(8)
Process: 8532 ExecStop=/etc/init.d/sphinxsearch stop (code=exited, status=0/SUCCESS)
Process: 8539 ExecStart=/etc/init.d/sphinxsearch start (code=exited, status=0/SUCCESS)
Nov 09 23:32:38 Node1 sphinxsearch[8539]: Copyright (c) 2001-2015, Andrew Aksyonoff
Nov 09 23:32:38 Node1 sphinxsearch[8539]: Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)
(truncated...)
Testing the Sphinx Search
Once everything is set up, it’s time to test sphinxsearch.
First, connect to the SphinxQL using the port 9306 with the following command:
mysql -h0 -P9306
You should see the following output:
Welcome to the MariaDB 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, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MySQL [(none)]>
Next, search a word test in the databases:
MySQL [(none)]> SELECT * FROM test1 WHERE MATCH('test '); SHOW META;
You should see the following output:
+------+----------+------------+
| id | group_id | date_added |
+------+----------+------------+
| 1 | 1 | 1478713253 |
| 2 | 1 | 1478713253 |
| 4 | 2 | 1478713253 |
+------+----------+------------+
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)
In the above output you can see that Sphinx found 3 matches from our test1 index.
Next, you can exit from the Mysql shell:
MySQL [(none)]> quit