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.
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"]
}'
The response includes a _formatted object in each hit. Inside _formatted, matched terms are wrapped in <em> tags by default:
{
"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..."
}
}
]
}
Use highlightPreTag and highlightPostTag to replace the default <em> tags with custom markup:
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>"
}'
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.
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
}'
The _formatted object contains the cropped text:
{
"_formatted": {
"overview": "...a sweeping romance set in the heart of..."
}
}
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:
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"]
}'
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:
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>"
}'
The _formatted response combines both:
{
"_formatted": {
"title": "<mark>Space</mark> Odyssey",
"overview": "...embark on a daring <mark>space</mark> <mark>adventure</mark> to save humanity from..."
}
}
Fields listed in attributesToCrop are automatically highlighted if they also appear in attributesToHighlight or if attributesToHighlight is set to ["*"].