From 93c97a58da1505ac8efd235773b2f6f5c2de12cf Mon Sep 17 00:00:00 2001 From: moayad Date: Mon, 11 Dec 2023 11:09:01 +0200 Subject: [PATCH] support postgress full text search --- Corals/core/Foundation/FoundationServiceProvider.php | 2 ++ Corals/core/Foundation/Search/Search.php | 12 ++++++++---- ...018_01_02_152913_create_fulltext_search_table.php | 3 +++ Corals/core/Utility/module.json | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Corals/core/Foundation/FoundationServiceProvider.php b/Corals/core/Foundation/FoundationServiceProvider.php index f7f75a7d..3afbe21e 100644 --- a/Corals/core/Foundation/FoundationServiceProvider.php +++ b/Corals/core/Foundation/FoundationServiceProvider.php @@ -37,6 +37,7 @@ use Corals\User\Facades\Roles; use Corals\User\Facades\TwoFactorAuth; use Corals\User\UserServiceProvider; +use Corals\Utility\UtilityServiceProvider; use Hashids\Hashids; use Illuminate\Auth\Notifications\ResetPassword; use Illuminate\Auth\Passwords\PasswordBrokerManager as BasePasswordBrokerManager; @@ -174,6 +175,7 @@ public function register() $this->app->register(ElfinderServiceProvider::class); $this->app->register(ThemeServiceProvider::class); $this->app->register(BreadcrumbsServiceProvider::class); + $this->app->register(UtilityServiceProvider::class); $this->app->singleton('JavaScript', function ($app) { return new Transformer( diff --git a/Corals/core/Foundation/Search/Search.php b/Corals/core/Foundation/Search/Search.php index d8304ee6..a99d141d 100644 --- a/Corals/core/Foundation/Search/Search.php +++ b/Corals/core/Foundation/Search/Search.php @@ -55,11 +55,11 @@ public function AddSearchPart($query, $search, $class, $config = []) $isBoolean = data_get($config, 'boolean', true); - $query - ->when($isBoolean, function ($query) use ($termsBool) { + + if (config('database.default') === 'mysql') { + $query->when($isBoolean, function ($query) use ($termsBool) { $query->whereRaw('MATCH (indexed_title, indexed_content) AGAINST (? IN BOOLEAN MODE)', [$termsBool]); - }) - ->when(!$isBoolean, function ($query) use ($termsMatch) { + })->when(!$isBoolean, function ($query) use ($termsMatch) { $query-> whereRaw('MATCH (indexed_title, indexed_content) AGAINST (? IN NATURAL LANGUAGE MODE)', [$termsMatch]); })->orderByRaw( @@ -68,6 +68,10 @@ public function AddSearchPart($query, $search, $class, $config = []) ) DESC', [$termsMatch, $termsMatch] ); + } else if (config('database.default') === 'pgsql') { + $query + ->whereRaw("to_tsvector(indexed_title || ' ' || indexed_content) @@ to_tsquery(?)", [str_replace('*', ':*', $termsMatch)]); + } return $query; } diff --git a/Corals/core/Foundation/database/migrations/2018_01_02_152913_create_fulltext_search_table.php b/Corals/core/Foundation/database/migrations/2018_01_02_152913_create_fulltext_search_table.php index 32ee2020..4e1837e6 100644 --- a/Corals/core/Foundation/database/migrations/2018_01_02_152913_create_fulltext_search_table.php +++ b/Corals/core/Foundation/database/migrations/2018_01_02_152913_create_fulltext_search_table.php @@ -33,6 +33,9 @@ public function up() if (\Illuminate\Support\Facades\DB::connection()->getConfig()['name'] == 'mysql') { \DB::statement('ALTER TABLE fulltext_search ADD FULLTEXT fulltext_title(indexed_title)'); \DB::statement('ALTER TABLE fulltext_search ADD FULLTEXT fulltext_title_content(indexed_title, indexed_content)'); + } else if (\Illuminate\Support\Facades\DB::connection()->getConfig()['name'] == 'pgsql') { + \DB::statement('ALTER TABLE fulltext_search ADD COLUMN tsvector_content tsvector GENERATED ALWAYS AS (to_tsvector(\'simple\', COALESCE(indexed_title, \'\') || \' \' || COALESCE(indexed_content, \'\'))) STORED'); + \DB::statement('CREATE INDEX tsvector_content_gin_index ON fulltext_search USING gin(tsvector_content)'); } } } diff --git a/Corals/core/Utility/module.json b/Corals/core/Utility/module.json index 8a5ae0cd..cca2b12d 100644 --- a/Corals/core/Utility/module.json +++ b/Corals/core/Utility/module.json @@ -2,7 +2,7 @@ "code": "corals-utility", "name": "Utility Module", "author": "Corals", - "type": "module", + "type": "core", "description": "Utility Package", "namespace": "Corals\\Utility\\", "provider": "UtilityServiceProvider",