This commit is contained in:
Jürgen Mummert
2025-12-27 18:18:26 +01:00
parent 0366995021
commit cfffdf2323
3 changed files with 33 additions and 59 deletions
@@ -14,6 +14,7 @@ Contao 5 Frontend Module Template
placeholder="Suche …" placeholder="Suche …"
autocomplete="off" autocomplete="off"
> >
<button <button
type="button" type="button"
class="meilisearch-clear" class="meilisearch-clear"
@@ -75,16 +76,21 @@ Contao 5 Frontend Module Template
try { try {
const response = await index.search(query, { const response = await index.search(query, {
limit: limit, limit: limit,
attributesToRetrieve: [ attributesToRetrieve: [
'title', 'title',
'url', 'url',
'text', 'text',
'poster', 'poster',
'type' 'priority'
], ],
attributesToHighlight: ['text'], attributesToHighlight: ['text'],
highlightPreTag: '<mark>', attributesToCrop: ['text'],
highlightPostTag: '</mark>' cropLength: 50,
cropMarker: '…',
sort: ['priority:desc']
}); });
renderResults(response.hits); renderResults(response.hits);
@@ -107,7 +113,7 @@ Contao 5 Frontend Module Template
for (const hit of hits) { for (const hit of hits) {
const article = document.createElement('article'); const article = document.createElement('article');
article.className = 'meilisearch-result type-' + (hit.type || 'unknown'); article.className = 'meilisearch-result type-' + (hit.type || 'default');
const link = document.createElement('a'); const link = document.createElement('a');
link.href = hit.url || '#'; link.href = hit.url || '#';
-53
View File
@@ -1,53 +0,0 @@
.meilisearch-search {
position: relative;
}
.meilisearch-search-field {
position: relative;
}
.meilisearch-search input[type="search"] {
width: 100%;
max-width: 600px;
padding: 0.75rem 2.5rem 0.75rem 0.75rem;
font-size: 1rem;
}
.meilisearch-clear {
position: absolute;
right: 0.5rem;
top: 50%;
transform: translateY(-50%);
background: none;
border: 0;
font-size: 1.25rem;
cursor: pointer;
}
.meilisearch-results {
margin-top: 0.5rem;
border: 1px solid #e0e0e0;
background: #fff;
}
.meilisearch-result {
display: grid;
grid-template-columns: 60px auto;
gap: 1rem;
padding: 1rem;
border-bottom: 1px solid #eee;
}
.meilisearch-result:hover {
background: #f5f5f5;
}
.meilisearch-result img {
width: 100%;
aspect-ratio: 1 / 1;
object-fit: cover;
}
.meilisearch-extract mark {
background: #ddd;
}
+23 -2
View File
@@ -6,6 +6,7 @@ use Contao\Config;
use Contao\CoreBundle\Framework\ContaoFramework; use Contao\CoreBundle\Framework\ContaoFramework;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Meilisearch\Client; use Meilisearch\Client;
use Meilisearch\Endpoints\Indexes;
class MeilisearchIndexService class MeilisearchIndexService
{ {
@@ -56,6 +57,9 @@ class MeilisearchIndexService
// bewusst ignorieren (Index existiert evtl. noch nicht oder Key ist bereits gesetzt) // bewusst ignorieren (Index existiert evtl. noch nicht oder Key ist bereits gesetzt)
} }
// ✅ INDEX-SETTINGS SICHERSTELLEN
$this->ensureIndexSettings($index);
// 1. kompletten Index löschen (Settings bleiben erhalten!) // 1. kompletten Index löschen (Settings bleiben erhalten!)
$index->deleteAllDocuments(); $index->deleteAllDocuments();
@@ -66,7 +70,24 @@ class MeilisearchIndexService
$this->indexTlSearchPdf($index); $this->indexTlSearchPdf($index);
} }
private function indexTlSearch($index): void /**
* Relevanz- & Sortierlogik für Meilisearch
*/
private function ensureIndexSettings(Indexes $index): void
{
$index->updateSettings([
'searchableAttributes' => [
'title',
'keywords',
'text',
],
'sortableAttributes' => [
'priority',
],
]);
}
private function indexTlSearch(Indexes $index): void
{ {
$rows = $this->connection->fetchAllAssociative('SELECT * FROM tl_search'); $rows = $this->connection->fetchAllAssociative('SELECT * FROM tl_search');
if (!$rows) { if (!$rows) {
@@ -104,7 +125,7 @@ class MeilisearchIndexService
$index->addDocuments($documents); $index->addDocuments($documents);
} }
private function indexTlSearchPdf($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'