SyntaxStudy
Sign Up
MongoDB Beginner 1 min read

Atlas Search

Atlas Search is a full-text search engine built into MongoDB Atlas, powered by Apache Lucene. It provides much more powerful text search capabilities than the built-in $text operator, including fuzzy matching, autocomplete, facets, relevance scoring, custom analysers, and synonym support. You define search indexes in the Atlas UI or through the API, specifying which fields to index and how they should be analysed. Queries use the $search aggregation stage with operators such as text, phrase, fuzzy, wildcard, range, and compound. Atlas Search indexes are eventually consistent with the underlying collection and do not impact write performance since indexing is asynchronous.
Example
// Atlas Search index definition (JSON, defined in Atlas UI)
// {
//   "mappings": {
//     "dynamic": false,
//     "fields": {
//       "title":       { "type": "string", "analyzer": "lucene.english" },
//       "description": { "type": "string", "analyzer": "lucene.english" },
//       "price":       { "type": "number" },
//       "category":    { "type": "stringFacet" }
//     }
//   }
// }

// $search query in aggregation pipeline
db.products.aggregate([
  { $search: {
      index: "default",
      compound: {
        must: [{
          text: {
            query: "wireless headphones",
            path: ["title", "description"],
            fuzzy: { maxEdits: 1 }
          }
        }],
        filter: [{
          range: { path: "price", gte: 50, lte: 300 }
        }]
      }
  }},
  { $limit: 10 },
  { $project: {
      title: 1, price: 1,
      score: { $meta: "searchScore" }
  }}
])