From c58ec08749345476083501d5f2cf671e092245b7 Mon Sep 17 00:00:00 2001 From: Yamen Hadla Date: Fri, 16 Feb 2024 09:29:04 +0100 Subject: [PATCH 1/8] chore: update meilisearch version --- composer.json | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/composer.json b/composer.json index 6b943e2..03e1fe1 100644 --- a/composer.json +++ b/composer.json @@ -1,25 +1,25 @@ { - "name": "visuellverstehen/t3meilisearch", - "description": "Extension to use Meilisearch as search.", - "type": "typo3-cms-extension", - "require": { - "typo3/cms-core": "^10.4 || ^11.5 || ^12.4", - "meilisearch/meilisearch-php": "^0.24", - "spatie/pdf-to-text": "^1.52" - }, - "autoload": { - "psr-4": { - "VV\\T3meilisearch\\": "Classes/" + "name": "visuellverstehen/t3meilisearch", + "description": "Extension to use Meilisearch as search.", + "type": "typo3-cms-extension", + "require": { + "typo3/cms-core": "^10.4 || ^11.5 || ^12.4", + "meilisearch/meilisearch-php": "^1.6", + "spatie/pdf-to-text": "^1.52.1" + }, + "autoload": { + "psr-4": { + "VV\\T3meilisearch\\": "Classes/" + } + }, + "autoload-dev": { + "psr-4": { + "VV\\T3meilisearch\\Tests\\": "Tests/" + } + }, + "extra": { + "typo3/cms": { + "extension-key": "t3meilisearch" + } } - }, - "autoload-dev": { - "psr-4": { - "VV\\T3meilisearch\\Tests\\": "Tests/" - } - }, - "extra": { - "typo3/cms": { - "extension-key": "t3meilisearch" - } - } } From 66902bab2d3979b3dd325f41f3201bf22cf2acea Mon Sep 17 00:00:00 2001 From: Malte Riechmann Date: Fri, 23 Feb 2024 17:18:45 +0100 Subject: [PATCH 2/8] fix: define primary key when adding documents If we do not define the primary key when adding documents, MeiliSearch does not know which should be the primary key Find more information here: https://www.meilisearch.com/docs/learn/core_concepts/primary_key#meilisearch-guesses-your-primary-key --- Classes/Service/IndexService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Service/IndexService.php b/Classes/Service/IndexService.php index 8e5ab50..2337b5b 100644 --- a/Classes/Service/IndexService.php +++ b/Classes/Service/IndexService.php @@ -46,7 +46,7 @@ public function add(Document $document) try { if ($this->client->isHealthy()) { $index = $this->client->index($this->index); - $index->addDocuments($document->toArray()); + $index->addDocuments($document->toArray(), 'id'); } else { $this->logger->warning('MeiliSearch is not healthy.'); } From 6a0c622a6d513edc23c6a8c62106bc30965231c5 Mon Sep 17 00:00:00 2001 From: Malte Riechmann Date: Tue, 27 Feb 2024 10:48:31 +0100 Subject: [PATCH 3/8] refactor: if there is no index, no exception is thrown anymore --- Classes/Service/SearchService.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Classes/Service/SearchService.php b/Classes/Service/SearchService.php index c258c26..3780e2e 100644 --- a/Classes/Service/SearchService.php +++ b/Classes/Service/SearchService.php @@ -27,13 +27,6 @@ public function search(string $query, string $sorting = 'crdate_desc', array $ty return $result; } - // Do not try to search when caching is disabled - // We might have no index ready and an exception - // would occur - if ($GLOBALS['TSFE']->no_cache === true) { - return $result; - } - [$sortingColumn, $sortingDesc] = explode('_', $sorting); foreach ($types as $type) { From b4e8f35444d5f9f2b79c55804061d279980dd2ee Mon Sep 17 00:00:00 2001 From: Rune Piper Date: Wed, 6 Mar 2024 10:07:04 +0100 Subject: [PATCH 4/8] feat: add multi-language support --- Classes/Domain/Model/Document.php | 13 +++++++++++++ Classes/Service/IndexService.php | 3 ++- Classes/Service/SearchService.php | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Classes/Domain/Model/Document.php b/Classes/Domain/Model/Document.php index 330aac6..6f3b4af 100644 --- a/Classes/Domain/Model/Document.php +++ b/Classes/Domain/Model/Document.php @@ -20,6 +20,7 @@ class Document extends AbstractDomainObject protected string $content = ''; protected string $type = ''; protected array $_formatted = []; + protected int $languageId = 0; public function getId(): string { @@ -91,6 +92,16 @@ public function setType(string $type): void $this->type = strtolower($type); } + public function getLanguageId(): int + { + return $this->languageId; + } + + public function setLanguageId(int $languageId): void + { + $this->languageId = $languageId; + } + public function setFormatted(array $_formatted): void { if ($_formatted['content']) { @@ -127,6 +138,7 @@ public static function createFromTSFE(TypoScriptFrontendController $tsfe): Docum $document->setTitle($tsfe->page['title'] ?? ''); $document->setUrl($url); $document->setCrdate($tsfe->page['crdate']); + $document->setLanguageId(0); return $document; } @@ -141,6 +153,7 @@ public function toArray(): array 'url' => $this->url, 'content' => $this->content, 'type' => $this->type, + 'languageId' => $this->languageId, ]; } } diff --git a/Classes/Service/IndexService.php b/Classes/Service/IndexService.php index 2337b5b..ae827e2 100644 --- a/Classes/Service/IndexService.php +++ b/Classes/Service/IndexService.php @@ -33,7 +33,7 @@ public function __construct() try { if ($this->client->isHealthy()) { $this->client->index($this->index)->updateSettings([ - 'filterableAttributes' => ['rootPageId', 'type'], + 'filterableAttributes' => ['rootPageId', 'type', 'languageId'], 'sortableAttributes' => ['crdate'], ]); } @@ -106,6 +106,7 @@ public function checkForFiles(TypoScriptFrontendController $tsfe) $document->setContent($content); $document->setType('pdf'); $document->setCrdate(filemtime($absolutePath)); + $document->setLanguageId(-1); $this->add($document); } diff --git a/Classes/Service/SearchService.php b/Classes/Service/SearchService.php index 3780e2e..427ac0f 100644 --- a/Classes/Service/SearchService.php +++ b/Classes/Service/SearchService.php @@ -46,6 +46,7 @@ public function search(string $query, string $sorting = 'crdate_desc', array $ty // Filter by checking the rootPageId is in the rootline 'filter' => [ 'rootPageId = ' . $GLOBALS['TSFE']->getSite()->getRootPageId(), + 'languageId IN [-1,' . $GLOBALS['TSFE']->getLanguage()->getLanguageId() .']', $typesFilter, ], ]); From 53d42b74214e4ec4394d7a909085082bedef759f Mon Sep 17 00:00:00 2001 From: Rune Piper Date: Wed, 6 Mar 2024 10:08:47 +0100 Subject: [PATCH 5/8] chore: use correct coding style --- Classes/Service/SearchService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Service/SearchService.php b/Classes/Service/SearchService.php index 427ac0f..53128e3 100644 --- a/Classes/Service/SearchService.php +++ b/Classes/Service/SearchService.php @@ -46,7 +46,7 @@ public function search(string $query, string $sorting = 'crdate_desc', array $ty // Filter by checking the rootPageId is in the rootline 'filter' => [ 'rootPageId = ' . $GLOBALS['TSFE']->getSite()->getRootPageId(), - 'languageId IN [-1,' . $GLOBALS['TSFE']->getLanguage()->getLanguageId() .']', + 'languageId IN [-1,' . $GLOBALS['TSFE']->getLanguage()->getLanguageId() . ']', $typesFilter, ], ]); From a461a814ab2c4e426cea74498da65a49cd7a48df Mon Sep 17 00:00:00 2001 From: Rune Piper Date: Wed, 6 Mar 2024 10:43:01 +0100 Subject: [PATCH 6/8] fix: respect language when indexing documents --- Classes/Domain/Model/Document.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Domain/Model/Document.php b/Classes/Domain/Model/Document.php index 6f3b4af..d2d5971 100644 --- a/Classes/Domain/Model/Document.php +++ b/Classes/Domain/Model/Document.php @@ -138,7 +138,7 @@ public static function createFromTSFE(TypoScriptFrontendController $tsfe): Docum $document->setTitle($tsfe->page['title'] ?? ''); $document->setUrl($url); $document->setCrdate($tsfe->page['crdate']); - $document->setLanguageId(0); + $document->setLanguageId($tsfe->getLanguage()->getLanguageId() ?? 0); return $document; } From d6cbb97962820028a5c1f8283151616ab2346a8c Mon Sep 17 00:00:00 2001 From: Rune Piper Date: Thu, 7 Mar 2024 09:32:14 +0100 Subject: [PATCH 7/8] fix: multiple content sections --- Classes/Domain/Model/Document.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Domain/Model/Document.php b/Classes/Domain/Model/Document.php index d2d5971..3a98086 100644 --- a/Classes/Domain/Model/Document.php +++ b/Classes/Domain/Model/Document.php @@ -133,7 +133,7 @@ public static function createFromTSFE(TypoScriptFrontendController $tsfe): Docum $document = new Document(); $document->setId(md5($url)); $document->setRootPageId($tsfe->getSite()->getRootPageId() ?? 0); - $document->setContent($content[0] ?? ''); + $document->setContent(implode(PHP_EOL, $content ?? [])); $document->setType('page'); $document->setTitle($tsfe->page['title'] ?? ''); $document->setUrl($url); From bb376f6958d901656339f7badfdd8a3f820b4d45 Mon Sep 17 00:00:00 2001 From: Rune Piper Date: Thu, 7 Mar 2024 14:59:03 +0100 Subject: [PATCH 8/8] chore: do not index selects ,inputs and labels --- Classes/Domain/Model/Document.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Classes/Domain/Model/Document.php b/Classes/Domain/Model/Document.php index 3a98086..9fea1b1 100644 --- a/Classes/Domain/Model/Document.php +++ b/Classes/Domain/Model/Document.php @@ -118,6 +118,12 @@ public static function createFromTSFE(TypoScriptFrontendController $tsfe): Docum preg_match('/(.*?)<\/body>/s', $tsfe->content, $content); } + // Remove code that shouldn't be indexed + $content = preg_replace('//s', '', $content); + $content = preg_replace('//s', '', $content); + $content = preg_replace('//s', '', $content); + $content = preg_replace('//s', '', $content); + // Remove query and fragments from URL $uri = $tsfe->cObj->getRequest()->getUri(); $url = $uri->getScheme() . '://' . $uri->getAuthority();