
|  | Common Usage Examples | 
This documents contains some fluent API examples for common usage scenarios
 Using Sphinx Functions
Using Sphinx FunctionsSphinx 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.
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:
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.
 Aggregates
AggregatesIn 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:
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
Other FunctionsThe functions IN and INTERVAL are supported through extension methods provided by SphinxConnector.NET. To use these methode you have to import the namespace SphinxConnector.FluentApi.Util.
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
Changing Query OptionsTo change the options for a query, use the Options method of the IFulltextQuery class:
var results = fulltextSession.Query<Book>(). Options(o => o.Ranker(SphinxRankMode.WordCount). MaxMatches(50)). ToList();
 Using Multi-Value Attributes
Using Multi-Value AttributesUsing 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:
public class Book { public int Id { get; set; } public IList<int> Genres { get; set; } }
 See Also
See Also