Docs Menu

term (Deprecated)

On this page

  • Definition
  • Examples
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.

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

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

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).

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

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

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.

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

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

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.
1db.fruit.aggregate([
2 {
3 $search: {
4 "term": {
5 "path": "description",
6 "query": ["fuj", "bannas"],
7 "fuzzy": { "maxEdits": 1, "prefixLength": 2 }
8 }
9 }
10 },
11 {
12 $project: {
13 "_id": 0,
14 "description": 1
15 }
16 }
17])

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."
}
←  spantext →

On this page

Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.