Skip to content

MongoDB Atlas Search: Lucene index inside the database

Use Atlas Search to add full-text relevance, faceting, autocomplete, and vector search to MongoDB without standing up a separate Elasticsearch cluster.

Atlas Search is a Lucene-powered search index that sits alongside your Atlas data, synchronized continuously. From the query side, it’s just a new aggregation stage — $search — that returns documents ordered by relevance score. From the operations side, it removes a category of pain: no separate Elasticsearch cluster, no CDC pipeline keeping the search index in sync, no two-system consistency story to debug at 3 AM. This article covers the index definition, the query DSL, and the trade-offs against a separate dedicated search service.

How to verify

From the Atlas UI or via the Admin API, list defined search indexes and run a $search query:

atlas clusters search indexes list --clusterName prod
mongosh "mongodb+srv://user:pass@prod.xxxxx.mongodb.net"
use shop
db.products.aggregate([
  { $search: {
      index: "products_search",
      text: { query: "wireless headphones", path: ["name", "description"] }
  } },
  { $project: { name: 1, score: { $meta: "searchScore" } } },
  { $limit: 10 }
])

{ $meta: "searchScore" } exposes the Lucene score. The index name must match the one configured in Atlas; if you have only one index on a collection, you can omit it and $search defaults to "default".

What’s happening

Atlas Search runs a Lucene index per collection per index definition. As documents are inserted, updated, or deleted in the source collection, change-stream events drive index updates within seconds. Index definitions are JSON documents describing field mappings, analyzers, synonyms, and field-specific options (faceting, autocomplete, vector embeddings). The mapping dynamic: true indexes all string fields with default settings; explicit mappings tune per-field analyzers and disable fields you do not need to search.

Queries use the $search aggregation stage, which compiles to a Lucene query. Operators include text (analyzed text search), phrase, regex, range, near (geospatial), compound (boolean combinations), autocomplete, embeddingsSearch for vector similarity. Results return in relevance order with a score; downstream pipeline stages can re-rank, filter, or join with $lookup against other collections.

Atlas Search nodes are a separate tier in your Atlas cluster — dedicated CPU/memory/storage that does not contend with operational queries. Indexes are sharded automatically across search nodes and rebuilt on configuration change. The latency between a write in the data nodes and visibility in the search index is typically a second or two, sometimes longer under load — design for it.

The procedure

  1. Define a search index. Use explicit mappings for production; dynamic: true is for prototyping.

    {
      "name": "products_search",
      "mappings": {
        "dynamic": false,
        "fields": {
          "name": { "type": "string", "analyzer": "lucene.standard" },
          "description": { "type": "string", "analyzer": "lucene.english" },
          "category": { "type": "stringFacet" },
          "price": { "type": "number" },
          "embedding": { "type": "knnVector", "dimensions": 1536, "similarity": "cosine" }
        }
      }
    }
  2. Use $search for the primary text match.

    db.products.aggregate([
      { $search: {
          index: "products_search",
          compound: {
            must: [{ text: { query: "wireless", path: "name" } }],
            should: [{ text: { query: "noise-cancelling", path: "description" } }]
          }
      } },
      { $limit: 20 }
    ])
  3. Add $searchMeta for facets. A separate $searchMeta query returns facet counts without document content.

    db.products.aggregate([
      { $searchMeta: {
          index: "products_search",
          facet: {
            operator: { text: { query: "wireless", path: "name" } },
            facets: { category: { type: "string", path: "category" } }
          }
      } }
    ])
  4. Vector search uses embeddingsSearch (or the newer knnBeta).

    db.products.aggregate([
      { $search: {
          index: "products_search",
          embeddingsSearch: {
            path: "embedding",
            queryVector: [/* 1536-element vector */],
            k: 10
          }
      } }
    ])
  5. Configure synonyms in a separate collection and reference them from the index.

    db.synonyms.insertOne({
      mappingType: "equivalent",
      synonyms: ["laptop", "notebook", "ultrabook"]
    })
  6. Monitor index size and replication lag from the Atlas UI. Index size grows roughly linearly with field count and analyzer complexity.

Operational notes

  • Search indexes are eventually consistent with the source. A write to the source collection is visible in $search after a delay (usually seconds). Do not use $search for read-your-writes semantics on freshly inserted data.
  • Index rebuilds happen on configuration change. Adding a field to an existing index triggers a rebuild — non-blocking from a query perspective, but expensive.
  • The Atlas Search tier is sized separately. A workload-heavy search index requires more dedicated nodes than a small one. Right-size based on indexed document count and query QPS.
  • Vector search dimensionality matters. Higher dimensions give better semantic discrimination but linearly increase index size and query cost. 1536 (OpenAI text-embedding-3-small) is a common ceiling.
  • Compared to Elasticsearch: simpler ops, slightly less DSL surface. Atlas Search covers maybe 70-80% of what Elasticsearch does. Highly-customized scoring, learning-to-rank, or specific Elasticsearch plugins are escape hatches.

Stack Harbor designs Atlas Search index definitions for clients whose workloads need real relevance ranking but do not justify the operational overhead of a separate Elasticsearch cluster. We tune analyzers, design the index schema against the dominant query patterns, and integrate vector search where the application benefits. See /en/services/managed-operations/ for the engagement model.