loading...
Cover image for Tutorial: Fuzzy Text Search In MongoDB The Easy Way!

Tutorial: Fuzzy Text Search In MongoDB The Easy Way!

djnitehawk profile image Đĵ ΝιΓΞΗΛψΚ Updated on ・2 min read

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

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; }
}

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();

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();

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();

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();

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

    Catherine Marcus
    Katheryne Markus
    Marcus Katerin Thompson

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

nuget nuget tests license

MongoDB.Entities

A light-weight .net standard library which simplifies access to mongodb by abstracting away the official .net mongodb driver and providing some additional features on top of it. The API is clean and intuitive resulting in less lines of code that is more human friendly than driver code.

More Info:

please visit the official website for detailed documentation:

https://mongodb-entities.com

Posted on by:

djnitehawk profile

Đĵ ΝιΓΞΗΛψΚ

@djnitehawk

Developer @ ChangeMon.com (Change Detection & Notification Service)

Discussion

pic
Editor guide
 

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.

 

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 🙏

 

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

 

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?

 

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.