I’m using MongoDB Atlas Search to query documents that contain large text fields (e.g., documents with multiple pages of text). However, when I run a search query, MongoDB Atlas Search returns the highlights, but it includes all pages, even those that do not contain the search term in the highlighted fields.
My goal is to return only the pages that contain the search term in the highlighted section.
Here’s the query and db I’m currently using:
{
"_id": "documentId123",
"documentContent": {
"pages": [
{
"pageNumber": 1,
"textContent": "This is the first page. It talks about the concept of 'Dolce far niente'.",
},
{
"pageNumber": 2,
"textContent": "This page discusses various historical aspects.",
},
{
"pageNumber": 3,
"textContent": "Here, we delve deeper into 'Dolce far niente' and its cultural significance.",
},
{
"pageNumber": 4,
"textContent": "The final page provides a summary of the key points discussed.",
}
]
}
}
And query i use
[
{
"$search": {
"compound": {
"must": [
{
"embeddedDocument": {
"path": "documentContent.pages",
"operator": {
"text": {
"path": "documentContent.pages.textContent",
"query": "Dolce far niente"
}
}
}
}
]
},
"highlight": {
"path": "documentContent.pages.textContent"
}
}
},
{
$unwind: '$documentContent', // Unwind the documentContent array
},
{
$unwind: '$documentContent.pages', // Unwind the pages array
},
{
"$project": {
"_id": 0,
"pageNumber": "$documentContent.pages.pageNumber",
"highlights": {
"$meta": "searchHighlights"
}
}
}
]