DEV Community

Brian Swank
Brian Swank

Posted on • Edited on • Originally published at swank.dev

Implement Fuzzy Text Search with Mongoose

There are a bunch of great services that make search really simple, but adding simple fuzzy text search to a Mongoose model is a quick & easy win for when those services are overkill.

Prerequisites

You'll need a Node project up and running with Mongoose installed and at least one Mongoose model.

A Little Bit About Fuzzy Search

Fuzzy search is a standard in search implementations. Users expect it and we should deliver as advocates of our users. A simple text search needs to allow for a small typo, a missing space, and other errors.

Text search in MongoDB is fairly straight-forward. The $regex operator allows for partial-text search and the $text operator makes a full-text search possible, but enabling fuzzy search is a little more complicated.

Indexing & n-grams

In order to match something like "uzz" to "fuzzy" we need to break "fuzzy" up into smaller pieces and index it. mongoose-fuzzy-searching is a simple package that helps us index text fields on our documents with n-grams, allowing us to match smaller text samples to potential matches in our collections – aka fuzzy search.

Implementation

Let's get started by installing the necessary package: npm i mongoose-fuzzy-searching.

With our package installed, we can add it as a plugin to our schema:

import mongoose, { Schema } from 'mongoose'
import mongooseFuzzySearching from 'mongoose-fuzzy-searching'

const user = new Schema({
  firstName: String
})

user.plugin(mongooseFuzzySearching, { fields: ['firstName'] })

export default mongoose.model('User', user)
Enter fullscreen mode Exit fullscreen mode

Now that we have the plugin added, any new documents added to our User collection will have the appropriate text index! In order to use our newly-implemented plugin, all we need to do is call the fuzzySearch method on our collection instead of find:

import User from '../models/User'

const users = await User.fuzzySearch('query')
Enter fullscreen mode Exit fullscreen mode

Given an empty query, our fuzzySearch method will act just like a find call, returning all relavent results.

Further Reading

To learn more about how to use mongoose-fuzzy-searching with existing data, it's various options, or with more complex find() calls, check out the readme on NPM.


Need some help? Feel free to reach out.

Top comments (5)

Collapse
 
paras594 profile image
Paras 🧙‍♂️

I found this article on swank dev site and I am glad that i found this article here tooo. Just wanted to say amazing post. keep it up sir !! you rock ; )

Collapse
 
imtemporaryhere profile image
ImTemporaryHere

im quite dissapointed cause i have string value in my document with - character, and if do any query with this one, it send empty array

Collapse
 
briansw profile image
Brian Swank

Send me a link to your repo and I'll take a look!

Collapse
 
jrafflenbeul profile image
jrafflenbeul • Edited

I also have problems, am working with Next.js and TypeScript (package doesn't provide types as well). Only get an empty array (no error). You can see the API route, where I try to use the fuzzy search in the image I appended. Help is much appreciated :)

Thread Thread
 
briansw profile image
Brian Swank

Feel free to send me a link to a reproducible example and I'll take a look!