Skip to content

Commit

Permalink
Merge pull request #7 from coralsio/postgresSql-support
Browse files Browse the repository at this point in the history
support postgress full text search
  • Loading branch information
saeed-corals authored Dec 12, 2023
2 parents 0b252bc + 93c97a5 commit 09ea7df
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Corals/core/Foundation/FoundationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
12 changes: 8 additions & 4 deletions Corals/core/Foundation/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)');
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Corals/core/Utility/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"code": "corals-utility",
"name": "Utility Module",
"author": "Corals",
"type": "module",
"type": "core",
"description": "Utility Package",
"namespace": "Corals\\Utility\\",
"provider": "UtilityServiceProvider",
Expand Down

0 comments on commit 09ea7df

Please sign in to comment.