| Getting Started |
This walkthrough will guide you through setting up an index for Sphinx and accessing it from a .NET application using the native API. The index will be created for the Sakila sample database provided by MySQL.
Create a file named sphinx.conf in the directory where the Sphinx executable files reside (usually the bin directory) and copy the following text into the file:
source sakila
{
type = mysql
sql_host = localhost
sql_user = #add MySQL user name here
sql_pass = #add password here
sql_db = sakila
sql_port = 3306
sql_query = SELECT film_id, title, title AS title_ord, description, release_year, rental_rate, language_id, \
length, UNIX_TIMESTAMP(last_update) AS last_update, 0 AS recommended_by_friends \
FROM film f;
sql_attr_str2ordinal = title_ord
sql_attr_uint = release_year
sql_attr_float = rental_rate
sql_attr_uint = language_id
sql_attr_uint = length
sql_attr_timestamp = last_update
sql_attr_multi = uint actor_id from query; SELECT film_id, actor_id FROM film_actor;
sql_attr_multi = uint rating from query; SELECT film_id, CRC32(rating) FROM film;
sql_attr_bool = recommended_by_friends
}
index sakila
{
source = sakila
path = sakila
charset_type = utf-8
min_word_len = 1
min_prefix_len = 0
min_infix_len = 1
}
searchd
{
listen = localhost:9312
listen = localhost:9306:mysql41
query_log_format = sphinxql
compat_sphinxql_magics = 0
log = searchd.log
query_log = query.log
pid_file = searchd.pid
max_matches = 1000
workers = threads
binlog_path =
}
indexer
{
mem_limit = 256M
}In the source section of the config file you need to modify the following settings for your installation of MySQL:
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. |
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 or Windows PowerShell.
You should now see output similar to this:
Sphinx 2.0.4-id64-release (r3135) Copyright (c) 2001-2012, Andrew Aksyonoff Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com) 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.
Start Visual Studio and create a new Console Application named SphinxConnectorDemo.
Add a reference to SphinxConnector.dll to your project. Either add SphinxConnector.NET via NuGet to your project, or manually add a reference to SphinxConnector.dll and Common.Logging to your project. You'll find these files in the Required Libs folder of the SphinxConnector.NET ZIP package.
Replace the code in your Program.cs file with the following:
using System; using SphinxConnector; namespace SphinxConnectorDemo { class Program { static void Main(string[] args) { SphinxClient sphinxClient = new SphinxClient(); SphinxSearchResult sphinxSearchResult = sphinxClient.Query("room", "sakila"); foreach (SphinxMatch match in sphinxSearchResult.Matches) { Console.WriteLine("DocumentId {0} Weight {1}", match.DocumentId, match.Weight); } Console.ReadLine(); } } }
| If necessary, adjust the parameters to the constructor of the SphinxClient class. |
DocumentId 52 Weight 1
DocumentId 87 Weight 1
DocumentId 743 Weight 1
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.