Tutorial: Fuzzy Text Search In MongoDB The Easy Way!
Dĵ ΝιΓΞΗΛψΚ
Posted on January 14, 2020
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.
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:
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.
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:
A data access library for MongoDB with an elegant api, LINQ support and built-in entity relationship management
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: