Navigation

term (Deprecated)

On this page

Definition

term

The term operator specifies an unanalyzed search. The engine searches for the query terms exactly as they are indexed, with no analyzer modifications at query time. Any analyzer which was used during the index creation still applies to the index, but the search itself does not use an analyzer.

Note

The term operator normalizes the query terms in the same way they are normalized in the index. That is, when you use it with the Standard Analyzer, the operator converts your query terms to lower case. When you use it with the Keyword Analyzer, the term operator performs an exact match on the field.

term has the following syntax:

{
  $search: {
    "index": <index name>, // optional, defaults to "default"
    "term": {
      "query": "<search-term>",
      "path": "<field-to-search>",
      "wildcard": <boolean>,
      "regex": <boolean>,
      "prefix": <boolean>,
      "fuzzy": { "maxEdits": <integer>, "prefixLength": <integer> },
      "score": <options>
    }
  }
}
Field Type Description Required? Default
query string or array of strings The term or terms to search for. yes  
path string or array of strings The indexed field or fields to search. See path construction for more information. yes  
wildcard boolean

Enable wildcard searching.

Note

The wildcard option is deprecated for the term operator. Use the wildcard operator instead.

no false
regex boolean

Enable regular expression searching.

Note

The regex option is deprecated for the term operator. Use the regex operator instead.

no false
prefix boolean

Use the string specified in query as a prefix and search for any term which starts with it.

Note

The prefix option is deprecated for the term operator. Use the wildcard or regex operators instead.

no false
fuzzy object Enable fuzzy search. Find strings which are similar to the search term or terms. no  
fuzzy.maxEdits integer

Maximum number of single-character edits required to match the specified search term.

Note

maxEdits has a maximum allowable value of 2.

no 2
fuzzy.prefixLength integer Number of characters at the beginning of the result that must exactly match the search term. no 0
score object

Modify the score assigned to matching search term results. Options are:

  • boost: multiply the result score by the given number.
  • constant: replace the result score with the given number.
no  

Important

The wildcard, regex, prefix and fuzzy options cannot be used in combination with each other.

Examples

The following examples use a collection called fruit which contains the following documents:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
  "_id" : 1,
  "type" : "apple",
  "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp."
},
{
  "_id" : 2,
  "type" : "banana",
  "description" : "Bananas are usually sold in bunches of five or six."
}

The fruit collection has an Atlas Search index on the description field which uses the standard analyzer. The standard analyzer lower-cases all words and disregards common stop words ("the", "a", "and", etc).

Basic Example

The following Atlas Search example performs a basic search of the description field for the query term several, using the term operator.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
db.fruit.aggregate([
  {
    $search: {
      "term": {
        "query": "several",
        "path": "description"
      }
    }
  }
])

The above query returns the document with _id: 1, which has the word several in the description field.

The following example performs a basic search of the description field for the query term Smith. The standard analyzer, which was used to create the index lower-cases all tokens, and the term operator also converts the query term to lower case.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
db.fruit.aggregate([
  {
    $search: {
      "term": {
        "query": "Smith",
        "path": "description"
      }
    }
  }
])

The above query returns the document with _id: 1, which has the word Smith in the description field.

Fuzzy Example

Use the fuzzy option to search for strings which are similar to a term or terms which you specify.

The following query searches for strings which are within one character of the strings fuj and bannas.

  • maxEdits field specifies that only one character change is allowed to match the query to the document.
  • prefixLength field specifies that the first two characters of the query term may not be changed to match a result.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
db.fruit.aggregate([
  {
    $search: {
      "term": {
        "path": "description",
        "query": ["fuj", "bannas"],
        "fuzzy": { "maxEdits": 1, "prefixLength": 2 }
      }
    }
  },
  {
    $project: {
       "_id": 0,
       "description": 1
    }
  }
])

The above query finds both of the documents in the collection. The $project stage limits the output to the description field.

{
  "description" : "Bananas are usually sold in bunches of five or six."
},
{
  "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp."
}