Skip to content

Commit

Permalink
feat(Solr): Highlightings keep fields name to allow to display title …
Browse files Browse the repository at this point in the history
…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`.
  • Loading branch information
ambroisemaupate committed Sep 25, 2023
1 parent 75bb3d3 commit 3ae0379
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"] = '<span class="solr-highlight">';
$tmp["hl.simple.post"] = '</span>';
Expand Down
20 changes: 7 additions & 13 deletions lib/RoadizCoreBundle/src/SearchEngine/SolrSearchResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, 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 [];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 3ae0379

Please sign in to comment.