Sphinx search engine and PHP (Part 2 – Searching from PHP)

In earlier post we saw how to install and configure sphinx. We also created an index for sphinx to search from. Now let us see how to search the index from a php script.

Sphinx comes with a PHP implementation of searchd client API. You can find it at api/sphinxapi.php in the root of the sphinx source you downloaded earlier.

Now continuing our earlier members example - we will create a php script to search for members using the sphinxapi.php client class. But first we need to configure searchd which is a daemon that receives search term and other parameters from an application (in our case it is the php script). The daemon then returns the search results to the calling application. All this is done through the API client i.e. sphinxapi class.

Add the following code to the /usr/local/sphinx/etc/sphinx.conf configuration file

CODE:
  1. # Searchd settings
  2. searchd
  3. {
  4.     # Port to listen on
  5.     port = 3312
  6.    
  7.     # Next few are the paths to log files
  8.     log           = /usr/local/sphinx/var/log/searchd.log
  9.     query_log = /usr/local/sphinx/var/log/query.log
  10.    
  11.     # Maximum amount of concurrent searches to run - 0 for unlimited
  12.     max_children = 30
  13.    
  14.     # Path to pid file
  15.     pid_file = /usr/local/sphinx/var/log/searchd.pid
  16. }

Start the daemon by executing /usr/local/sphinx/bin/searchd. As configured, the daemon will listen on port 3312 so make sure your firewall is not blocking that port.

Now head on to your php script which needs to perform the search. The code should look something like this...

PHP:
  1. <?php
  2. // Include the sphinx API class
  3. require('sphinxapi.php');
  4.  
  5. // Connect to sphinx server
  6. $sp = new SphinxClient();
  7.  
  8. // Set the server
  9. $sp->SetServer('localhost', 3312);
  10.  
  11. // SPH_MATCH_ALL will match all words in the search term
  12. $sp->SetMatchMode(SPH_MATCH_ALL);
  13.  
  14. // We want an array with complete per match information including the document ids
  15. $sp->SetArrayResult(true);
  16.  
  17. /**
  18. * Run the search query. Here the first argument is the search term
  19. * and the second is the name of the index to search in.
  20. * Search term can come from a search form
  21. */
  22. $results = $sp->Query('search term', 'members');
  23. ?>

$results['matches'] will be an array with all the matched documents. Its up to you what to do with the results. You will only get "id" of the documents in the results and if you want to get more information then you can get it from database table using the "id".

Above example was a tip of an ice berg. Sphinx is very powerful with many more features such as filtering results based on attributes, sorting, weighting, distributed searching etc. For a complete list of features and to create a complex search please refer the documentation.

About Abbas Ali

Abbas Ali is a Mechanical Engineer by education. He turned to programming and took it as a profession just after finishing his studies. He is fascinated equally by both machines and computers. He leads the team of dynamic programmers at SANIsoft and works as a Technology Manager. He is also an active developer on the Coppermine Picture Gallery team.

8 Responses to Sphinx search engine and PHP (Part 2 – Searching from PHP)

  1. Chris Rymer September 21, 2010 at 11:03 pm #

    OK, I got my head around it!

    You can set up a config file for any given projects database or table along with the directories you wish to store the indexes in.

    On my test DB I have 100,000 rows I want to try and index but obviously this produces a behemoth XML file which fails in a foreach loop.

    I realise this is beyond the requirements of the tutorial but what tool would you recommend to ease the load on the foreach. I cannot think of anyway other than to set the time out limit which doesn't matter as it is make for no impact in implementation, for me anyway :) .

  2. Abbas Ali September 22, 2010 at 10:01 am #

    @Chris: I suggest in such cases use the SQL data source instead of xmlpipe2.

    You will need to use the sql_query_range and sql_range_step options so that large data set can be fetched in steps.

  3. Robert D November 29, 2010 at 1:19 am #

    Thanks for this tutorial!

  4. raghu May 2, 2011 at 5:02 pm #

    how to insert data into sphinx real time index ( rt ) using php script

  5. raghu May 2, 2011 at 5:03 pm #

    how to index a table in the data base using rt

Trackbacks/Pingbacks

  1. Sphinx search engine and PHP (Part 1 – Installation and Indexing) at SANIsoft – PHP for E Biz - January 29, 2010

    [...] Update: Part 2 – Searching from PHP [...]

  2. Volltextsuche mit SPHINX, MySQL und PHP « devgeek.de: news, tutorials und links by Mark Buch - August 25, 2011

    [...] weiterführende Links: http://devreview.net/0305/installation-sphinx-search-engine-on-freebsd-7-2/ http://www.sanisoft.com/blog/2010/01/29/sphinx-search-php/ http://www.ibm.com/developerworks/library/os-php-sphinxsearch/ [...]

Leave a Reply