Querying: Suggestions

RavenDB has an indexing mechanism built upon the Lucene engine which has a great suggestions feature. This capability allows a significant improvement of search functionalities enhancing the overall user experience of the application.

Let's consider an example where the users have the option to look for products by their name. The index and query would appear as follows:

public class Products_ByName : AbstractIndexCreationTask<Product>
{
    public Products_ByName()
    {
        Map = products => from product in products
                          select new
                          {
                              product.Name
                          };

        Indexes.Add(x => x.Name, FieldIndexing.Search);     // (optional) splitting name into multiple tokens
        Suggestion(x => x.Name);                            // configuring suggestions
    }
}

var product = session
    .Query<Product, Products_ByName>()
    .Search(x => x.Name, "chaig")
    .FirstOrDefault();

If our database has Northwind samples deployed then it will not return any results. However, we can ask RavenDB for help:

if (product == null)
{
    Dictionary<string, SuggestionResult> suggestionResult = session
        .Query<Product, Products_ByName>()
        .SuggestUsing(builder => builder.ByField(x => x.Name, "chaig"))
        .Execute();

    Console.WriteLine("Did you mean?");

    foreach (string suggestion in suggestionResult["Name"].Suggestions)
    {
        Console.WriteLine("\t{0}", suggestion);
    }
}
if (product == null)
{
    Dictionary<string, SuggestionResult> suggestionResult = session.Advanced
        .DocumentQuery<Product, Products_ByName>()
        .SuggestUsing(builder => builder.ByField(x => x.Name, "chaig"))
        .Execute();

    Console.WriteLine("Did you mean?");

    foreach (string suggestion in suggestionResult["Name"].Suggestions)
    {
        Console.WriteLine("\t{0}", suggestion);
    }
}
from index 'Products/ByName' 
select suggest('Name', 'chaig')

It will produce these suggestions:

Did you mean?
    chang
    chai

Client API

The SuggestUsing method is an extension contained in the Raven.Client.Documents namespace. You can read more about it in our Client API article.

Suggest Over Multiple Words

RavenDB allows you to perform a suggestion query over multiple words.

var resultsByMultipleWords = session
    .Query<Product, Products_ByName>()
    .SuggestUsing(builder => builder
        .ByField(x => x.Name, new[] { "chaig", "tof" })
        .WithOptions(new SuggestionOptions
        {
            Accuracy = 0.4f,
            PageSize = 5,
            Distance = StringDistanceTypes.JaroWinkler,
            SortMode = SuggestionSortMode.Popularity
        }))
    .Execute();

Console.WriteLine("Did you mean?");

foreach (string suggestion in resultsByMultipleWords["Name"].Suggestions)
{
    Console.WriteLine("\t{0}", suggestion);
}
var resultsByMultipleWords = session.Advanced
    .DocumentQuery<Product, Products_ByName>()
    .SuggestUsing(builder => builder
        .ByField(x => x.Name, new[] { "chaig", "tof" })
        .WithOptions(new SuggestionOptions
        {
            Accuracy = 0.4f,
            PageSize = 5,
            Distance = StringDistanceTypes.JaroWinkler,
            SortMode = SuggestionSortMode.Popularity
        }))
    .Execute();

Console.WriteLine("Did you mean?");

foreach (string suggestion in resultsByMultipleWords["Name"].Suggestions)
{
    Console.WriteLine("\t{0}", suggestion);
}

This will produce the following results:

Did you mean?
    chai
    chang
    chartreuse
    chef
    tofu

Remarks

Increased indexing time

Indexes with turned on suggestions tend to use a lot more CPU power than other indexes. This can impact indexing speed (querying is not impacted).