# "Cropping" result string around matches?

**URL:** https://discuss.elastic.co/t/cropping-result-string-around-matches/351998
**Category:** Elasticsearch
**Created:** [January 29, 2024, 2:20pm UTC](https://discuss.elastic.co/t/cropping-result-string-around-matches/351998 "2024-01-29T14:20:01Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Seb\_Jones](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seb_jones/32/131192_2.png) [@Seb\_Jones](https://discuss.elastic.co/u/Seb_Jones)
#### Post date: [January 29, 2024, 2:20pm UTC](https://discuss.elastic.co/t/cropping-result-string-around-matches/351998/1 "2024-01-29T14:20:01Z")

</div>

Hi Folks,

Meilisearch offers a "cropping" function that trims or shortens the result string while still including the matching term.

So using an example from their docs (which this forum won't let me link to 😅), if the user searches `shifu` and the match is the following string:

> The Winter Feast is Po's favorite holiday. Every year he and his father hang decorations, cook together, and serve noodle soup to the villagers. But this year Shifu informs Po that as Dragon Warrior, it is his duty to host the formal Winter Feast at the Jade Palace. Po is caught between his obligations as the Dragon Warrior and his family traditions: between Shifu and Mr. Ping.

Then the cropped result would be:

> …this year Shifu informs Po…

Does Elasticsearch have a similar function?

---

<div class="post-metadata">

### Author: ![yago82](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yago82/32/97755_2.png) [@yago82](https://discuss.elastic.co/u/yago82)
#### Post date: [January 29, 2024, 3:24pm UTC](https://discuss.elastic.co/t/cropping-result-string-around-matches/351998/2 "2024-01-29T15:24:35Z")

</div>

> [@Seb\_Jones](#):
>
> Hi Folks,
> 
> Meilisearch offers a "cropping" function that trims or shortens the result string while still including the matching term.
> 
> So using an example from their docs (which this forum won't let me link to 😅), if the user searches `shifu` and the match is the following string:
> 
> > The Winter Feast is Po's favorite holiday. Every year he and his father hang decorations, cook together, and serve noodle soup to the villagers. But this year Shifu informs Po that as Dragon Warrior, it is his duty to host the formal Winter Feast at the Jade Palace. Po is caught between his obligations as the Dragon Warrior and his family traditions: between Shifu and Mr. Ping.
> 
> Then the cropped result would be:
> 
> > …this year Shifu informs Po…
> 
> Does Elasticsearch have a similar function?

Hi,

Elasticsearch does have a similar feature. It's called "highlighting". When you perform a search, you can ask Elasticsearch to highlight the parts of the fields in the search results that match the query.

[Highlighting | Elasticsearch Guide [8.12] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/highlighting.html)

Regards

---

<div class="post-metadata">

### Author: ![Seb\_Jones](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seb_jones/32/131192_2.png) [@Seb\_Jones](https://discuss.elastic.co/u/Seb_Jones)
#### Post date: [January 29, 2024, 4:43pm UTC](https://discuss.elastic.co/t/cropping-result-string-around-matches/351998/3 "2024-01-29T16:43:40Z")

</div>

Hi,

Thanks for your response. When I first saw that "highlighting" function I assumed is was just for inserting `<em>` tags around matched words, rather than cropping. So just to clarify, is `number_of_fragments` the main option for controlling the cropping?

Also, is it possible to get Elasticsearch to automatically add the `...` characters at the start/end of the cropped string depending on whether the match is at the edge of the original string?

Many Thanks

Seb

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [February 26, 2024, 4:44pm UTC](https://discuss.elastic.co/t/cropping-result-string-around-matches/351998/4 "2024-02-26T16:44:15Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
