Reader small image

You're reading from  MariaDB Cookbook

Product typeBook
Published inMar 2014
Reading LevelBeginner
Publisher
ISBN-139781783284399
Edition1st Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Daniel Bartholomew
Daniel Bartholomew
author image
Daniel Bartholomew

Daniel Bartholomew has been using Linux since 1997 and databases since 1998. In addition to this book, he has also written MariaDB Cookbook, Packt Publishing, and dozens of articles for various magazines, including The Linux Journal, Linux Pro, Ubuntu User, and Tux. He became involved with the MariaDB project shortly after it began in early 2009 and continues to be involved to this day. He currently works for MariaDB, Inc. and splits his time between managing MariaDB releases, documentation, and maintaining various bits and pieces that keep the MariaDB project running smoothly.
Read more about Daniel Bartholomew

Right arrow

Chapter 9. Searching Data Using Sphinx

In this chapter, we will cover the following recipes:

  • Installing SphinxSE in MariaDB

  • Installing the Sphinx daemon on Linux

  • Installing the Sphinx daemon on Windows

  • Configuring the Sphinx daemon

  • Searching using the Sphinx daemon and SphinxSE

Introduction


With any growing or evolving database, there comes a time when the limitations of MariaDB's built in, full text-searching functionality becomes more of a hindrance than its convenience is worth. At that point, another method is needed to efficiently index and search through our textual data. This is where Sphinx comes in.

There are actually two parts to Sphinx: an external daemon called Sphinx that does the work of building and maintaining the search index using that we use to search our data, and a storage engine component called SphinxSE that is part of MariaDB, which the Sphinx daemon uses to talk to MariaDB. The recipes in this chapter will cover setting up and using both these.

Installing SphinxSE in MariaDB


Before we can start using Sphinx, we need to enable SphinxSE in MariaDB.

How to do it...

  1. Open the mysql command-line client and connect to our database server with a user that has the SUPER privilege.

  2. Run the following command to install SphinxSE:

    INSTALL SONAME 'ha_sphinx';
    
  3. Run the following command, shown as follows, to check that the Sphinx Storage Engine (SphinxSE) is enabled (the Support column will say YES):

    SHOW storage engines;
    
  4. Run the following command to view the SphinxSE status variables (they will be empty):

    SHOW STATUS LIKE 'sphinx_%';
    

How it works...

SphinxSE is included in MariaDB, but it is disabled by default. To enable it, we run the INSTALL SONAME command with the name of the plugin (ha_sphinx). This is a one-time operation.

Once SphinxSE is enabled in this manner and if we have the external Sphinx daemon installed and running, we can start using Sphinx to search our data. This is the topic of the following two recipes.

There's more...

The following...

Installing the Sphinx daemon on Linux


In order to use SphinxSE and Sphinx, we must install the daemon on our server. This recipe covers the process for Linux servers running Ubuntu, Debian, Red Hat, Fedora, or CentOS.

How to do it...

  1. On Red Hat, CentOS, Ubuntu, or Debian, go to the Sphinx download site at http://sphinxsearch.com/downloads/release/ and download the latest Sphinx package for our Linux distribution.

  2. On Debian or Ubuntu servers, run the following statement to install the Sphinx daemon:

    sudo apt-get install unixodbc libpq5 mariadb-client
    sudo dpkg -i sphinxsearch*.deb
    
  3. On Red Hat and CentOS, run the following statement to install the Sphinx daemon:

    sudo yum install postgresql-libs unixODBC
    sudo rpm -Uhv sphinx*.rpm
    
  4. On Fedora, run the following command to install Sphinx:

    sudo yum install sphinx
    
  5. On all server types, configure the Sphinx daemon as described in the Configuring the Sphinx daemon recipe in this chapter.

  6. On Ubuntu and Debian, edit the /etc/default/sphinxsearch file and set...

Installing the Sphinx daemon on Windows


In order to use Sphinx, we must install the daemon on our server. This recipe is all about installing the Windows version of the Sphinx daemon.

How to do it...

  1. Go to http://sphinxsearch.com/downloads/release/ and download the latest version of Sphinx for MySQL, either the 64-bit or 32-bit version depending on our version of Windows. For this recipe, we'll assume that we're running a 64-bit version of Windows.

  2. Navigate to the Downloads folder and extract the ZIP file.

  3. Extract the file to a location that is convenient. This recipe will assume that we extracted the files to C:\Sphinx (as the Sphinx docs recommend).

  4. If the unzipping process creates a Sphinx subfolder (such as sphinx-2.1.3-release-win64) under C:\Sphinx move the contents of that subdirectory to the C:\Sphinx folder and then remove the empty directory using the following statements:

    cd C:\Sphinx 
    mv .\sphinx-2.1.3-release-win64\* .\
    rmdir sphinx-2.1.3-release-win64
    
  5. Configure Sphinx as described...

Configuring the Sphinx daemon


In order to use Sphinx, we need to add a user to our MariaDB database and configure the Sphinx daemon so that it indexes the content we want it to.

Getting ready

Install the SphinxSE as described in the Installing SphinxSE in MariaDB recipe in this chapter. Install the Sphinx daemon as described in either the Installing the Sphinx daemon on Linux recipe or the Installing the Sphinx daemon on Windows recipe, both in this chapter, depending on which operating system we are using.

How to do it...

  1. Open the mysql command-line client and create a user with SELECT rights for the tables in the databases we want the Sphinx daemon to index, using the following statements:

    CREATE USER 'sphinx'@localhost 
      IDENTIFIED BY 'sphinxsecretpassword';
    GRANT SELECT on test.* to 'sphinx'@localhost;
    
  2. Move the default sphinx.conf file out of the way; for example, the following will work on Linux:

    sudo mv -vi sphinx.conf sphinx.conf.dist
    
  3. Create a new sphinx.conf file in a text editor ...

Searching with the Sphinx daemon and SphinxSE


After completing the other recipes in this chapter, this is the recipe where we actually get to see Sphinx doing something.

Getting ready

This recipe requires that we install and configure SphinxSE and the Sphinx daemon. See the previous recipes in this chapter for instructions.

In the previous recipe, we configured the Sphinx daemon to index and search a table called documents in the test database. For the purposes of this recipe and to match the previous recipe, we need to create this table with the following CREATE TABLE command:

CREATE TABLE documents (
    id SERIAL PRIMARY KEY,
    date_added TIMESTAMP,
    title VARCHAR(256),
    content TEXT
);

We also need to add some example data to the table. The 4399OS_09_documents.sql file, available from this book's website, will create the table and populate it with some example data. We can load the file using the following statement:

mysql -u user -p test < 4399OS_09_documents.sql

We'll need...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
MariaDB Cookbook
Published in: Mar 2014Publisher: ISBN-13: 9781783284399
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Daniel Bartholomew

Daniel Bartholomew has been using Linux since 1997 and databases since 1998. In addition to this book, he has also written MariaDB Cookbook, Packt Publishing, and dozens of articles for various magazines, including The Linux Journal, Linux Pro, Ubuntu User, and Tux. He became involved with the MariaDB project shortly after it began in early 2009 and continues to be involved to this day. He currently works for MariaDB, Inc. and splits his time between managing MariaDB releases, documentation, and maintaining various bits and pieces that keep the MariaDB project running smoothly.
Read more about Daniel Bartholomew