Apr 3, 2014. SphinxQL and the MySQL .NET connector

Want to use C#/.NET with Sphinx? This blog post will tell you how to do it. Check it out.

Sphinx with C#/.NET

Here are your options:

  • use SphinxSE. This option makes sense only if you already use MySQL server and you want to use Sphinx via the MySQL server… It has it’s advantages and disadvantages. Depending on which MySQL version/fork you use, you might need to compile the SphinxSE plugin into the MySQL server. If you use Mariadb, this will be an easy task: SphinxSE is built in!
  • use an API. There is no official API port for C#/.NET. There are several 3rd party APIs, some free, some not free. You can find a list in our wiki.
  • use SphinxQL. Here, there are 2 choices which we will discuss in this post.

SphinxQL with the .NET MySQL connector

If you don’t use the MySQL server, don’t worry. You don’t need to install or run it. You only need the connector.

Supported versions of the .NET connector are 6.7 or greater.  You will need Sphinx 2.1.4 or greater (including 2.2.x or trunk). At some point the .NET connector introduced a query that runs right after the connection was made which ran time_diff() functions. So, we had to introduce these MySQL functions in Sphinx to make it work.

The .NET connector follows MySQL protocol very closely, so you need to do these two things to use it with Sphinx:

  • mysql_version_string must be set. Otherwise the connector will panic if it receives the version provided by default. 5.0.37 works.
  • use only UTF-8 charset. So be sure the indexes have charset_type = utf8 and in the connection string you have charset=utf8

In your project you will need to import the MySql.Data reference.

To get the query meta data, just run a SHOW META after you execute the SELECT statement.

A simple code sample for C#/.NET (and some other languages) can be found here.

SphinxQL via ODBC driver

Another way is to use ODBC. You will need to install the MySQL ODBC Unicode driver in your system. Be careful what type of application you are going to run. If you have a 32bit application, install the 32bit ODBC driver. If you have a 64bit application, install the 64bit ODBC driver.

For the driver you will need to specify the IP and Port. Although UTF-8 should be detected by default, you can also set Character Set to utf8.  After that, test the connection.

ODBCdriver

The code for this is similar to what was shown above. Check it out here.

In our test we used a simple RT index. The output should look like this:

outputcmd

So, that’s about it. If you have questions feel free to ask them. And, if you need help… we’re here to support you. Contact us.

Happy Sphinxing!


« »

Leave a Reply