DEV Community

Cover image for Tutorial: Fuzzy Text Search In MongoDB The Easy Way!
Dĵ ΝιΓΞΗΛψΚ
Dĵ ΝιΓΞΗΛψΚ

Posted on • Updated on

Tutorial: Fuzzy Text Search In MongoDB The Easy Way!

if you've ever worked with mongodb, you may already know that mongodb server does not have a built-in mechanism to search for documents based on fuzzy matching. say for example you have a couple of people saved in your database with the following names:

- Katheryne Markus
- Catherine Marcus
- Marcus Katerin Thompson
- Jack Jonas
Enter fullscreen mode Exit fullscreen mode

and the requirement is to retrieve all records that sounds similar to Catheryn Marcus.

we want the resulting record set to only include the first 3 people and the most relevant person to be on top.

let's see how we can achieve this goal step-by-step...

Getting Started

if you haven't already, please see the introductory article mentioned below in order to get a new project scafolded and setup before continuing with the rest of this article.

Define The Entity Class

add a new class file called Person.cs and add the following code to it:

public class Person : Entity
{
    public FuzzyString Name { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

in order to make fuzzy matching work with mongodb we need to store text data in a special FuzzyString type property. that class/type is provided by the MongoDB.Entities library we are using.

Create A Text Index

fuzzy text searching requires the use of a mongodb text index which can be easily created like this:

await DB.Index<Person>()
        .Key(p => p.Name, KeyType.Text)
        .CreateAsync();
Enter fullscreen mode Exit fullscreen mode

the above code should be self explanatory, if not please see the documentation here.

Store The Entities

await new[]
{   new Person { Name = "Jack Jonas" },
    new Person { Name = "Marcus Katerin Thompson" },
    new Person { Name = "Catherine Marcus" },
    new Person { Name = "Katheryne Markus" }
}.SaveAsync();
Enter fullscreen mode Exit fullscreen mode

nothing fancy here. just doing a bulk save of multiple records MongoDB.Entities style ;-)

Do The Fuzzy Search

var people = DB.Find<Person>()
               .Match(Search.Fuzzy, "Catheryn Marcus")
               .ExecuteAsync();
Enter fullscreen mode Exit fullscreen mode

here we're saying find Person entities that fuzzily matches the words Catheryn Marcus from the text index. you can read more about how this works under the hood in the documentation here.

Sort By Relevance

now that we have the results from the database, the following utility method can be used to get a sorted list that uses the levenshtein distance method.

var list = people.SortByRelevance("Catheryn Marcus", p => p.Name);

foreach (var person in list)
{
    Console.WriteLine(person.Name);
}

Console.Read();
Enter fullscreen mode Exit fullscreen mode

you will now see the following result displayed in the console window:

    Catherine Marcus
    Katheryne Markus
    Marcus Katerin Thompson
Enter fullscreen mode Exit fullscreen mode

which is exactly the end result we expected.

Next Steps...

i've purposefully tried to keep this tutorial as brief as possible to get your feet wet on the concepts of the library. if the above code seems easy and interesting please refer to the official website of MongoDB.Entities. you can also check out the source code on github:

GitHub logo dj-nitehawk / MongoDB.Entities

A data access library for MongoDB with an elegant api, LINQ support and built-in entity relationship management

license nuget nuget tests discord

MongoDB.Entities

A light-weight .net standard library with barely any overhead that aims to simplify access to mongodb by abstracting the official driver while adding useful features on top of it resulting in an elegant API surface which produces beautiful, human friendly data access code.

More Info:

please visit the official website for detailed documentation:

https://mongodb-entities.com

Oldest comments (5)

Collapse
 
lopugit profile image
Nikolaj

Is this possible using a javascript adapter?

those Search.Fuzzy and FuzzyString classes can't be unique to c can they? MongoDB must support fuzzy querying natively or?

Collapse
 
djnitehawk profile image
Dĵ ΝιΓΞΗΛψΚ

sorry mate this is a library written in c#. mongodb doesn't support fuzzy matching natevely. but it can be done manually with the sacrificing of some storage space. check here for an explanation of how it work.

Collapse
 
reijnemans profile image
reijnemans

Is there also a blog available how to migrate to mongoDB.entities from mongoDB.core.driver (for existing application). Especially how to change a string to FuzzyString.

Collapse
 
djnitehawk profile image
Dĵ ΝιΓΞΗΛψΚ

nothing like that has been written yet. I'll look into writing a migration tutorial when I find some free time. thanks for the suggestion 🙏

Collapse
 
reijnemans profile image
reijnemans

Should be nice, looking forward to it. Please let me know when it is ready :)