Skip to main content
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.
The response includes a _formatted object in each hit. Inside _formatted, matched terms are wrapped in <em> tags by default:

Custom highlight tags

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

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.
The _formatted object contains the cropped text:

Crop parameters

Custom crop length per attribute

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

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:
The _formatted response combines both:
Fields listed in attributesToCrop are automatically highlighted if they also appear in attributesToHighlight or if attributesToHighlight is set to ["*"].
For the full parameter reference, see the search API reference.