Skip to content

Atlas Search

Zhamri Che Ani edited this page Feb 1, 2025 · 3 revisions

search_index.json

{
    "name": "sample_supplies-sales-dynamic",
    "searchAnalyzer": "lucene.standard",
    "analyzer": "lucene.standard",
    "collectionName": "sales",
    "database": "sample_supplies",
    "mappings": {
        "dynamic": true
    }
}

The /app/search_index.json file contains the Atlas search index definition. Here is an overview of the fields defined:

  1. The name field specifies the name of the index as it will appear in MongoDB Atlas.
  2. The searchAnalyzer specifies the analyzer to apply to query text before searching with it. In this example, we use the default lucene.standard analyzer.
  3. The analyzer defines how Atlas search will turn a string field's contents into searchable terms. It defines the tokenizer used to extract tokens from the > text and filters to use. In this example, we use the default lucene.standard analyzer.
  4. The collectionName and database define the collection and database on which to create the index.
  5. The mappings object is used to define the mapping to use, dynamic or static. In this example, we are using dynamic which means that we want Atlas > Search to automatically index all supported field types.

To create the Atlas Search index:

atlas clusters search indexes create --clusterName myAtlasClusterEDU -f /app/search_index.json

To verify that the index creation has started, please run the following command in the terminal:

atlas clusters search indexes list --clusterName myAtlasClusterEDU --db sample_supplies --collection sales

Run the following findOne() command to obtain sample document.

db.sales.findOne()
var pipeline = [
{
  $search: {
    index: "sample_supplies-sales-dynamic",
    text: {
      query: "notepad",
      path: { "wildcard": "*" }
    } } },
{
  $set: {
    score: {
      $meta: "searchScore" }
    }
}
]
db.sales.aggregate(pipeline)

Clone this wiki locally