Guide to using Sphinx with PHP and MySQL
I came across this post but didn't find an answer I wanted to see. So here is my Quick Start Guide:
1. Install Sphinx
On Mac with Homebrew:
brew install sphinx
On Amazon Linux (CentOS) with yum:
yum install sphinx
2. Create Sphinx config
Sphinx comes with config template. Look for sphinx.conf.dist in the configs directory:
On Mac installed with Homebrew:
/usr/local/Cellar/sphinx/<sphinx version>/etc
On Amazon Linux installed with yum:
/etc/sphinx
It is pretty straightforward but might contain too many settings for a newbie. In such case you can use this simple config:
source TestSource { type = mysql sql_host = <host> sql_user = <user> sql_pass = <password> sql_db = <db> sql_query_range = select min(id), max(id) from TestTable sql_range_step = 2048 sql_query = select id, some_info from TestTable\ where id >= $start and id <= $end}index TestIndex { source = TestSource path = /var/lib/sphinx/test-index min_word_len = 3 min_infix_len = 3}searchd { log = /var/log/sphinx/searchd.log query_log = /var/log/sphinx/query.log pid_file = /var/run/searchd.pid max_matches = 200 listen = localhost:9312}
I added max_matches setting to this config because my first question after I got everything working was "Why do I always get only 20 search results?". With max_matches you can set the limit for search results number.
3. Create index using indexer
indexer --all
4. Run Sphinx daemon
sudo searchd -c /path/to/config/sphinx.conf
5. Install PHP Sphinx extension
On Mac with Homebrew:
brew install homebrew/php/php56-sphinx
On Amazon Linux with yum:
yum install libsphinxclientpecl install sphinx
6. Query your index from PHP
$index = new SphinxClient();$index->setServer("127.0.0.1", 9312);$result = $index->query('some search term', 'TestIndex');print_r($result);
In case of any errors you can get more information with the following method:
$index->getLastError();
7. Keep up to date index
To maintain an up to date index you can use two indices:
- Main index, which is not updated often (once per week, month, etc)
- And delta index, which updates often (every hour, 5 min, etc)
Every time delta index is re-indexed it is merged with the main index
Follow this link http://www.sphinxconsultant.com/sphinx-search-delta-indexing/ to read more about this approach.
Links I found useful:
- http://sphinxsearch.com/docs/current.html
- http://sphinxsearch.com/info/faq/
- http://atlchris.com/1996/working-with-sphinx-search-engine-on-a-lamp-linux-apache-mysql-and-php-stack-server/
- http://www.sphinxconsultant.com/sphinx-search-delta-indexing/
- https://github.com/schmittjoh/php-stubs/tree/master/res/php/sphinx
I'm not too sure about a good guide but here are my steps.
a) Download and install it's quite straightforward
b) Make your first index - you need a source a location the given config is very good remember you can use a primary source to config all the main areas and then other sources stem off from that. Each source should start with the primary key and I find it works best to do key_id AS id
c) Test you index using the search
d) Start your search demon for sphinx - searchd this is what php will connect to and how it gets your results.
e) Make a function to search all indexes pass in the index you want to search and it will return the ids in an array that have matched your search
f) Make a delta and updates.
Job done - the sphinx forum is very nice and should provide you if you need any help.Richard