Sphinx Search Using PHP on Windows

There is this new thing I picked up. It is called SPHINX. Sphinx is an open source full text search server, designed from the ground up with performance, relevance (aka search quality), and integration simplicity in mind. You can check the features from their site. I am not trying to promote sphinx but I can say boldly that it is very much easy to install.

Some other highlights are:

    Batch and Real-Time full-text indexes.
    Non-SQL storage indexing.
    Advanced full-text searching syntax.
    Rich database-like querying features.
    Relevance ranking.

Another interesting thing about sphinx is that it has been ported to some languages and this is where SphinxAPI comes in. SphinxAPI is a native library available for Java, PHP, Python, Perl, C, and other languages.

When you have decided that you want try sphinx out, download the latest release for your system specifications here and let us proceed with installation.

INSTALLATION
Extract the .zip file to a preferred folder. If you look through the contents of the folder, you will find an example.sql file and some other config files. These are to make testing, tour and usage easier.
To make our folder less dirty, we will create two new folders ourselves. Create data and log folders. Index files will be saved in data while stuff will be logged into the log folder.

Then open the sphinx.conf.in the find and replace @CONFDIR@ to a preferred path (e.g C:/Sphinx) also check the database parameters. Edit the user to your user e.g root and supply the password.

From your command prompt, cd to the sphinx folder and run
bin\searchd --install --config path\to\sphinx\sphinx.conf.in --servicename SphinxSearch.
This command installs and starts the SphinxSearch service.
If you got this error FATAL: OpenSCManager() failed: code=5, error=Access is denied, then make sure your command prompt was run as an administrator.
Now that we have the success message, let’s move on.

DATABASE AND TABLES
Create a test table in your database. This is the table that will be used in the example.sql included in the folder downloaded. You can open and view the queries in it.
Import the example.sql into your test database.

INDEXING
Now we will create our index files. How? Just by running another command on your cmd.
bin\indexer --config path\to\sphinx\sphinx.conf.in --all
SEARCHING
Create a php script in your C:/xampp/htdocs/folderName.
Copy and paste the snippet below

<?php
mysql_connect("localhost", "root", "yourpassword");
mysql_select_db("test");
require_once('sphinxapi.php');
$s = new SphinxClient;
$s->setServer("127.0.0.1", 9312); // NOT "localhost" under Windows 7!
$s->setMatchMode(SPH_MATCH_ANY);
$s->SetLimits(0, 25);
$q=$_GET["q"];
$result = $s->Query($q);
if ($result['total'] > 0) {
echo 'Total: ' . $result['total'] . "<br>\n";
echo 'Total Found: ' . $result['total_found'] . "<br>\n";
echo '<table>';
echo '<tr><td>No.</td><td>ID</td><td>Group ID</td><td>Group ID 2</td><td>Date Added</td><td>Title</td><td>Content</td></tr>';
foreach ($result['matches'] as $id => $otherStuff) {
$row = mysql_fetch_array(mysql_query("select * from documents where id = $id"));
extract($row);
++ $no;
echo "<tr><td>$no</td><td>$id</td><td>$group_id</td><td>$group_id2</td><td>$date_added</td><td>$title</td><td>$content</td></tr>";
}
echo '</table>';
} else {
    print $s->GetLastError();
echo 'No results found';
}
?>

Run the script from the browser like this localhost/folderName/sphinxtest.php?q=test

That is it about installing and using sphinx.

You can tell that there are still many more things to do, right? There are still other tweaks that we need to include in our configuration file and script. But as I mentioned, I picked sphinx up about two days ago. Therefore, there is still so much for me to learn and share about sphinx. Till then, drop your comments or tweet at me.