Skip to main content

Mongo DB Atlas Search

Here is an example of Mongo DB atlas search. :::

Create search index

{
"mappings": {
"dynamic": true
}
}

How to use

Here is an example of query

const query = [
{
$search: {
compound: {
should: [
{
text: {
query: options.searchBy,
path: 'name',
score: {
boost: {
value: 6,
},
},
fuzzy: {},
},
},
{
text: {
query: options.searchBy,
path: ['description', 'year', 'others'],
fuzzy: {},
},
},
],
},
},
},
{
$match: {
isDeleted: false,
},
},
];
const movies = await Movies.aggregate(query)
.collation({ locale: 'en', strength: 2 })
.skip(options.limit * options.page - options.limit)
.limit(options.limit)
.exec();

Here the first priority is in the "name" field, after that it search for the keyword in the mentioned fields "['description', 'year', 'others']". You can manage the results accuracy by boosting the score.

score: {
boost: {
value: 6,
},
},

If you want to get the result if the search keywords are misspelled you can use fuzziness in your query.

fuzzy: {},

You have extra options in fuzzy

 - maxEdits : Maximum number of single-character edits required to match the specified search term. Value can be 1 or 2. The default value is 2

- prefixLength : Number of characters at the beginning of each term in the result that must exactly match. The default value is 0.

- maxExpansions : The maximum number of variations to generate and search for. This limit applies on a per-token basis. The default value is 50.

Syntax

{
$search: {
"index": <index name>, // optional, defaults to "default"
"text": {
"query": "<search-string>",
"path": "<field-to-search>",
"fuzzy": <options>,
"score": <options>,
"synonyms": "<synonyms-mapping-name>"
}
}
}

Fields

FieldTypeDescriptionNecessity
querystring or array of stringsThe string or strings to search for. If there are multiple terms in a string, Atlas Search also looks for a match for each term in the string separately.Required
pathstring or array of stringsThe indexed field or fields to search. You can also specify a wildcard path to search. See path construction for more information.Required
fuzzydocumentEnable fuzzy search. Find strings which are similar to the search term or terms. You can't use fuzzy with synonyms.Optional
fuzzy.maxEditsintegerMaximum number of single-character edits required to match the specified search term. Value can be 1 or 2. The default value is 2Optional
fuzzy.prefixLengthintegerNumber of characters at the beginning of each term in the result that must exactly match. The default value is 0.Optional
fuzzy.maxExpansionsintegerThe maximum number of variations to generate and search for. This limit applies on a per-token basis. The default value is 50.Optional
scoredocumentThe score assigned to matching search term results. Use one of the following options to modify the score: 1, boost: multiply the result score by the given number. 2, constant: replace the result score with the given number.Optional
synonymsstringRequired for running queries using synonyms.Optional