Lesson 25 +10 XP

Atlas Search

Atlas Search

Atlas Search is MongoDB Atlas's built-in full-text search engine, powered by Apache Lucene - the same engine behind Elasticsearch and Solr.

How it differs from normal indexes

Regular indexes are created with db.collection.createIndex(). Atlas Search indexes are built in the Atlas UI (under the Search tab) and then used from an aggregation pipeline.

Create a search index

In the Atlas dashboard:

  1. Open your cluster's Search tab.
  2. Click Create Search Index.
  3. Use the Visual Editor, name it, and choose the database and collection.
  4. Wait for the index to finish building.

Search inside an aggregation pipeline

Use the $search stage - it must be the first stage:

db.movies.aggregate([
  {
    $search: {
      index: "default",
      text: {
        query: "star wars",
        path: "title"
      }
    }
  },
  { $project: { title: 1, year: 1 } }
])
  • If the index is named default, you can omit the index field.
  • text.query is the search text; text.path is the field to search.
  • This finds movies whose title contains "star" or "wars".

Key facts

  • $search can only be the first stage of a pipeline.
  • It returns relevance-ranked results.
  • Built in the Atlas UI, not via createIndex().

TL;DR

  • Atlas Search = full-text search powered by Lucene.
  • Search indexes are created in the Atlas UI.
  • Use the $search stage (first stage only) in an aggregation pipeline.