Navigation

Highlighting

On this page

The Full Text Search highlight option adds fields to the result set that display search terms in their original context. You can use it in conjunction with $searchBeta operators to display search terms as they appear in the returned documents, along with the adjacent text content (if any). highlight results are returned as part of the $meta field.

Definition

highlight has the following syntax:

{
  $searchBeta: {
    "index": <index name>, // optional, defaults to "default"
    "<operator>": { // may be ``search``, ``term``, ``compound``, or ``span``
      "query": "<search-string>",
      "path": "<field-to-search>"
    },
    "highlight": { "path": "<field-to-search>" }
  }
},
{
  $project: {
    "highlights": { "$meta": "searchHighlights" } }
  }
}

The path field may contain:

  • A string
  • An array of strings
  • A multi analyzer specification
  • An array containing a combination of strings and multi analyzer specifications

See Path Construction for more information.

The "$meta": "searchHighlights" field contains the highlighted results. That field isn’t part of the original document, so it is necessary to use a $project pipeline stage to add it to the query output.

Example

The following example uses a collection called fruit that contains the following documents:

{
  "_id" : 1,
  "type" : "apple",
  "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp."
},
{
  "_id" : 2,
  "type" : "banana",
  "description" : "Bananas are usually sold in bunches of five or six."
}

Note

One useful aspect of highlighting is that it reveals the original text returned by the search query, which may not be exactly the same as the search term. For example, if you use a language-specific analyzer, your text searches return all the stemmed variations of your search terms.

The fruit collection has an index definition that uses the english analyzer and dynamic field mappings.

{
  "analyzer": "lucene.english",
  "searchAnalyzer": "lucene.english",
  "mappings": {
    "dynamic": true
  }
}

The following query searches for variety and bunch in the description field of the fruit collection, with the highlight option enabled.

The $project pipeline stage restricts the output to the description field and adds a new field called highlights, which contains highlighting information.

db.fruit.aggregate([
  {
    $searchBeta: {
      "search": {
        "path": "description",
        "query": ["variety", "bunch"]
      },
      "highlight": {
        "path": "description"
      }
    }
  },
  {
    $project: {
      "description": 1,
      "_id": 0,
      "highlights": { "$meta": "searchHighlights" }
    }
  }
])

The query returns the following results:

{
  "description": "Bananas are usually sold in bunches of five or six.",
  "highlights" : [
    {
      "path" : "description",
      "texts" : [
        {
          "value" : "Bananas are usually sold in ",
          "type" : "text"
        },
        {
          "value" : "bunches",
          "type" : "hit"
        },
        {
          "value" : " of five or six.",
          "type" : "text"
        }
      ],
      "score" : 1.2841906547546387
    }
  ]
}
{
  "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp.",
  "highlights" : [
    {
      "path" : "description",
      "texts" : [
        {
          "value" : "Apples come in several ",
          "type" : "text"
        },
        {
          "value" : "varieties",
          "type" : "hit"
        },
        {
          "value" : ", including Fuji, Granny Smith, and Honeycrisp.",
          "type" : "text"
        }
      ],
      "score" : 1.2178014516830444
    }
  ]
}

The highlights field is an array containing the following output fields:

Field Type Description
path string Document field which returned a match.
texts array of objects Each search match returns one or more objects, containing the matching text and the surrounding text (if any).
texts.value string Text from the field which returned a match.
texts.type string Returned value is either hit or text. Results of type hit contain the string which returned a match. Results of type text contain the text content adjacent to the matching string.
score float The score assigned to the returned document.

The search term bunch returns a match on the document with _id: 2, because the description field contains the word bunches. The search term variety returns a match on the document with _id: 2, because the description field contains the word varieties. See stemming.