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
Field | Type | Description | Necessity |
---|---|---|---|
query | string or array of strings | The 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 |
path | string or array of strings | The indexed field or fields to search. You can also specify a wildcard path to search. See path construction for more information. | Required |
fuzzy | document | Enable fuzzy search. Find strings which are similar to the search term or terms. You can't use fuzzy with synonyms. | Optional |
fuzzy.maxEdits | integer | Maximum number of single-character edits required to match the specified search term. Value can be 1 or 2. The default value is 2 | Optional |
fuzzy.prefixLength | integer | Number of characters at the beginning of each term in the result that must exactly match. The default value is 0. | Optional |
fuzzy.maxExpansions | integer | The maximum number of variations to generate and search for. This limit applies on a per-token basis. The default value is 50. | Optional |
score | document | The 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 |
synonyms | string | Required for running queries using synonyms. | Optional |