已在Ubuntu部署Sphinx 2.2.11,如何通过C#连接该搜索引擎?
Hey there! Let's figure out how to connect your C# application to your Sphinx 2.2.11 instance since you already have it integrated with Oracle and working via the command line. Since there's no official .NET API, here are three reliable approaches you can use right now:
Sphinx natively supports the MySQL protocol (that’s exactly why your SQL client works from the command line!). This means you can use a standard, well-maintained MySQL ADO.NET driver like MySqlConnector to query Sphinx directly from C#—no weird third-party dependencies needed.
- First, install the
MySqlConnectorNuGet package in your project. It’s free and regularly updated. - Set up your connection string to point to your Sphinx instance. By default, Sphinx listens on port 9306 for MySQL protocol connections:
string sphinxConnectionString = "server=your-sphinx-server-ip;port=9306;database=;charset=utf8;"; - Example code to run a search query:
The driver won’t even notice it’s talking to Sphinx instead of a real MySQL server—this method is super reliable for Sphinx 2.2.11.using MySqlConnector; using System; using System.Threading.Tasks; class SphinxQueryExample { static async Task Main(string[] args) { using var connection = new MySqlConnection(sphinxConnectionString); await connection.OpenAsync(); var searchCommand = new MySqlCommand( "SELECT id, title, content FROM your_oracle_index WHERE MATCH('your search terms') LIMIT 10;", connection ); using var reader = await searchCommand.ExecuteReaderAsync(); while (await reader.ReadAsync()) { Console.WriteLine($"ID: {reader.GetInt32("id")}, Title: {reader.GetString("title")}"); // Process other fields as needed } } }
If you’d rather avoid using a MySQL driver, Sphinx also exposes an HTTP API (default port 9308). You can call this directly from C# using HttpClient, which is part of the .NET standard library.
- Example code to send a search request via HTTP:
This method is lightweight and doesn’t require any extra NuGet packages beyond what’s already in .NET. Just make sure to handle JSON deserialization correctly based on your index’s fields.using System; using System.Net.Http; using System.Threading.Tasks; using System.Text.Json; class SphinxHttpExample { static async Task Main(string[] args) { var httpClient = new HttpClient(); var baseUrl = "http://your-sphinx-server-ip:9308"; var searchTerms = "your search terms"; var indexName = "your_oracle_index"; // Build the request URL with encoded parameters var requestUrl = $"{baseUrl}/search?q={Uri.EscapeDataString(searchTerms)}&index={indexName}&limit=10"; var response = await httpClient.GetAsync(requestUrl); response.EnsureSuccessStatusCode(); // Parse the JSON response var responseJson = await response.Content.ReadAsStringAsync(); var searchResults = JsonSerializer.Deserialize<SphinxSearchResult>(responseJson); // Process results foreach (var result in searchResults.Matches) { Console.WriteLine($"ID: {result.Id}, Score: {result.Score}"); // Access stored fields via result.Attributes } } } // Helper class to deserialize Sphinx's JSON response public class SphinxSearchResult { public int Total { get; set; } public SphinxMatch[] Matches { get; set; } } public class SphinxMatch { public int Id { get; set; } public int Score { get; set; } public Dictionary<string, object> Attributes { get; set; } }
You mentioned SphinxSE—this is a MySQL storage engine that acts as a bridge between MySQL and Sphinx. It’s a good option if you already have a MySQL server in your stack and want to join Sphinx search results with other MySQL data.
- Here’s the high-level workflow:
- Install SphinxSE into your MySQL server (you’ll need to compile it from source or use pre-built binaries compatible with your MySQL version).
- In MySQL, create a virtual table that maps to your Sphinx index:
CREATE TABLE sphinx_your_index ( id INT NOT NULL, title VARCHAR(255) NOT NULL, content TEXT NOT NULL, KEY(id) ) ENGINE=SPHINX CONNECTION="sphinx://your-sphinx-server-ip:9312/your_oracle_index"; - Then, from your C# app, connect to MySQL using the same MySQL driver as before, and query the virtual table like any other MySQL table:
var mysqlCommand = new MySqlCommand( "SELECT * FROM sphinx_your_index WHERE MATCH('your search terms') LIMIT 10;", mysqlConnection ); - Pros: Lets you combine Sphinx results with data from other MySQL tables using JOINs.
- Cons: Adds extra complexity (needing a MySQL server, setting up SphinxSE). Only worth it if you already use MySQL.
- Double-check your
sphinx.confto ensure thelistendirective allows connections from your C# app’s IP address (e.g.,listen = 0.0.0.0:9306for all IPs). - For Sphinx 2.2.11, both the MySQL protocol and HTTP API are stable—stick to these instead of unmaintained third-party libraries.
- When using the HTTP API, always URL-encode your search terms to avoid invalid requests.
内容的提问来源于stack exchange,提问作者DAVID_ROA

