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
-
# Searchd settings
-
searchd
-
{
-
# Port to listen on
-
port = 3312
-
-
# Next few are the paths to log files
-
log = /usr/local/sphinx/var/log/searchd.log
-
query_log = /usr/local/sphinx/var/log/query.log
-
-
# Maximum amount of concurrent searches to run - 0 for unlimited
-
max_children = 30
-
-
# Path to pid file
-
pid_file = /usr/local/sphinx/var/log/searchd.pid
-
}
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
-
// Include the sphinx API class
-
require('sphinxapi.php');
-
-
// Connect to sphinx server
-
$sp = new SphinxClient();
-
-
// Set the server
-
$sp->SetServer('localhost', 3312);
-
-
// SPH_MATCH_ALL will match all words in the search term
-
$sp->SetMatchMode(SPH_MATCH_ALL);
-
-
// We want an array with complete per match information including the document ids
-
$sp->SetArrayResult(true);
-
-
/**
-
* Run the search query. Here the first argument is the search term
-
* and the second is the name of the index to search in.
-
* Search term can come from a search form
-
*/
-
$results = $sp->Query('search term', 'members');
-
?>
$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.
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
.
@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.
Thanks for this tutorial!
how to insert data into sphinx real time index ( rt ) using php script
how to index a table in the data base using rt
@raghu: http://www.sanisoft.com/blog/2010/12/27/how-to-live-index-updates-in-sphinx-search/