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:

