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

Queries++ in RavenDB: I Suggest You Can Do Better

DZone's Guide to

Queries++ in RavenDB: I Suggest You Can Do Better

All users have one common tendency: they'll be entering weird stuff into your system. And when stuff doesn’t work, you’ll need to make it work, even it makes no sense.

· Database Zone ·
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.  

Sometimes, there is no escaping it and you must accept some user’s input into your system. That sad state of affairs is problematic because all users have one common tendency: they are going to be entering weird stuff into your system. And when stuff doesn’t work, you’ll need to make it work, even it makes no sense.

Let's take a simple example with this user, shall we?

image

Assuming that you have a search form for users, and a call goes to the call center, where you need to have the helpdesk staff search for that particular user.

Here are the queries that they tried:

  • Stefanie
  • Stephenie
  • Stefanee
  • Stepfanie
  • Stephany

Now, you may think poorly of the helpdesk staff (likely outsourced and non-native speakers), but the same thing can happen for Yasmin, Jasmyne, and Jasmyn (I literally took the first two examples I found on Twitter to show that this is a real issue).

How do you handle something like this? Well, if you are Google, you do this:

image

But what happens if you aren’t Google? Well, since we are talking about RavenDB here, you are going to run a suggestion query like so:

from index 'Users/Search' select suggest(Name, "Stefanie")

This requires us to have defined the Users/Search index and mark the Name field as viable for suggestions. This tends to be quite computing intensive during indexing time, but it allows us to make a suggestion query on the field, which will give us this result:

image

What's going on here? During indexing, RavenDB is going to generate a list of permutations of the data that is being indexed. Then, when you run a suggestion query, we can compare the user’s input to the data that has been indexed and suggest possible alternatives to what the user actually entered. This isn’t a generic selection; it is based on what you actually have in your system.

A more serious case is the international scene. When you have a user such as “André Sørina:”

image

How do you search for them? On my keyboard, I don’t know how to type this marks (diacritic, I had to search for that). If someone tried to tell me these over the phone, I would be completely lost. It’s a good thing that we have a good solution for that:

from index 'Users/Search' select suggest(Name, "andre")

Which will give us:

image

And now we can search for that, and find the user very easily.

This is a feature that we had since 2010, but it got a serious facelift and made easier to use in RavenDB 4.0.

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

Topics:
database ,querying ,database performance ,ravendb

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}