Raven Suggest

time to read 3 min | 457 words

Originally posted at 11/18/2010

image Since RavenDB’s indexing is based on Lucene, we get a lot of advantages. One of them is supporting suggestions.

What is the Raven Suggest feature?  Let us say that we give the user the option to perform some query, but the query that they sent us isn’t returning enough results (or non at all).

We can ask RavenDB to figure out what the user meant. Here is the code:

 var q = from user in session.Query<User>("Users/ByName")
         where user.Name == name
         select user;

Now, we can call q.FirstOrDefault() on this query, and see if we got any results. If we didn’t get a result, we can now ask RavenDB for help:

 var suggestionQueryResult = q.Suggest();
 Console.WriteLine("Did you mean?");
 foreach (var suggestion in suggestionQueryResult.Suggestions)
     Console.WriteLine("\t{0}", suggestion);

This will produce the following output:

Enter user name: oren
Found user: users/1
Enter user name: ore
Did you mean?
Enter user name: onre
Did you mean?

This is a nice little feature, which can really make a difference in terms of your application’s user experience.