Navigation

Path Construction

Overview

The path parameter is used by the Atlas Search operators to specify the field or fields to be searched. It may contain:

  • A string
  • An array of strings
  • A multi analyzer specification
  • An array containing a combination of strings and multi analyzer specifications

Note

Not all operators can use all the different types of paths. See the documentation for each individual operator for details on what types of path it supports.

Usage

To search only a single indexed field, use a quoted string in the path parameter. The following example searches a field named description.

"path": "description"

To search multiple indexed fields, use an array of quoted strings in the path parameter. Documents which match on any of the specified fields are included in the result set. The following example searches the description and type fields.

"path": ["description", "type"]

Note

The multi path option is available only to fields of type string.

If your index definition contains a field with multiple analyzers, you can specify which one to use. The path parameter can take an object with the following fields:

Field Description
value The name of the field to search.
multi The name of the alternate analyzer specified in a multi object in an index definition.
wildcard

The object containing the wildcard character * to match any character in the name of the field to search, including nested fields. A wildcard path:

  • Must be defined as an object.
  • Cannot contain the value or multi option.
  • Cannot contain multiple consecutive wildcard characters such as **.

Wildcard path is only accepted by the following operators:

In the following index definition, fields named names and notes use the standard analyzer. A field named comments uses standard as its default analyzer, and it also specifies a multi named mySecondaryAnalyzer which uses the lucene.whitespace analyzer.

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "names": {
        "type": "string",
        "analyzer": "lucene.standard"
      },
      "notes": {
        "type": "string",
        "analyzer": "lucene.standard"
      },
      "comments": {
        "type": "string",
        "analyzer": "lucene.standard",
        "multi": {
          "mySecondaryAnalyzer": {
            "analyzer": "lucene.whitespace",
            "type": "string"
          }
        }
      }
    }
  }
}

The following path example searches the comments field using the multi named mySecondaryAnalyzer in the index definition.

"path": { "value": "comments", "multi": "mySecondaryAnalyzer" }

To search a combination of indexed fields and fields with multiple analyzers, use an array. The following example searches the names and notes fields with the default analyzer, and the comments field using the multi named mySecondaryAnalyzer in the index definition.

"path": [ "names", "notes", { "value": "comments", "multi": "mySecondaryAnalyzer" } ]

The following path example searches all the fields that contain the letter n followed by any characters, and the comments field using the multi named mySecondaryAnalyzer in the index definition.

"path": [{"wildcard": "n*"}, { "value": "comments", "multi": "mySecondaryAnalyzer" }]

Examples

The following examples use a collection named cars which has the following documents:

{
  "_id" : 1,
  "type" : "sedan",
  "make" : "Toyota",
  "description" : "Blue four-door sedan, lots of trunk space. Three to four
  passengers."
}
{
  "_id" : 2,
  "type" : "coupe",
  "make" : "BMW",
  "description" : "Red two-door convertible, driver's-side airbag."
}
{
  "_id" : 3,
  "type" : "SUV",
  "make" : "Ford",
  "description" : "Black four-door SUV, three rows of seats."
}

Static field mappings allow you to specify how individual fields within a collection should be indexed and searched.

The index definition for the cars collection is as follows:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "make": {
        "type": "string",
        "analyzer": "lucene.standard"
      },
      "description": {
        "type": "string",
        "analyzer": "lucene.standard",
        "multi": {
          "simpleAnalyzer": {
            "analyzer": "lucene.simple",
            "type": "string"
          }
        }
      }
    }
  }
}

The preceding index definition specifies that the make field is indexed with the standard analyzer. The description field uses the standard analyzer by default, but it can also use the simple analyzer by specifying simpleAnalyzer with the multi parameter.

Nested Field Example

The following compound query searches the field post.body for the string broccoli, and also specifies that the field must not contain the string cauliflower.

db.posts.aggregate([
  {
    $search: {
      "compound": {
        "must": {
          "search": {
            "query": "broccoli",
            "path": "post.body"
          }
        },
        "mustNot": {
          "search": {
            "query": "cauliflower",
            "path": "post.body"
          }
        }
      }
    }
  }
])

The preceding query returns the document with _id: 1, in which the posts.body field contains the string broccoli.