Raven Suggest: Review & Options

time to read 10 min | 1834 words

Phil Jones has posted some demo code for doing suggestions in RavenDB.

        [HttpPost]
        public JsonResult CompanySuggestions(string term)
        {
            var rq = RavenSession.Query<Company, Companies_QueryIndex>();

            var ravenResults = rq.Search(x => x.Name, string.Format("*{0}*", term), 
escapeQueryOptions: EscapeQueryOptions.AllowAllWildcards,
options: SearchOptions.And) .Take(5) .ToList(); return Json(ravenResults.Select(x => new { id = x.Id, value = x.Name, description = string.Format("({0}, {1})", x.Category, x.Location) })); }

 

And the data looks like this:

image

The Companies_QueryIndex is a simple one, with the Name field marked as analyzed.

This code works, but it isn’t ideal. In fact, it contains an major problem. It uses string.Format("*{0}*", term), which is identical to doing a Foo LIKE ‘%’ + @term +’%’ in SQL. And bad for the same reason, it means that we can’t use the index efficiently, but have to scan the entire index.

The test app also populate the database with about 30,000 documents, enough to get a handle on how thins are going, even if this is a relatively small data set. Let us see how this actually behaves:

image

imageimage

There are a few things to note here:

  • The query time is significant. – Leading wildcard queries tend to be expensive, they are essentially an O(N) query.
  • The results are… interesting. – In particular, look at the first few results, they are a match, but not what I would have expected.

A very simple change would be:

image

imageimage

All I did was remove the leading wildcard, and suddenly the results look at a lot nicer, if only because they are closer to what I am actually searching on. By the way, note that in the last result, we are actually finding a company whose second name starts with Lua, it isn’t just a raw StartsWith, since we are actually working on individual tokens levels, not the entire name level. This is because we indexed the Name as analyzed.

Let us see how this plays out in the actual app, first, using *term*:

image

Now, using term*:

image

That is somewhat better, but the results are still poor in terms of search relevance. Let us try something a little bit different:

 var ravenResults = rq
    .Search(x => x.Name, term)
    .Search(x=>x.Name, term + "*", escapeQueryOptions: EscapeQueryOptions.AllowPostfixWildcard
.Take(5) .ToList();

Here we force the search to search for the user’s actual term, as well as terms that starts with it. This means that any results that actually contains the name will stand up:

image

The most important thing about searching is to know that the problem is NOT that you can’t find enough information, it is that you find too much information that is not relevant.

But a better alternative all together might be using RavenDB suggest feature:

[HttpPost]
public JsonResult CompanySuggestions(string term)
{
    var rq = RavenSession.Query<Company, Companies_QueryIndex>()
        .Search(x => x.Name, term)
        .Take(5);

    var ravenResults = rq
        .ToList();

    if(ravenResults.Count < 5)
    {
        var suggestionQueryResult = rq.Suggest();

        ravenResults.AddRange(RavenSession.Query<Company, Companies_QueryIndex>()
                                  .Search(x => x.Name, string.Join(" ", suggestionQueryResult.Suggestions))
                                  .Take(5 - ravenResults.Count));
    }

    return Json(ravenResults.Select(x => new
    {
        id = x.Id,
        value = x.Name,
        description = string.Format("({0}, {1})", x.Category, x.Location)
    }));
}

Which result in:

image

What we are doing here is basically query the database for exact matches. Then, if we have no matches, we ask RavenDB to suggest additional words, and then query for those.

Here are the raw network traffic:

Query:  Name:<<lua>>
        Time: 1 ms
        Index: Companies/QueryIndex
        Results: 1 returned out of 1 total.

Suggest: 
    Index: Companies/QueryIndex
    Term: lua
    Field=Name
Time: 1 ms Query: Name:<<wlua luaz luka luau laua luoa lue luw lut lum luj lui lug luf lwa>> Time: 3 ms Index: Companies/QueryIndex Results: 4 returned out of 22 total.

As you can see, the speed difference between this and the first version is non trivial. More than that, note that it found companies with names such as “wlua”, which was also found in the first (*term*) version.

Just about any searching strategy would require that you take into account the dataset, the customer requirements, the user behavior and many more. But I would start with the suggestion option before I would go to anything as brute force as *term*.