Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Raven Suggest

DZone's Guide to

Raven Suggest

· ·
Free Resource

RavenDB vs MongoDB: Which is Better? This White Paper compares the two leading NoSQL Document Databases on 9 features to find out which is the best solution for your next project.  

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?
oren
Enter user name: onre
Did you mean?
oren

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

Do you pay to use your database? What if your database paid you? Learn more with RavenDB.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}