Rewrite Global search

Maybe someone will need a solution for rewrite global search. It's searches by different column (in my case PostgreSql full text search column - tsvector data type) I did it like this. Enable global search
protected static ?string $recordTitleAttribute = 'topic';
protected static ?string $recordTitleAttribute = 'topic';
Rewrite global search results (getGlobalSearchResults)
public static function getGlobalSearchResults(string $searchQuery): Collection
{
$customSearchQuery = Str::replace(' ', ':* & ', trim($searchQuery));

return RfiIncome::query()
->whereRaw("rfi_incomes_fts @@ to_tsquery('english', ?)", [$customSearchQuery.':*'])
->limit(static::getGlobalSearchResultsLimit())
->get()
->map(function (Model $record): ?GlobalSearchResult {
$url = static::getGlobalSearchResultUrl($record);

if (blank($url)) {
return null;
}

return new GlobalSearchResult(
title: static::getGlobalSearchResultTitle($record),
url: $url,
details: static::getGlobalSearchResultDetails($record),
actions: static::getGlobalSearchResultActions($record),
);
})
->filter();

}
public static function getGlobalSearchResults(string $searchQuery): Collection
{
$customSearchQuery = Str::replace(' ', ':* & ', trim($searchQuery));

return RfiIncome::query()
->whereRaw("rfi_incomes_fts @@ to_tsquery('english', ?)", [$customSearchQuery.':*'])
->limit(static::getGlobalSearchResultsLimit())
->get()
->map(function (Model $record): ?GlobalSearchResult {
$url = static::getGlobalSearchResultUrl($record);

if (blank($url)) {
return null;
}

return new GlobalSearchResult(
title: static::getGlobalSearchResultTitle($record),
url: $url,
details: static::getGlobalSearchResultDetails($record),
actions: static::getGlobalSearchResultActions($record),
);
})
->filter();

}
Maybe someone can give better solution..
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server