From 4db61a30c162d914a70eb1bcb81665a27065e87a Mon Sep 17 00:00:00 2001 From: thomas-sc Date: Wed, 8 Jan 2025 16:51:57 +0100 Subject: [PATCH] optimize mappping and handling of nested fields --- Classes/Processing/BibElasticMapping.php | 12 +++--- Classes/Processing/BibEntryProcessor.php | 50 ++++++++++++++++++++++- Configuration/TypoScript/setup.typoscript | 2 +- 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/Classes/Processing/BibElasticMapping.php b/Classes/Processing/BibElasticMapping.php index 4c74328..faf2605 100644 --- a/Classes/Processing/BibElasticMapping.php +++ b/Classes/Processing/BibElasticMapping.php @@ -53,12 +53,12 @@ public static function getMappingParams(string $index): array ] ], 'fulltext' => [ 'type' => 'text' ], - 'tx_lisztcommon_header' => [ 'type' => 'text' ], - 'tx_lisztcommon_body' => [ 'type' => 'text' ], - 'tx_lisztcommon_footer' => [ 'type' => 'text' ], - 'tx_lisztcommon_searchable' => ['type' => 'text', 'copy_to' => 'fulltext'], - 'tx_lisztcommon_boosted' => ['type' => 'text'], - 'tx_lisztcommon_creators' => [ + BibEntryProcessor::HEADER_FIELD => [ 'type' => 'text' ], + BibEntryProcessor::BODY_FIELD => [ 'type' => 'text' ], + BibEntryProcessor::FOOTER_FIELD => [ 'type' => 'text' ], + BibEntryProcessor::SEARCHABLE_FIELD => ['type' => 'text', 'copy_to' => 'fulltext'], + BibEntryProcessor::BOOSTED_FIELD => ['type' => 'text'], + BibEntryProcessor::CREATORS_FIELD => [ 'type' => 'nested', 'properties' => [ 'fullName' => [ diff --git a/Classes/Processing/BibEntryProcessor.php b/Classes/Processing/BibEntryProcessor.php index c11a9ec..9d4c026 100644 --- a/Classes/Processing/BibEntryProcessor.php +++ b/Classes/Processing/BibEntryProcessor.php @@ -57,7 +57,7 @@ public static function process( $bibliographyItem[self::SEARCHABLE_FIELD] = self::buildListingField($bibliographyItem, BibEntryConfig::SEARCHABLE_FIELDS); $bibliographyItem[self::BOOSTED_FIELD] = self::buildListingField($bibliographyItem, BibEntryConfig::BOOSTED_FIELDS); - $bibliographyItem[self::CREATORS_FIELD] = self::buildListingField($bibliographyItem, BibEntryConfig::CREATORS_FIELD); + $bibliographyItem[self::CREATORS_FIELD] = self::buildNestedField($bibliographyItem, BibEntryConfig::CREATORS_FIELD); return $bibliographyItem; } @@ -70,6 +70,7 @@ public static function buildListingField( $collectedFields = Collection::wrap($fieldConfig)-> map( function($field) use ($bibliographyItem) { return self::buildListingEntry($field, $bibliographyItem); }); if (is_array($collectedFields->get(0))) { + print_r($collectedFields->get(0)); return $collectedFields->get(0); } return $collectedFields-> @@ -77,6 +78,53 @@ public static function buildListingField( trim(); } +/* + * @Matthias: this is a new function for nested fields with an array of object, + * ToDo: optimize and remove hard coded 'fullname' for tx_lisztbibliography_creators + * maybe ist much easier with an own BibEntryConfig +*/ + public static function buildNestedField( + array $bibliographyItem, + array $fieldConfig + ): Stringable|array + { + $collectedFields = Collection::wrap($fieldConfig) + ->map(function ($field) use ($bibliographyItem) { + return self::buildListingEntry($field, $bibliographyItem); + }); + + $result = $collectedFields->flatMap(function ($item) { + if (is_array($item)) { + return array_map(function ($i) { + // convert stringable to string if needed + if ($i instanceof Illuminate\Support\Stringable) { + $i = (string)$i; + } + return ['fullName' => $i]; + }, $item); + } + + // return fullName keys, ToDo: hardcoded key here + if ($item instanceof Illuminate\Support\Stringable) { + return [['fullName' => (string)$item]]; + } + + throw new \UnexpectedValueException('Unexpected type: ' . gettype($item)); + })->toArray(); + + return $result; + /*Array + returns: + ( + [0] => Array + ([fullName] => Illuminate\Support\Stringable Object ([value:protected] => Michael Saffle)) + + [1] => Array + ([fullName] => Illuminate\Support\Stringable Object([value:protected] => Michael Saffle) + ) + */ + } + private static function buildListingEntry(array $field, array $bibliographyItem): Stringable|array|null { // return empty string if field does not exist diff --git a/Configuration/TypoScript/setup.typoscript b/Configuration/TypoScript/setup.typoscript index ed06157..54f0f96 100644 --- a/Configuration/TypoScript/setup.typoscript +++ b/Configuration/TypoScript/setup.typoscript @@ -41,7 +41,7 @@ plugin.tx_lisztcommon_searchlisting { type = keyword } 1 { - field = tx_lisztcommon_creators + field = tx_lisztbibliography_creators type = nested key = fullName }