Docs Menu

search (Deprecated)

On this page

  • Definition
  • Syntax
  • Options
  • Examples
Warning

The search operator is deprecated. Use the text operator.

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.

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}

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 ({}) indicates that the default options should be used. Options are:

  • slop: the allowable distance within which words may be considered a phrase. The default is 0, meaning that words must be adjacent to be considered a phrase. See examples.
  • prefix: a boolean which indicates whether or not to treat the final word of the phrase as a prefix search. The default is false. May be used in conjuncion with the maxExpansions option. See example.
  • maxExpansions: a number which indicates the maximum number of expansions a phrase may expand to. Defaults to 50, with a maximum value of 1000. Must be used in conjunction with the prefix option.

    If the number of terms in a phrase: { prefix: true } } match exceeds maxExpansions, your query may not return all matching results. This may be avoided by setting maxExpansions to a higher value, up to 1000. If there are more than 1000 matching terms, your query is not guaranteed to match all results.

    Note

    Increasing the value of maxExpansions may cause your query to consume more memory and performance may decrease.

no
empty
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

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

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.

1db.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.

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:

1db.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.

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

1db.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.

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.

1db.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.

1db.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.

←  regexspan →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.