Navigation

term (Operator)

On this page

Definition

term

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

term has the following syntax:

{
  $searchBeta: {
    "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> }
    }
  }
}
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. no false
regex boolean Enable regular expression searching. no false
prefix boolean Use the string specified in query as a prefix and search for any term which starts with it. 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

For beta purposes, 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

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:

{
  "_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 a Full Text 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 Full Text Search example performs a basic search of the description field for the query term several, using the term operator.

db.fruit.aggregate([
  {
    $searchBeta: {
      "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 returns no results. The standard analyzer which was used to create the index lower-cases all tokens, and the term operator specifies an unanalyzed search. Smith is not in the index, but smith is.

db.fruit.aggregate([
  {
    $searchBeta: {
      "term": {
        "query": "Smith",
        "path": "description"
      }
    }
  }
])

Wildcard Example

The wildcard option allows you to use the * character, which evaluates to zero or more characters of any value within a given token.

db.fruit.aggregate([
  {
    $searchBeta: {
      "term": {
        "query": "s*l*",
        "path": "description",
        "wildcard": true
      }
    }
  }
])

The above query returns both the documents in the collection. s*l* matches several in the first document’s description field and sold in the second.

Regular Expression Example

db.fruit.aggregate([
  {
    $searchBeta: {
      "term": {
        "query": "([a-z]{7})",
        "path": "description",
        "regex": true
      }
    }
  }
])

The above query returns both the documents in the collection, because both description fields contain seven-letter words.

Prefix Example

The prefix option allows you to search for all tokens which begin with a given string.

db.fruit.aggregate([
  {
    $searchBeta: {
      "term": {
        "query": "sev",
        "path": "description",
        "prefix": true
      }
    }
  }
])

The above query returns the following result:

{
  "_id" : 1,
  "type" : "apple",
  "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp."
}

The document with _id: 1 matches the query because the word several in the description field begins with sev.

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.

  • The maxEdits field specifies that only one character change is allowed to match the query to the document.
  • The prefixLength field specifies that the first two characters of the query term may not be changed to match a result.
db.fruit.aggregate([
  {
    $searchBeta: {
      "term": {
        "path": "description",
        "query": ["fuj", "bannas"],
        "fuzzy": { "maxEdits": 1, "prefixLength": 2 }
      }
    }
  }

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." }