Pranav Suri
Pranav Suri

Reputation: 65

How to create a MongoDB Atlas Search index for substring matching with up to 2 typos on firstName, lastName, username?

I have a MongoDB collection called users with documents like the following:

{
  "_id": ObjectId("6769c7a725f248c843ac57f8"),
  "firebaseId": "gg6PNHieVmWNFrwuWlh2wcezyDh1",
  "email": "[email protected]",
  "emailVerified": false,
  "createdAt": ISODate("2024-12-23T20:27:19.411Z"),
  "lastUpdated": ISODate("2024-12-23T20:27:20.085Z"),
  "bio": "Admoveo adfero ... advoco tactus.",
  "dateOfBirth": ISODate("1987-04-19T15:36:18.038Z"),
  "displayName": "Shana_Mills",
  "firstName": "Mertie",
  "gender": "MALE",
  "lastName": "Roberts",
  "location": {
    "city": "Blue Springs",
    "country": "Djibouti",
    "coordinates": {
      "latitude": 23.9406,
      "longitude": 100.8504
    }
  },
  "profilePicture": "https://avatars.githubusercontent.com/u/98341203",
  "rating": 2,
  "username": "mose8854"
}

I want users of my app to be able to search by firstName, lastName, and username such that:

  1. Substring matches are allowed (e.g. searching “Mer” should find “Mertie”).
  2. Up to 2 character typos or fuzzy matching is allowed (i.e. “Mrtie” or “Merti” should still find “Mertie”).

I’ve tried building a search index in the MongoDB Atlas UI but can’t figure out the correct JSON config. How should I define the mapping (e.g., analyzers, autocomplete/fuzzy settings, etc.)?

Also, is there a best practice on whether to define this search index in code (using an IaC approach) versus doing it through the Atlas UI? Thanks!

Upvotes: 0

Views: 31

Answers (0)

Related Questions