TYPO3 Solr - 按文件扩展名分面

TYPO3 Solr - faceting by file extension

TYPO3 10.4.20,Solr 10.0.5,solr_file_indexer2.3.3.

将我的网站编入索引后,我有 5 种不同的类型

pages 
tt_address 
tx_events2_domain_model_event
tx_news_domain_model_news
sys_file_metadata

我用来刻面的。

plugin.tx_solr.search {
    faceting = 1
    faceting {
        facets {
            contentType {
                label = Content Type
                field = type
            }
        }
    }
}

我的所有文件(*.pdf、*.mp3、*.mp4)的类型都是 sys_file_metadata。我也很乐意通过文件扩展名提供分面。然后用户可以很容易地找到所有的音频和视频文件。

有什么方法可以得到下面的结果?

pages 
tt_address 
tx_events2_domain_model_event
tx_news_domain_model_news
PDF
MP3
MP4

开箱即用是不可能的,因为字段 type 设置为 sys_file_metadata

然而,解决方案非常简单:您需要一个额外的类型字段

plugin.tx_solr.index.queue {
  # Set it for pages
  pages.fields {
    customtype_stringS = TEXT
    customtype_stringS.value = pages
  }

  # Set it for news, and all other records
  tx_news_domain_model_news.fields {
    customtype_stringS = TEXT
    customtype_stringS.value = tx_news_domain_model_news
  }
}

现在分面需要使用新字段:

plugin.tx_solr.search.faceting.facets.type.field = customtype_stringS

使用 EXT:solr 的钩子在您的扩展中添加元数据:

ext_localconf.php

$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments'][] = \Vendor\Theme\Hooks\Solr\RecordIndexModifier::class;

和代码

<?php

declare(strict_types=1);

namespace Vendor\Theme\Hooks\Solr;

use ApacheSolrForTypo3\Solr\IndexQueue\Item;
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerDocumentsModifier;
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;

class RecordIndexModifier implements PageIndexerDocumentsModifier
{

    /**
     * @param Item $item
     * @param int $language
     * @param Document[] $documents
     * @return Document[]|array
     */
    public function modifyDocuments(Item $item, int $language, array $documents): array
    {
        $record = $item->getRecord();
        if ($item->getType() === 'sys_file_metadata') {
            foreach ($documents as $k => $document) {
                $this->enrichMetaData($record, $document);
            }
        }

        return $documents;
    }

    protected function enrichMetaData(array $row, Document $document): void
    {
        $document->setField('customtype_stringS', $document->getFields()['fileExtension'] ?? '');
    }

}

重要:需要重新索引所有记录才能获得新字段