From 5a8158148e024de5cc399792ab89b1f75f5e6dec Mon Sep 17 00:00:00 2001 From: Holger Veltrup Date: Wed, 15 May 2024 17:12:35 +0200 Subject: [PATCH] fix: create Resource from index entry with external url --- .../Search/ExternalResourceFactory.php | 40 ++++++++++++++----- src/Service/Search/SolrSearch.php | 2 +- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/Service/Search/ExternalResourceFactory.php b/src/Service/Search/ExternalResourceFactory.php index 892b6cb..0ad9885 100644 --- a/src/Service/Search/ExternalResourceFactory.php +++ b/src/Service/Search/ExternalResourceFactory.php @@ -22,7 +22,7 @@ class ExternalResourceFactory implements ResourceFactory public function accept(Document $document, ResourceLanguage $lang): bool { $location = $this->getField($document, 'url'); - if ($location === null) { + if ($location === '') { return false; } return ( @@ -34,24 +34,46 @@ public function accept(Document $document, ResourceLanguage $lang): bool public function create(Document $document, ResourceLanguage $lang): Resource { $location = $this->getField($document, 'url'); - if ($location === null) { + if ($location === '') { throw new LogicException('document should contain an url'); } return new Resource( location: $location, - id: $this->getField($document, 'sp_id') ?? '', - name: $this->getField($document, 'title') ?? '', - objectType: 'external', + id: $this->getField($document, 'sp_id'), + name: $this->getField($document, 'title'), + objectType: $this->getField( + $document, + 'sp_objecttype', + 'external' + ), lang: ResourceLanguage::of( $this->getField($document, 'meta_content_language') ), - data: new DataBag([]), + data: new DataBag([ + 'base' => [ + 'teaser' => [ + 'text' => $this->getField($document, 'description') + ] + ] + ]), ); } - private function getField(Document $document, string $name): ?string - { - return $document->getFields()[$name] ?? null; + private function getField( + Document $document, + string $name, + string $default = '' + ): string { + $value = $document->getFields()[$name]; + if ($value === null) { + return $default; + } + + if (is_array($value)) { + return implode(' ', $value); + } + + return (string)$value; } } diff --git a/src/Service/Search/SolrSearch.php b/src/Service/Search/SolrSearch.php index 0abe651..9ec264b 100644 --- a/src/Service/Search/SolrSearch.php +++ b/src/Service/Search/SolrSearch.php @@ -113,7 +113,7 @@ private function addSortToSolrQuery( private function addRequiredFieldListToSolrQuery( SolrSelectQuery $solrQuery ): void { - $solrQuery->setFields(['url', 'title', 'sp_id']); + $solrQuery->setFields(['url', 'title', 'description', 'sp_id', 'sp_objecttype']); } private function addTextFilterToSolrQuery(