> ## Documentation Index
> Fetch the complete documentation index at: https://www.meilisearch.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Search with snippets

> Return highlighted and cropped result snippets to show users exactly where their query matched in each document.

Search snippets let you display the portion of a document that matches a user's query, with matched terms highlighted. This helps users quickly evaluate whether a result is relevant without reading the full document content.

Meilisearch provides two complementary features for this: **highlighting** wraps matched terms in tags, and **cropping** trims long text fields to show only the relevant portion around matched terms.

## Highlighting matched terms

Use `attributesToHighlight` to specify which fields should have matched terms wrapped in highlight tags. Set it to `["*"]` to highlight all [displayed attributes](/capabilities/full_text_search/how_to/configure_displayed_attributes).

<CodeGroup>
  ```bash theme={null}
  curl \
    -X POST 'MEILISEARCH_URL/indexes/movies/search' \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer MEILISEARCH_KEY' \
    --data-binary '{
      "q": "american hero",
      "attributesToHighlight": ["title", "overview"]
    }'
  ```
</CodeGroup>

The response includes a `_formatted` object in each hit. Inside `_formatted`, matched terms are wrapped in `<em>` tags by default:

<CodeGroup>
  ```json theme={null}
  {
    "hits": [
      {
        "title": "Captain America: The First Avenger",
        "overview": "An American hero rises during World War II...",
        "_formatted": {
          "title": "Captain <em>America</em>: The First Avenger",
          "overview": "An <em>American</em> <em>hero</em> rises during World War II..."
        }
      }
    ]
  }
  ```
</CodeGroup>

### Custom highlight tags

Use `highlightPreTag` and `highlightPostTag` to replace the default `<em>` tags with custom markup:

<CodeGroup>
  ```bash theme={null}
  curl \
    -X POST 'MEILISEARCH_URL/indexes/movies/search' \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer MEILISEARCH_KEY' \
    --data-binary '{
      "q": "american hero",
      "attributesToHighlight": ["title", "overview"],
      "highlightPreTag": "<mark>",
      "highlightPostTag": "</mark>"
    }'
  ```
</CodeGroup>

## Cropping long text fields

Use `attributesToCrop` to trim long text fields so only the portion around matched terms is returned. This is especially useful for fields like descriptions or article bodies.

<CodeGroup>
  ```bash theme={null}
  curl \
    -X POST 'MEILISEARCH_URL/indexes/movies/search' \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer MEILISEARCH_KEY' \
    --data-binary '{
      "q": "romance",
      "attributesToCrop": ["overview"],
      "cropLength": 20
    }'
  ```
</CodeGroup>

The `_formatted` object contains the cropped text:

<CodeGroup>
  ```json theme={null}
  {
    "_formatted": {
      "overview": "...a sweeping romance set in the heart of..."
    }
  }
  ```
</CodeGroup>

### Crop parameters

| Parameter          | Default | Description                                                                   |
| ------------------ | ------- | ----------------------------------------------------------------------------- |
| `attributesToCrop` | `null`  | Array of attributes to crop. Use `["*"]` for all displayed attributes.        |
| `cropLength`       | `10`    | Maximum number of words in the cropped text.                                  |
| `cropMarker`       | `"..."` | String placed at the beginning or end of cropped text to indicate truncation. |

### Custom crop length per attribute

You can set a specific crop length for individual attributes by appending `:length` to the attribute name:

<CodeGroup>
  ```bash theme={null}
  curl \
    -X POST 'MEILISEARCH_URL/indexes/movies/search' \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer MEILISEARCH_KEY' \
    --data-binary '{
      "q": "adventure",
      "attributesToCrop": ["overview:30", "tagline:10"]
    }'
  ```
</CodeGroup>

## Combining highlighting and cropping

For the best user experience, use both features together. This gives you a short, relevant snippet with matched terms visually emphasized:

<CodeGroup>
  ```bash theme={null}
  curl \
    -X POST 'MEILISEARCH_URL/indexes/movies/search' \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer MEILISEARCH_KEY' \
    --data-binary '{
      "q": "space adventure",
      "attributesToHighlight": ["title", "overview"],
      "attributesToCrop": ["overview"],
      "cropLength": 30,
      "highlightPreTag": "<mark>",
      "highlightPostTag": "</mark>"
    }'
  ```
</CodeGroup>

The `_formatted` response combines both:

<CodeGroup>
  ```json theme={null}
  {
    "_formatted": {
      "title": "<mark>Space</mark> Odyssey",
      "overview": "...embark on a daring <mark>space</mark> <mark>adventure</mark> to save humanity from..."
    }
  }
  ```
</CodeGroup>

<Tip>
  Fields listed in `attributesToCrop` are automatically highlighted if they also appear in `attributesToHighlight` or if `attributesToHighlight` is set to `["*"]`.
</Tip>

<Info>
  For the full parameter reference, see the [search API reference](/reference/api/search/search-with-post).
</Info>
