Configuring Sphinx on CentOS

As an open-source search engine, Sphinx is a popular choice for those looking to perform searches over large data efficiently. Its high indexing performance, advanced result post-processing, scalability and versatility make it stand out among similar software.

To set up Sphinx on a server running CentOS, you’ll need a sudo non-root user and mySQL already installed.

 

Installation

  • Right before installing Sphinx, you’ll need to install its dependencies, by entering this command:
sudo yum install -y postgresql-libs unixODBC
  • Next, head over to the tmp directory to download its files:
cd /tmp
  • Now download the latest version of Sphinx from the website, using wget:
wget http://sphinxsearch.com/files/sphinx-2.2.11-1.rhel7.x86_64.rpm
  • Using the yum command, install Sphinx:
sudo yum install -y sphinx-2.2.11-1.rhel7.x86_64.rp

 

Test database

  • It is important to create a “dummy” database to text the Sphinx search at a later time. First, you need to log in to the MySQL server shell, like so:
 mysql -u root -p
  • You’ll be asked for the password for the root user. At this point, the prompt will change to MariaDB>.
  • Now it’s time to create the test database:
CREATE DATABASE test;
  • Once that’s done, import the sample SQL file that comes in the package:
SOURCE /usr/share/doc/sphinx-2.2.11/example.sql;
  • Finally, leave the MySQL shell, by simply typing:
quit

 

Configuration

  • Sphinx’s configuration file, called sphinx.conf, should be located in /etc/sphinx. You need to move this file.
mv /etc/sphinx/sphinx.conf /etc/sphinx/sphinx.conf2
  • Next, you should create a new sphinx.conf file with a text editor of your choice.
sudo vi /etc/sphinx/sphinx.conf
  • Paste the following code:
source src1
{
  type          = mysql

  sql_host      = localhost
  sql_user      = root
  sql_pass      = your_root_mysql_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/sphinx/test1
  docinfo           = extern
}
searchd
{
  listen            = 9306:mysql41
  log               = /var/log/sphinx/searchd.log
  query_log         = /var/log/sphinx/query.log
  read_timeout      = 5
  max_children      = 30
  pid_file          = /var/run/sphinx/searchd.pid
  seamless_rotate   = 1
  preopen_indexes   = 1
  unlink_old        = 1
  binlog_path       = /var/lib/sphinx/

 

Final steps

  • Next, you should add some data to the index using the configuration file previously created.
sudo indexer --all
  • The output should look like this:
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/sphinx/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, 29765 bytes/sec, 616.90 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
  • To keep the index up to date, you need a Cron job. Open a crontab like so:
crontab -e
  • Then, enter the following command, which will instruct your machine to execute a cron job every hour and add new data to the index each time. After adding this at the end of the file, save it and close.
@hourly /usr/bin/indexer --rotate --config /etc/sphinx/sphinx.conf --all
  • Finally, you can start Sphinx. Use the systemctl command to start the Sphinx daemon.
sudo systemctl start searchd

Once that’s done, you’re all set. You’ve successfully installed and configured Sphinx on your server running CentOS!

 

Leave a Reply