search (Deprecated)
On this page
The search
operator is deprecated. Use the
text operator.
Definition
search
The
search
operator performs a full-text search using the analyzer specified in the index configuration. If no search analyzer is specified, the default standard analyzer is used.
Syntax
search
has the following syntax:
1 { 2 $search: { 3 "index": <index name>, // optional, defaults to "default" 4 "search": { 5 "query": "<search-string>", 6 "path": "<field-to-search>", 7 "phrase": <options>, 8 "score": <options> 9 } 10 } 11 }
Options
search
uses the following terms to construct a query:
Field | Type | Description | Necessity | Default |
---|---|---|---|---|
query | string or array of strings | The string or strings to search for. | yes | |
path | string or array of strings | The indexed field or fields to search. See path
construction for more information. | yes | |
phrase | object | Match documents containing an ordered sequence of terms. An
empty object (
| no | empty |
score | object | Modify the score assigned to matching search term results. Options are:
| no |
Examples
The examples on this page use a collection called fruit
that 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 that 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 string several
, using
the $search
aggregation pipeline stage and the
search
operator.
1 db.fruit.aggregate([ 2 { 3 $search: { 4 "search": { 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.
Array Example
You can search for multiple strings with an array value for the
query
parameter. If any of the strings in the array match a
document, that document is returned.
The following example searches for documents with the strings
several
, bunches
, and/or oranges
in the description field:
1 db.fruit.aggregate([ 2 { 3 $search: { 4 "search": { 5 "query": ["several", "bunches", "oranges"], 6 "path": "description" 7 } 8 } 9 } 10 ])
The above query returns both of the documents in the collection because
the first document has the word several
in the description
field and the second document has the word bunches
in the
description
field.
Phrase Examples
Phrase Example with Default Options
The following query performs a phrase search. Phrase searches match
on ordered sequences of words, with a degree of precision specified
by the slop
option.
In following example, the phrase
parameter has an empty object as
its value, so MongoDB uses the default values { "slop": 0, "prefix":
false }
.
1 db.fruit.aggregate([ 2 { 3 $search: { 4 "search": { 5 "query": "sold in bunches", 6 "path": "description", 7 "phrase": {} 8 } 9 } 10 } 11 ])
The above query returns the document with _id: 2
.
Phrase Example with Higher Precision
You can set the slop
option to a higher or lower integer to adjust
the required precision of the phrase match. A higher value matches
on documents in which the words in the phrase may be farther apart,
while a value of 0
requires an exact match.
1 db.fruit.aggregate([ 2 { 3 $search: { 4 "search": { 5 "query": "Bananas in bunches", 6 "path": "description", 7 "phrase": { "slop": 1 } 8 } 9 } 10 } 11 ])
The above phrase search returns no results. The document with
_id: 2
contains all the words in the query
parameter in its
description
field, but there are three words separating Bananas
from in bunches
and the slop
parameter is set to 1
.
Setting the prefix
option to true
performs a wildcard search
based on the words in the query
parameter.
1 db.fruit.aggregate([ 2 { 3 $search: { 4 "search": { 5 "query": "several var", 6 "path": "description", 7 "phrase": { "prefix": true } 8 } 9 } 10 } 11 ])
The above query returns the document with _id: 1
, because
the description
field contains the phrase several varieties
and the prefix
parameter is set to true
.