Ayende @ Rahien

Refunds available at head office

Raven Suggest

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?
        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.

Comments

Jason Meckley
11/20/2010 04:47 PM by
Jason Meckley

It's awesome to see Raven coming into it's own. The feature set incorporated into Raven is really making this product stand out among the competition.

Julio Camps
11/22/2010 05:20 AM by
Julio Camps

Hi, where is located Suggest() method?

ex: q.Suggest();

I can not find it.

Please excuse my poor english, i am cuban engineer.

John Harman
11/22/2010 07:42 AM by
John Harman

Great feature, I presume it's running off something similar to Snowball.NET?

Ayende Rahien
11/22/2010 08:36 AM by
Ayende Rahien

Julio,

You are probably looking at the stable branch, you should look at the unstable, that is where the new features are.

Ayende Rahien
11/22/2010 08:36 AM by
Ayende Rahien

John,

No, it is based on Spell.NET

jalchr
11/29/2010 11:11 AM by
jalchr

That's a nice feature. What I need in addition to this, is I need to query for all possible matches as the "suggest" is doing ... but in a query

Something like this :

var q = from user in session.Query <user("Users/ByName")

     where user.Name.SuggestedFrom(name)

     select user;

Is this doable ... ?

Ayende Rahien
11/29/2010 12:03 PM by
Ayende Rahien

Jalchr,

You can certainly write a query that would do that, but why would you want to do this?

jalchr
11/29/2010 12:37 PM by
jalchr

You know users sometimes misspell names ... I need to check for name uniques even if it is entered "wrong" and use one copy of a name ...

Can you suggest a way to query this ... or can we expect raven to have such feature built in ?

Ayende Rahien
11/29/2010 12:42 PM by
Ayende Rahien

Jalchr,

You can query for each of the possible names, sure. Or combine them into a single query.

Not very useful, though.

Julio Camps
12/01/2010 05:22 AM by
Julio Camps

HI again :)

IN build 206, say that

Full support for suggestions, on both lucene & linq queries

But i can not find it :(, i must use a specific namespace ??

Where is located this feature?? You may post using clause for this sample? Thank's in advanced

Ayende Rahien
12/01/2010 11:26 AM by
Ayende Rahien

Julio,

Is is there, as an extension method, you need to import the namespace Raven.Client.Linq

Comments have been closed on this topic.