Sphinx is a simple, relevance and open source full-text search server. It is written in C++ programming language and works with Linux and other popular operating systems. This tutorial will help you to install and configure Sphinx full-text search server on Ubuntu 16.04, 14.04 LTS operating systems.
Prerequisites
Before you begin this guide, you must have the followings.
Step 1 — Install Sphinx
Installing Sphinx on Ubuntu is easy because it’s in the native package repository. Install it using the apt-get package manager on your Ubuntu system.
sudo add-apt-repository ppa:builds/sphinxsearch-rel22 sudo apt-get update sudo apt-get install sphinxsearch
Step 2 – Import MySQL Database
Let’s import the sample SQL file into the database.First, create a database named test in your MySQL server, after that restore the database provided by the sphinx search package.
sudo mysqladmin -u root -p create test sudo mysql -u root -p test < /usr/share/doc/sphinxsearch/example-conf/example.sql
Step 3 – Configure Sphinx
Edit sphinx configuration as below and edit for the MySQL connection configuration as showing below.
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
Step 4 – Running Indexer
Run the indexer to create the full-text index from your data. The indexer is the first of the two principal tools as part of Sphinx. It works for gathering the data that will be searchable. You will see the results like below.
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'... collected 4 docs, 0.0 MB sorted 0.0 Mhits, 100.0% done total 4 docs, 193 bytes total 0.001 sec, 99382 bytes/sec, 2059.73 docs/sec skipping non-plain index 'dist1'... skipping non-plain index 'rt'... total 8 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg total 24 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
Step 5 – Starting Sphinx
Also, configure you Sphinx server to auto start on system boot. Use below command to set START to yes.
sudo sed -i 's/START=no/START=yes/g' /etc/default/sphinxsearch
Now also start service for the first time and check the status.
service sphinxsearch start service sphinxsearch status
You can also configure the indexer in your crontab to run it on a regular interval. The below crontab will run on every hour.
0 * * * * /usr/bin/indexer --rotate --all
Step 6 – Working with Sphinx
Let's execute some queries on your Sphinx server. First connect to Sphinx MySQL server using the following ocmmand.
mysql -h0 -P9306
Now run one by one command below and see the changes. This is for your own learning only.
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;