Showing the Paragraph of a Search result

In the example where I used it at the time, my search was limited to title and text field. And I limited highlighting to the text field.

The code snippet is a function that can go into the controller or into a plugin index.php, then has to be called on the text field:

 <?php foreach ($results as $result) : ?>
  <li>
    <h2><a href="<?= $result->url() ?>"><?= $result->title() ?></a></h2>
    <?= getResultText($result->text(), $query) ?>
  </li>
<?php endforeach ?>

If you search through all possible fields, you would have to approach this differently.