From 6f9486f73b0137e3279dcdfc46386553c6d9173f Mon Sep 17 00:00:00 2001 From: Tymoteusz Motylewski Date: Fri, 18 Jun 2021 14:05:57 +0200 Subject: [PATCH] Add support for highlighting the searched query Fixes: #16 --- src/Repository/ElasticRepository.php | 9 +++++++++ templates/search/search.html.twig | 10 +++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Repository/ElasticRepository.php b/src/Repository/ElasticRepository.php index e948870..2a4c52b 100644 --- a/src/Repository/ElasticRepository.php +++ b/src/Repository/ElasticRepository.php @@ -194,6 +194,15 @@ public function findByQuery(SearchDemand $searchDemand): array ], ], ], + 'highlight' => [ + 'fields' => [ + 'snippet_content' => [ + 'fragment_size' => 400, + 'number_of_fragments' => 1 + ] + ], + 'encoder' => 'html' + ] ]; $filters = $searchDemand->getFilters(); if (!empty($filters)) { diff --git a/templates/search/search.html.twig b/templates/search/search.html.twig index eea354d..f09b448 100644 --- a/templates/search/search.html.twig +++ b/templates/search/search.html.twig @@ -56,6 +56,10 @@ display: block; } } + .summary em { + font-style: normal; + font-weight: 600; + } {% endblock %} @@ -78,7 +82,11 @@ {{ version }} {% endfor %} -

{{ hit.data.snippet_content|slice(0, 400) }}...

+ {% if hit.highlights.snippet_content[0] %} +

{{ hit.highlights.snippet_content[0] | raw }}...

+ {% else %} +

{{ hit.data.snippet_content|slice(0, 400) }}...

+ {% endif %} {% endfor %}