This commit is contained in:
Jürgen Mummert
2025-12-26 21:34:31 +01:00
parent 2cef524ccf
commit b184b5f3c4
+24 -30
View File
@@ -3,6 +3,7 @@
namespace MummertMedia\ContaoMeilisearchBundle\Service; namespace MummertMedia\ContaoMeilisearchBundle\Service;
use Contao\Config; use Contao\Config;
use Contao\CoreBundle\Framework\ContaoFramework;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Meilisearch\Client; use Meilisearch\Client;
@@ -13,19 +14,27 @@ class MeilisearchIndexService
public function __construct( public function __construct(
private readonly Connection $connection, private readonly Connection $connection,
) { private readonly ContaoFramework $framework,
$host = Config::get('meilisearch_host'); ) {}
$apiKey = Config::get('meilisearch_api_write');
$this->indexName = Config::get('meilisearch_index');
$this->client = new Client($host, $apiKey);
}
/** /**
* Entry point for command & cron * Entry point for command & cron
*/ */
public function run(): void public function run(): void
{ {
// Contao vollständig initialisieren (CLI & Cron!)
$this->framework->initialize();
$host = (string) Config::get('meilisearch_host');
$apiKey = (string) Config::get('meilisearch_api_write');
$this->indexName = (string) Config::get('meilisearch_index');
if ($host === '' || $this->indexName === '') {
throw new \RuntimeException('Meilisearch is not configured in tl_settings.');
}
$this->client = new Client($host, $apiKey);
$index = $this->client->index($this->indexName); $index = $this->client->index($this->indexName);
// 1. kompletten Index löschen // 1. kompletten Index löschen
@@ -38,15 +47,9 @@ class MeilisearchIndexService
$this->indexTlSearchPdf($index); $this->indexTlSearchPdf($index);
} }
/**
* Indexiert Seiten, Events und News aus tl_search
*/
private function indexTlSearch($index): void private function indexTlSearch($index): void
{ {
$rows = $this->connection->fetchAllAssociative( $rows = $this->connection->fetchAllAssociative('SELECT * FROM tl_search');
'SELECT * FROM tl_search'
);
if (!$rows) { if (!$rows) {
return; return;
} }
@@ -71,15 +74,9 @@ class MeilisearchIndexService
$index->addDocuments($documents); $index->addDocuments($documents);
} }
/**
* Indexiert PDFs aus tl_search_pdf
*/
private function indexTlSearchPdf($index): void private function indexTlSearchPdf($index): void
{ {
$rows = $this->connection->fetchAllAssociative( $rows = $this->connection->fetchAllAssociative('SELECT * FROM tl_search_pdf');
'SELECT * FROM tl_search_pdf'
);
if (!$rows) { if (!$rows) {
return; return;
} }
@@ -87,7 +84,9 @@ class MeilisearchIndexService
$documents = []; $documents = [];
foreach ($rows as $row) { foreach ($rows as $row) {
$fileType = $row['type'] ?: 'pdf'; $fileType = in_array($row['type'], ['pdf','docx','xlsx','pptx'], true)
? $row['type']
: 'pdf';
$documents[] = [ $documents[] = [
'id' => $fileType . '_' . $row['id'], 'id' => $fileType . '_' . $row['id'],
@@ -104,11 +103,6 @@ class MeilisearchIndexService
$index->addDocuments($documents); $index->addDocuments($documents);
} }
/**
* Robuste Typ-Erkennung ausschließlich über tl_search.meta
*
* @return page|event|news
*/
private function detectTypeFromMeta(?string $meta): string private function detectTypeFromMeta(?string $meta): string
{ {
if (!$meta) { if (!$meta) {
@@ -125,11 +119,11 @@ class MeilisearchIndexService
continue; continue;
} }
switch ($entry['@type']) { if ($entry['@type'] === 'https://schema.org/Event') {
case 'https://schema.org/Event':
return 'event'; return 'event';
}
case 'https://schema.org/NewsArticle': if ($entry['@type'] === 'https://schema.org/NewsArticle') {
return 'news'; return 'news';
} }
} }