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.NoteThe
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, theterm
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> } } } FieldTypeDescriptionRequired?Defaultquery
string or array of stringsThe term or terms to search for.yespath
string or array of stringsThe indexed field or fields to search. See path construction for more information.yeswildcard
booleannofalse
regex
booleannofalse
prefix
booleannofalse
fuzzy
objectEnable fuzzy search. Find strings which are similar to the search term or terms.nofuzzy.maxEdits
integerMaximum number of single-character edits required to match the specified search term.
NotemaxEdits
has a maximum allowable value of2
.no2
fuzzy.prefixLength
integerNumber of characters at the beginning of the result that must exactly match the search term.no0
score
objectModify 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.
noImportantThe
wildcard
,regex
,prefix
andfuzzy
options cannot be used in combination with each other.
Examples¶
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).
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 db.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.
1 db.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.
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 db.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." }