Tutorial: Fuzzy Text Search In MongoDB The Easy Way!

djnitehawk

Dĵ ΝιΓΞΗΛψΚ

Posted on January 14, 2020

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:






💖 💪 🙅 🚩
djnitehawk
Dĵ ΝιΓΞΗΛψΚ

Posted on January 14, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related