This walkthrough will guide you through setting up an index for Sphinx and accessing it from a .NET application using SphinxConnector.NET. The index will be created for the Sakila sample database provided by MySQL.

Prerequisites

A working installation of a MySQL Server and Visual Studio 2005 or 2008.

Walkthrough

Download and install the Sakila sample database

  1. Download the Sakila database from here.

  2. Install the database as described here.

Download and install the Sphinx search engine

  1. Download either Sphinx 0.9.8 or 0.9.9 from here. The choice of operating system is irrelevant for SphinxConnector.NET.

  2. Install Sphinx as described here.

Create an index for the Sakila database

  1. Go to the Samples directory of your SphinxConnector.NET installation and, depending on the Sphinx version you installed, copy either the file named sphinx_098.conf or sphinx_099.conf to the appropriate directory of your Sphinx installation. On Windows this usually is the bin directory, on Linux this usually is the etc directory. Alternatively you can just copy the index and source sections from a sample config file into an existing Sphinx config file.

  2. In the source section of the config file you need to modify the following settings for your installation of MySQL:

    • sql_user
    • sql_pass
    • sql_host
    • sql_port

    Note:

    You may also need to modify the address or listen setting in the searchd section, depending on whether you installed Sphinx locally or on a remote machine.

  3. Now open a command line/terminal window and switch to the directory where the indexer executable resides. To create the index type indexer.exe sakila if you are using Windows or ./indexer sakila if you are using Linux.

    You should now see output similar to this:

    Copy 
    Sphinx 0.9.9-rc2 (r1785)
    Copyright (c) 2001-2009, Andrew Aksyonoff
    using config file './sphinx.conf'...
    indexing index 'sakila'...
    collected 1000 docs, 0.1 MB
    collected 6462 attr values
    sorted 0.0 Mvalues, 100.0% done
    sorted 0.5 Mhits, 98.2% done
    total 1000 docs, 108077 bytes
    total 0.313 sec, 344736 bytes/sec, 3189.73 docs/sec
    total 10 reads, 0.002 sec, 285.2 kb/call avg, 0.2 msec/call avg
    total 17 writes, 0.044 sec, 397.8 kb/call avg, 2.6 msec/call avg

    The index has been created and can now be accessed from SphinxConnector.NET.

  4. Now start the Sphinx search deamon by typing searchd (Windows) or ./searchd (add --console to have the log written to the console) (Linux).

Create a Visual Studio project

  1. Start Visual Studio and create a new Console Application named SphinxConnectorDemo.

  2. Add a reference to Gronewold.SphinxConnector.dll to your project. To do so in Visual Studio, navigate to the Solution Explorer and right click on the References entry in your project. In the conext menu, select Add References, choose Gronewold.SphinxConnector.dll from the .NET tab, and hit OK.

  3. Replace the code in your Program.cs file with the following:

    CopyC#
    using System;
    
    using Gronewold.SphinxConnector;
    
    namespace SphinxConnectorDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                SphinxClient sphinxClient = new SphinxClient();
                //Uncomment the following line, if you installed Sphinx 0.9.8
                //sphinxClient.Version = new Version(0,9,8);  
                SphinxSearchResult sphinxSearchResult = sphinxClient.Query("room", "sakila");
    
                foreach (SphinxMatch match in sphinxSearchResult.Matches)
                {
                    Console.WriteLine("DocumentId {0} Weight {1}", match.DocumentId, match.Weight);
                }
    
                Console.ReadLine();
            }
        }
    }
    Note:
    If neccessary, adjust the parameters to the constructor of the SphinxClient class.
  4. Now hit F5 and you should see output similar to this:
    DocumentId 52 Weight 1
    DocumentId 87 Weight 1
    DocumentId 743 Weight 1

Next Steps

Check out the documentation on methods of the SphinxSearchOptions class to see how you can set filters on attributes, set weights on fields and much more.