Let's say I have a collection with only one field BlogText
. When a user searches for a word and if that word is present in BlogText
, I want to:
- Retrieve only 10 words before the matched word and 10 words after the matched query, led and followed by an ellipsis.
- Also, I want to replace
Matched word
by<b>Matched word</b>
For example, if the searched query is 1500, I want to retrieve following:
... has been the industry's standard dummy text ever since the <b>1500<b>s, when an unknown printer took a galley of type and ...
given that original text in BlogText
is:
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
I know this can by done on the server as well, but I want to avoid retrieving data that I don't need(referring to 1st point).
You can return a substring of a long text using aggregation.
Assuming you need a substring around first occurrence of the matched term, and a space
is used as a word delimiter, the pipeline can be like this: