Introduction
This introduction is intended to give a quick overview about the main classes and interfaces of the fluent API.
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();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 QueryTDocument method along with your document model as the type parameter e.g.
using (IFulltextSession fulltextSession = fulltextStore.StartSession())
{
var results = fulltextSession.Query<Book>().
ToList();
}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.
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.
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; }
}Querying
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.