Introducing the Fluent Query API Part 1 of n: A Quick Overview

by Dennis 4. May 2012 08:48

Disclaimer: The API presented here is still under development, so there might be changes until the final release. If you have any suggestions or comments post them here, over at Uservoice or drop me a mail!

With the upcoming release of SphinxConnector.NET 3.0 there will be a new addition to the API’s provided by SphinxConnector.NET: the fluent query API. This new API let’s you (surprise!) fluently compose your full-text queries based on an object model of the data contained in the index. With this approach building queries is much simpler and much more pleasant than writing SphinxQL by hand. But see for yourself:

using (IFulltextSession fulltextSession = fulltextStore.StartSession())
    IList<Product> products = fulltextSession.Query<Product>().
                                              Match("my product query").                   
                                              Where(x => x.VendorId == 2 && x.CategoryId ==5).
                                              OrderBy(x => x.Price).
Take(100). Results(); }


List<Product> products = new List<Product>();

using (SphinxQLConnection connection = new SphinxQLConnection())
    SphinxQLCommand command = connection.CreateCommand(@"SELECT * FROM products     
                                                         WHERE MATCH(@query) 
                                                         AND VendorId = @vendorId 
                                                         AND CategoryId = @categoryId
                                                         ORDER BY Price ASC LIMIT 0, 100");

    command.Parameters.Add("query", "my product query");
    command.Parameters.Add("vendorId", "2");
    command.Parameters.Add("categoryId", "5");


    using (SphinxQLDataReader dataReader = command.ExecuteReader())
        while (dataReader.Read())
            Product product = new Product
                                      Id = dataReader.GetInt32("Id"),
                                      CategoryId = dataReader.GetInt32("CategoryId"),
                                      VendorId = dataReader.GetInt32("VendorId"),
                                      Name = dataReader.GetString("Name")


And that’s not even a really complex query. How about this:

using (IFulltextSession fulltextSession = fulltextStore.StartSession())
    var results = fulltextSession.Query<Product>().
                                  Match("my product query").
                                  GroupBy(p => p.CategoryId).
                                  WithinGroupOrderByDescending(p => p.Weight).
                                  WithinGroupOrderBy(p => p.Price).
                                  Select(p => new
                                      ProductsInCategory = Projection.Count()
                                  OrderByDescending(x => x.ProductsInCategory).

I’ll spare you the SphinxQL equivalent Winking smile. I’ll be posting in more detail about the classes and methods involved in these examples in the course of this series. For now, you can see that we have a class called Products that represents the data we want to query (which can be in one or more index), an interface called IFulltextSession which is provided by a class named FulltextStore. The Query method of the IFulltextSession interface returns an instance of IFulltextQuery<T> which in turn provides the methods to perform full-text queries. 

This was just a quick introduction and basic overview, to let you see what the new query API has to offer. In the next parts, we'll take a more detailed look at each component, so stay tuned!

Tags: , , ,