Click or drag to resize

Working with async/await

Like SphinxQL, the fluent API has become fully asynchronous since V4 of SphinxConnector.NET.

Asynchronously executing queries with the fluent API
C#
using (IFulltextSession fulltextSession = fulltextStore.StartSession())
{
    var results = await fulltextSession.Query<Book>().
                                        Match("crime").
                                        ToListAsync();
}

Retrieving query metadata works the same with asynchronous queries as it does with synchronous queries:

C#
using (IFulltextSession fulltextSession = fulltextStore.StartSession())
{
    var results = await fulltextSession.Query<Book>().
                                        Metadata(out QueryMetadata metadata).
                                        Match("crime").
                                        ToListAsync();
}

With the fluent API you don't have to await each method call before invoking another one, you may execute several async methods without awaiting them to achieve some parallelism:

C#
using (IFulltextSession fulltextSession = fulltextStore.StartSession())
{
    var mainQuery = fulltextSession.Query<Book>().
                                    Match(incomingSearchTerm).
                                    ToListAsync();

    //Get suggestions in case the search yields no results
    //By not awaiting the main query both requests execute in parallel so that the suggestions will
    //most likely already be available when needed
    var suggestionQuery = fulltextSession.GetSuggestionsAsync<Book>(incomingSearchTerm");

    await Task.WhenAll(mainQuery, suggestionQuery)

    //Do further processing

}
Asynchronously inserting/updating/deleting data with the fluent API
C#
using (IFulltextSession session = fulltextStore.StartSession())
{
    session.Save(new Book
                     {
                         Id = 1,
                         Author = "George R.R. Martin",
                         Title = "A Game of Thrones: A Song of Ice and Fire: Book One",
                         EbookAvailable = true,
                         Categories = new [] { 1, 2 },
                         Price = 5.60m,
                         ReleaseDate = new DateTime(1997, 8, 4)
                     });

    session.Delete<Book>(10);

    await session.FlushChangesAsync();
}