Create A Super Fast Search with Sphinx
Sphinx (acronym for SQL Phrase Index) is a full-text search engine, it runs as a daemon and serves to requests of client applications. Client applications need to access Sphinx daemon via the native SphinxAPI, for which libraries are available in almost all popular languages like PHP, Perl, Ruby, Java, C# etc. The client can also access the search daemon via Sphinx's own MySQL network protocol called SphinxQL or via a MySQL storage engine called SphinxSE.
Sphinx can load data from various sources like MySQL, PgSQL, ODBC, XML file, etc. and then it creates indexes, you can also update indexes from time to time from the data source. In this article we'll see how to install and setup Sphinx and then use it from a PHP script.
Get the latest release of Sphinx from from http://sphinxsearch.com/downloads/release/ and extract everything from the tarball, and run configure:
All should go well, configure will automatically figure out the location of MySQL libraries, in case you face any trouble visit http://sphinxsearch.com/ and get help from the docs.
I am going to use MySQL as my datasource, and in the example I'll be have only one table which is a table containing articles with the following fields:
Now, let's go through the config file I am using, the comments will be self explanatory.
Our config it ready, now let' create the indexes.
If you'd like your index to be updated, setup a cron to run at an interval of your choice like this:
Now, we'll start the Sphinx search daemon:
Search daemon running, let's try some code.
You'll need to the PECL extension for Sphinx installed for PHP, if you don't have it installed check here how to: http://www.php.net/manual/en/sphinx.installation.php
The code below it a simple example:
Happy building search engines :-)
|All times are GMT +5.5. The time now is 00:17.|