Skip to content

Commit

Permalink
optimize mappping and handling of nested fields
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-sc committed Jan 8, 2025
1 parent b36764c commit 4db61a3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
12 changes: 6 additions & 6 deletions Classes/Processing/BibElasticMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
50 changes: 49 additions & 1 deletion Classes/Processing/BibEntryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -70,13 +70,61 @@ 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->
join('')->
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
Expand Down
2 changes: 1 addition & 1 deletion Configuration/TypoScript/setup.typoscript
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ plugin.tx_lisztcommon_searchlisting {
type = keyword
}
1 {
field = tx_lisztcommon_creators
field = tx_lisztbibliography_creators
type = nested
key = fullName
}
Expand Down

0 comments on commit 4db61a3

Please sign in to comment.