Using Atlas Search

1. Creating Search Index

db.products.createSearchIndex("default", {
  mappings: { dynamic: true }
});

2. Using $search Aggregation Stage

db.products.aggregate([
  { $search: { index:"default", text:{ query:"wireless", path:"name" } } },
  { $limit: 20 }
]);
OperatorDetail
textAnalyzed full-text search
phraseExact phrase match
queryStringLucene syntax (AND, OR, NOT, *)

4. Using Autocomplete

{ $search: { autocomplete: { query:"wir", path:"name", tokenOrder:"sequential" } } }
SettingDetail
Index type"autocomplete" with edgeGram tokenizer
fuzzyTypo tolerance

5. Using Fuzzy Matching

OptionDetail
fuzzy.maxEdits1–2 typos
fuzzy.prefixLengthChars that must match
fuzzy.maxExpansionsCap on term variants

6. Using Wildcards

OperatorDetail
wildcardGlob patterns: *, ?
regexFull regex with allowAnalyzedField
db.products.aggregate([
  { $searchMeta: { facet: {
      operator: { text: { query:"laptop", path:"name" } },
      facets: { brandFacet: { type:"string", path:"brand" } }
  } } }
]);

8. Using Highlighting

OptionDetail
highlight.pathField(s) to highlight
Output$meta:"searchHighlights"

9. Scoring and Ranking Results

MechanismDetail
$meta:"searchScore"Relevance score per doc
score.boostMultiply
score.functionCustom expression (decay, log, etc.)

10. Using Compound Queries

{ $search: { compound: {
  must:    [{ text:{ query:"laptop", path:"name" } }],
  should:  [{ text:{ query:"gaming", path:"tags", score:{boost:{value:2}} } }],
  filter:  [{ range:{ path:"price", lte: 2000 } }],
  mustNot: [{ text:{ query:"refurbished", path:"description" } }]
} } }
ClauseDetail
mustRequired + contributes to score
shouldOptional; boosts score
filterRequired, no score impact
mustNotExclusion

11. Configuring Custom Analyzers

{
  "analyzers": [{
    "name": "myCustom",
    "charFilters": [{"type":"htmlStrip"}],
    "tokenizer": {"type":"standard"},
    "tokenFilters": [{"type":"lowercase"}, {"type":"asciiFolding"}]
  }],
  "mappings": { "dynamic": false,
    "fields": { "title": {"type":"string","analyzer":"myCustom"} }
  }
}
ComponentExamples
charFiltershtmlStrip, mapping, persian
tokenizersstandard, keyword, whitespace, edgeGram, nGram
tokenFilterslowercase, asciiFolding, stemmer, stopwords, synonym, shingle