0PricingLogin
MongoDB Academy · Lesson

Autocomplete and Fuzzy Matching

Learners will configure an autocomplete analyzer on a field and write fuzzy queries to handle typos in user search input.

Why Autocomplete and Fuzzy Search Matter

Modern search experiences require two key features: autocomplete (suggesting completions as the user types) and fuzzy matching (finding results even when the user misspells a query). These features significantly improve user experience—autocomplete reduces search friction by guiding users to valid queries, while fuzzy matching ensures a typo does not result in 'no results found'. Atlas Search provides both through dedicated operators and analyzers.

Configuring an Autocomplete Analyzer

Autocomplete requires a special field configuration in the Atlas Search index. Use the autocomplete data type in the index mapping for the field you want to support type-ahead. This causes Atlas to index n-grams and edge n-grams of the field value—substrings that match partial inputs. The tokenization option can be 'edgeGram' (left-anchored substrings) or 'nGram' (all substrings), with optional minGrams and maxGrams sizes.

// Atlas Search index definition with autocomplete field
{
  'mappings': {
    'dynamic': false,
    'fields': {
      'name': [
        {
          'type': 'string',           // for regular text search
          'analyzer': 'lucene.standard'
        },
        {
          'type': 'autocomplete',     // for type-ahead queries
          'tokenization': 'edgeGram', // 'mongod' -> 'm', 'mo', 'mon', 'mong', 'mongo', 'mongod'
          'minGrams': 2,
          'maxGrams': 10
        }
      ]
    }
  }
}

All lessons in this course

  1. Creating an Atlas Search Index
  2. Writing $search Queries: Text, Phrase, and Wildcard
  3. Autocomplete and Fuzzy Matching
  4. Facets and Compound Queries
← Back to MongoDB Academy