This commit is contained in:
Jürgen Mummert
2025-12-27 20:29:20 +01:00
parent 131e24fe2e
commit 76e081e51e
+56 -33
View File
@@ -54,19 +54,17 @@ class MeilisearchIndexService
'primaryKey' => 'id', 'primaryKey' => 'id',
]); ]);
} catch (\Throwable) { } catch (\Throwable) {
// bewusst ignorieren (Index existiert evtl. noch nicht oder Key ist bereits gesetzt) // bewusst ignorieren
} }
// ✅ INDEX-SETTINGS SICHERSTELLEN // ✅ Index-Settings sicherstellen
$this->ensureIndexSettings($index); $this->ensureIndexSettings($index);
// 1. kompletten Index löschen (Settings bleiben erhalten!) // 🔄 Index leeren (Settings bleiben erhalten)
$index->deleteAllDocuments(); $index->deleteAllDocuments();
// 2. tl_search indexieren // 📄 Inhalte indexieren
$this->indexTlSearch($index); $this->indexTlSearch($index);
// 3. tl_search_pdf indexieren
$this->indexTlSearchPdf($index); $this->indexTlSearchPdf($index);
} }
@@ -88,6 +86,36 @@ class MeilisearchIndexService
]); ]);
} }
/**
* startDate aus schema.org Event extrahieren
*/
private function extractEventStartDate(?string $meta): ?int
{
if (!$meta) {
return null;
}
$data = json_decode($meta, true);
if (!is_array($data)) {
return null;
}
foreach ($data as $entry) {
if (
($entry['@type'] ?? null) === 'https://schema.org/Event'
&& !empty($entry['startDate'])
) {
$timestamp = strtotime($entry['startDate']);
return $timestamp ?: null;
}
}
return null;
}
/**
* tl_search indexieren
*/
private function indexTlSearch(Indexes $index): void private function indexTlSearch(Indexes $index): void
{ {
$rows = $this->connection->fetchAllAssociative('SELECT * FROM tl_search'); $rows = $this->connection->fetchAllAssociative('SELECT * FROM tl_search');
@@ -103,14 +131,14 @@ class MeilisearchIndexService
foreach ($rows as $row) { foreach ($rows as $row) {
$type = $this->detectTypeFromMeta($row['meta'] ?? null); $type = $this->detectTypeFromMeta($row['meta'] ?? null);
// 🛑 VERGANGENE EVENTS FILTERN // 📅 Event-Startdatum einmal ermitteln
if ($type === 'event' && !$indexPastEvents) { $eventStart = null;
if (!empty($row['startDate'])) { if ($type === 'event') {
$eventStart = (int) $row['startDate']; $eventStart = $this->extractEventStartDate($row['meta'] ?? null);
if ($eventStart < $today) { // ⛔ Vergangene Events überspringen (wenn nicht erlaubt)
continue; // ⛔ Event überspringen if (!$indexPastEvents && $eventStart !== null && $eventStart < $today) {
} continue;
} }
} }
@@ -126,9 +154,9 @@ class MeilisearchIndexService
'priority' => (int) ($row['priority'] ?? 0), 'priority' => (int) ($row['priority'] ?? 0),
]; ];
// 📅 startDate nur für Events übernehmen // 📅 startDate nur für Events setzen
if ($type === 'event' && !empty($row['startDate'])) { if ($eventStart !== null) {
$doc['startDate'] = (int) $row['startDate']; $doc['startDate'] = $eventStart;
} }
// 🖼️ Bild aus UUID erzeugen // 🖼️ Bild aus UUID erzeugen
@@ -147,12 +175,12 @@ class MeilisearchIndexService
} }
} }
/**
* tl_search_pdf indexieren
*/
private function indexTlSearchPdf(Indexes $index): void private function indexTlSearchPdf(Indexes $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;
} }
@@ -164,25 +192,24 @@ class MeilisearchIndexService
? $row['type'] ? $row['type']
: 'pdf'; : 'pdf';
$doc = [ $documents[] = [
'id' => $fileType . '_' . $row['id'], 'id' => $fileType . '_' . $row['id'],
'type' => $fileType, 'type' => $fileType,
'title' => $row['title'], 'title' => $row['title'],
'text' => $row['text'], 'text' => $row['text'],
'url' => $row['url'], 'url' => $row['url'],
'checksum' => $row['checksum'], 'checksum' => $row['checksum'],
'poster' => self::FILETYPE_ICON_MAP[$fileType]
?? self::FILETYPE_ICON_MAP['pdf'],
]; ];
// 🖼️ Icon als Poster setzen (Bundle-Asset)
$doc['poster'] = self::FILETYPE_ICON_MAP[$fileType]
?? self::FILETYPE_ICON_MAP['pdf'];
$documents[] = $doc;
} }
$index->addDocuments($documents); $index->addDocuments($documents);
} }
/**
* Typ (page | event | news) aus meta erkennen
*/
private function detectTypeFromMeta(?string $meta): string private function detectTypeFromMeta(?string $meta): string
{ {
if (!$meta) { if (!$meta) {
@@ -195,15 +222,11 @@ class MeilisearchIndexService
} }
foreach ($data as $entry) { foreach ($data as $entry) {
if (!isset($entry['@type'])) { if (($entry['@type'] ?? null) === 'https://schema.org/Event') {
continue;
}
if ($entry['@type'] === 'https://schema.org/Event') {
return 'event'; return 'event';
} }
if ($entry['@type'] === 'https://schema.org/NewsArticle') { if (($entry['@type'] ?? null) === 'https://schema.org/NewsArticle') {
return 'news'; return 'news';
} }
} }