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)

Static Mapping

search_index.json

{
  "name": "sample_supplies-sales-static",
  "searchAnalyzer": "lucene.standard",
  "analyzer": "lucene.standard",
  "collectionName": "sales",
  "database": "sample_supplies",
  "mappings": {
      "dynamic": false,
      "fields": {
          "storeLocation": {
              "type": "string"
          }
      }
  }
}
atlas clusters search indexes create --clusterName myAtlasClusterEDU -f /app/search_index.json

Using $search and Compound Operators

The compound operator within the $search aggregation stage allows us to give weight to different field and also filter our results without having to create additional aggregation stages. The four options for the compound operator are "must", "mustNot, "should", and "filter".

"must" will exclude records that do not meet the criteria. "mustNot" will exclude results that do meet the criteria. "should" will allow you to give weight to results that do meet the criteria so that they appear first. "filter" will remove results that do not meet the criteria.

$search {
  "compound": {
    "must": [{
      "text": {
        "query": "field",
        "path": "habitat"
      }
    }],
    "should": [{
      "range": {
        "gte": 45,
        "path": "wingspan_cm",
        "score": {"constant": {"value": 5}}
      }
    }]
  }
}

Clone this wiki locally