Click or drag to resize

Common Usage Examples

This documents contains some fluent API examples for common usage scenarios

Using Sphinx Functions

Sphinx supports quite a few functions that can be used in a query. It ranges from numeric functions like FLOOR, CEIL over date functions like YEAR to comparison functions like IF. SphinxConnector.NET supports these functions by recognizing the corresponding .NET methods and translating them to their Sphinx equivalents. Most numeric functions can used via the Math class provided by .NET. e.g.

C#
var results = fulltextSession.Query<Book>().
                               Select(b => new
                               {
                                   Floor = Math.Floor(b.Price),
                                   Ceiling = Math.Ceiling(b.Price)
                               }).ToList();

The date functions can be used via the properties of the SystemDateTime class, currently supported are DateTimeDay, DateTimeYear, and DateTimeMonth.

IF can be used via the ternary operator, e.g:

C#
var results = fulltextSession.Query<Product>().
                              Select(p => new
                              {
                                  Price = p.CategoryId == 5 ? p.Price * 0.9m : p.Price
                              }).ToList();

For functions that have no corresponding .NET method, SphinxConnector.NET provides the Function class which contains methods for functions like Fibonacci and Geodist.

Manticore Search provides additional functions that Sphinx doesn't support like CONCAT() and REGEX() which can be used as follows:

C#
var results = fulltextSession.Query<Book>().
                              Select(b => new
                              {
                                  b,
                                  Display = String.Concat(b.Title, " ", b.Author, " (", b.Price, " €)")
                              }).ToList();

SphinxConnector.NET will automatically emit a call to TO_STRING() if you are using a non string attribute within Concat().

C#
var results = fulltextSession.Query<Book>().
                              Select(b => new
                              {
                                  b,
                                  IsMatch = Regex.IsMatch(b.Title, "(?i)^a")
                              }).
                              Where(x => x.IsMatch).
                              ToList();
Aggregates

In order to create aggregate values like the sum, the maximum of values etc., the fluent API provides a static class named Projection which contains methods for all supported aggregation operations. For example, for a book search we could get the number of genres that contain matching books and the minimum and maximum prices in each genres like this:

C#
var results = fulltextSession.Query<Book>().
                                Match("a search").
                                GroupBy(b => b.Genre).
                                Select(b => new
                                {
                                    b.Genre,
                                    ProductCount = Projection.Count(),
                                    MinimumPrice = Projection.Min(() => b.Price),
                                    MaximumPrice = Projection.Max(() => b.Price)
                                }).ToList();
Other Functions

The functions IN and INTERVAL are supported through extension methods provided by SphinxConnector.NET. To use these methods you have to import the namespace SphinxConnector.FluentApi.Util.

C#
var results = fulltextSession.Query<Product>().
                              Where(p => p.CategoryId.In(4, 8, 15, 16, 23, 42)).
                              Select(p => new
                              {
                                    Count = Projection.Count(),
                                    PriceInterval = p.Price.Interval(10, 50, 100, 1000)
                              }).
                              GroupBy(p => p.PriceInterval).
                              ToList();
Changing Query Options

To change the options for a query, use the Options method of the IFulltextQuery class:

C#
var results = fulltextSession.Query<Book>().
                              Options(o => o.Ranker(SphinxRankMode.WordCount).
                                             MaxMatches(50)).
                              ToList();
Using Multi-Value Attributes

Using multi-value attributes is as simple as adding a property of type IList<T> or IEnumerable<T> to your document model. T can of any numeric type that is large enough to hold the values for your scenario. In the following example we'll use int:

C#
public class Book
{
    public int Id { get; set; }
    public IList<int> Genres { get; set; }
}
See Also