From 3ae0379100940b69da32798395f117d6e51536bc Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Mon, 25 Sep 2023 17:53:42 +0200 Subject: [PATCH] feat(Solr): Highlightings keep fields name to allow to display title with haighlighted parts, not only collection_txt BREAKING CHANGE: Solr search with hightlightings will output `highlighting` object with different labelled keys (`title_txt_en`, `collection_txt_en`) according to search `locale`. --- .../SearchEngine/AbstractSearchHandler.php | 2 +- .../src/SearchEngine/SolrSearchResults.php | 20 +++++++------------ ...tDocumentTranslationIndexingSubscriber.php | 2 +- .../DefaultNodesSourcesIndexingSubscriber.php | 2 +- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/lib/RoadizCoreBundle/src/SearchEngine/AbstractSearchHandler.php b/lib/RoadizCoreBundle/src/SearchEngine/AbstractSearchHandler.php index 18f001c4..5b5d1ae8 100644 --- a/lib/RoadizCoreBundle/src/SearchEngine/AbstractSearchHandler.php +++ b/lib/RoadizCoreBundle/src/SearchEngine/AbstractSearchHandler.php @@ -98,7 +98,7 @@ protected function getHighlightingOptions(array &$args = []): array { $tmp = []; $tmp["hl"] = true; - $tmp["hl.fl"] = $this->getCollectionField($args); + $tmp["hl.fl"] = $this->getTitleField($args) . ' ' . $this->getCollectionField($args); $tmp["hl.fragsize"] = $this->getHighlightingFragmentSize(); $tmp["hl.simple.pre"] = ''; $tmp["hl.simple.post"] = ''; diff --git a/lib/RoadizCoreBundle/src/SearchEngine/SolrSearchResults.php b/lib/RoadizCoreBundle/src/SearchEngine/SolrSearchResults.php index fe74fc47..056653a6 100644 --- a/lib/RoadizCoreBundle/src/SearchEngine/SolrSearchResults.php +++ b/lib/RoadizCoreBundle/src/SearchEngine/SolrSearchResults.php @@ -104,24 +104,18 @@ function ($item) { } /** - * Merge collection_txt localized fields. + * Get highlighting for one field. + * This do not merge highlighting for all fields anymore. * * @param string $id - * @return array|array[]|mixed + * @return array */ - protected function getHighlighting(string $id): mixed + protected function getHighlighting(string $id): array { - $highlights = $this->response['highlighting'][$id]; - if (!isset($highlights['collection_txt'])) { - $collectionTxt = []; - foreach ($highlights as $field => $value) { - $collectionTxt = array_merge($collectionTxt, $value); - } - $highlights = array_merge($highlights, [ - 'collection_txt' => $collectionTxt - ]); + if (isset($this->response['highlighting'][$id]) && \is_array($this->response['highlighting'][$id])) { + return $this->response['highlighting'][$id]; } - return $highlights; + return []; } /** diff --git a/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultDocumentTranslationIndexingSubscriber.php b/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultDocumentTranslationIndexingSubscriber.php index 66de4921..c2940513 100644 --- a/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultDocumentTranslationIndexingSubscriber.php +++ b/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultDocumentTranslationIndexingSubscriber.php @@ -112,7 +112,7 @@ public function onIndexing(DocumentTranslationIndexingEvent $event): void */ $assoc['collection_txt'] = $collection; // Compile all text content into a single localized text field. - $assoc['collection_txt_' . $lang] = implode(PHP_EOL, $collection); + $assoc['collection_txt_' . $lang] = trim(implode(PHP_EOL, array_filter($collection))); $event->setAssociations($assoc); } } diff --git a/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultNodesSourcesIndexingSubscriber.php b/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultNodesSourcesIndexingSubscriber.php index e1ea15ae..825d1a79 100644 --- a/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultNodesSourcesIndexingSubscriber.php +++ b/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultNodesSourcesIndexingSubscriber.php @@ -152,7 +152,7 @@ function (Tag $tag) { */ $assoc['collection_txt'] = $collection; // Compile all text content into a single localized text field. - $assoc['collection_txt_' . $lang] = implode(PHP_EOL, $collection); + $assoc['collection_txt_' . $lang] = trim(implode(PHP_EOL, array_filter($collection))); $event->setAssociations($assoc); }