I have a document with a structure like this:
{
"_id": "",
"title": "Todo Demo",
"todos": [
{
"todo": "Travel to France",
"isCompleted": false,
},
{
"todo": "Send money to rome",
"isCompleted": false,
},
{
"todo": "Play football",
"isCompleted": false,
}
],
}
I want to use Atlas search to search from both the title field and the todos.todo field in the embedded document.
This is my index mapping definition:
{
"mappings": {
"dynamic": false,
"fields": {
"title": {
"analyzer": "lucene.standard",
"type": "string"
},
"todos": {
"dynamic": false,
"fields": {
"todo": {
"analyzer": "lucene.standard",
"type": "string"
}
},
"type": "embeddedDocuments"
}
}
}
}
Here is my search aggregation pipeline:
const searchtodos = await this.todoModel.aggregate([
{
$search: {
index: 'search_todos',
embeddedDocument: {
path: 'todos',
operator: {
compound: {
must: [
{
text: {
query: search,
path: ['todos.todo'],
fuzzy: {
maxEdits: 1,
maxExpansions: 10,
},
},
},
],
should: [
{
phrase: {
query: search,
path: ['todos.todo'],
slop: 2,
},
},
],
},
},
},
count: {
type: 'total',
},
},
},
{
$match: query,
},
{
$lookup: {
from: 'userentities',
localField: 'userId',
foreignField: '_id',
as: 'userData',
},
},
{
$unwind: '$userData',
},
{
$project: todoProject,
},
{
$facet: {
totalCount: [{ $count: 'count' }],
documents: [
{
$skip: (page - 1) * limit,
},
{
$limit: limit,
},
],
},
},
]);
However, if I search with a string that matches the todos.todo field e.g “football”, the search works properly but if I search for a string that matches the title field e.g “Demo”, the search is not working. What update do I need to make to my search aggregation so that it can return result from both the title field and the todos.todo field?