This article is part of a series. Please see the complete series above.
Visit the query term highlighting Playground.
The collection contains this document:
[
{
"_id": 1,
"name": "Introduction to Atlas Search"
}
]
Enable highlighting and project the searchHighlights
:
[
{
$search: {
index: "default",
text: {
query: "atlas",
path: "name"
},
highlight: {
path: "name"
}
}
},
{
$project: {
highlights: {
$meta: "searchHighlights"
}
}
}
]
The query searched for the word "atlas", and is shown in context of the full field value in the response:
[
{
"_id": 1,
"highlights": [
{
"score": 1.3466932773590088,
"path": "name",
"texts": [
{
"value": "Introduction to ",
"type": "text"
},
{
"value": "Atlas",
"type": "hit"
},
{
"value": " Search",
"type": "text"
}
]
}
]
}
]
The application code, when presenting the search results, can concatenate all the value
s, accentuating the hit
s.
Introduction to Atlas Search
Learn more search fundamentals!
Top comments (0)