MongoDB
Beginner
1 min read
Atlas Search
Example
// Atlas Search index definition (JSON, defined in Atlas UI)
// {
// "mappings": {
// "dynamic": false,
// "fields": {
// "title": { "type": "string", "analyzer": "lucene.english" },
// "description": { "type": "string", "analyzer": "lucene.english" },
// "price": { "type": "number" },
// "category": { "type": "stringFacet" }
// }
// }
// }
// $search query in aggregation pipeline
db.products.aggregate([
{ $search: {
index: "default",
compound: {
must: [{
text: {
query: "wireless headphones",
path: ["title", "description"],
fuzzy: { maxEdits: 1 }
}
}],
filter: [{
range: { path: "price", gte: 50, lte: 300 }
}]
}
}},
{ $limit: 10 },
{ $project: {
title: 1, price: 1,
score: { $meta: "searchScore" }
}}
])