Click or drag to resize

This introduction is intended to give a quick overview about the main classes and interfaces of the fluent API.

Caution note Caution

For the fluent API to work, you need to tell Sphinx to disable the legacy SphinxQL quirks compatiblity mode. This is done by setting compat_sphinxql_magics = 0 in the searchd section of your sphinx.conf file.

Classes and Interfaces involved

The entry point to the fluent API is the FulltextStore class. This class is used to configure connection strings, mapping conventions and other settings. Its other main purpose is to serve as a factory for creating instances of the IFulltextSession interface. All these classes and interfaces are located in the namespace SphinxConnector.FluentApi. Before you can use the FulltextStore class, you need to call its Initialize method:

IFulltextStore fulltextStore = new FulltextStore().Initialize();
Note Note

You'll normally create one instance of the FulltextStore class per application. If you're using an IoC container, you should configure it as a singleton.

With the IFulltextSession interface you are able to access Sphinx functionality like performing full-text queries and saving and deleting documents from Sphinx real-time indexes. To create a full-text query you use the Query method along with your document model as the type parameter e.g.

using (IFulltextSession fulltextSession = fulltextStore.StartSession())
    var results = fulltextSession.Query<Book>().
Note Note

The IFulltextSession is a lightweight interface that is not thread-safe and should therefore be created for every thread wishing to use it. For example in a web application each request should get its own instance.

Creating Document Models

Creating a document model for your index is straightforward: With the default conventions, the index name is the pluralized class name. Pluralization is done by appending an 's', additionally the class name is converted to lower case. So, if you have an index books you'll name your document model Book. The attributes of an index are represented by properties or fields. Their names are taken as is and converted to lower case with the default conventions. Make sure that your class has a parameterless non-private constructor.

Note Note

A property representing a multi-value attribute should be of type IEnumerable<T>, IList<T>, or T[] where T is a numeric type that is large enough to hold the values that are stored in the index.

During a full-text search, Sphinx assigns each document a weight. To access the weight of document in your query, or if want to include it in your results for display purposes, you just have to add a property named Weight to your document class.

Document Model Example
public class Book
    public int Id { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }
    public decimal Price { get; set; }
    public bool EbookAvailable { get; set; }
    public DateTime ReleaseDate { get; set; }
    public IEnumerable<int> Categories { get; set; }
    public int Weight { get; set; }        

As stated above, to execute a query you use the Query method of the IFulltextSession interface, which returns an instance of IFulltextQueryTDocument. This interface provides all the necessary methods for building a query and retrieving the results from the Sphinx server.

Saving and Deleting Documents

To save a document in a real-time index, the IFulltextSession interface provides the method Save. Save takes either a single document or an enumerable of documents as an argument.

Deleting documents from a real-time index is done via the Delete method of the IFulltextSession interface. You can either provide the id's of the documents to delete or an instance of a document that should be deleted.

Note Note

Note that all saves and delete are only executed when you call FlushChanges

See Also