Skip to content

Commit

Permalink
feat: setting for pgsql preferred search config
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Jun 21, 2024
1 parent 10ebd4d commit ff96395
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 5 deletions.
9 changes: 8 additions & 1 deletion framework/core/js/src/admin/AdminApplication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export type Extension = {
require?: Record<string, string>;
};

export enum DatabaseDriver {
MySQL = 'MySQL',
PostgreSQL = 'PostgreSQL',
SQLite = 'SQLite',
}

export interface AdminApplicationData extends ApplicationData {
extensions: Record<string, Extension>;
settings: Record<string, string>;
Expand All @@ -50,8 +56,9 @@ export interface AdminApplicationData extends ApplicationData {
safeModeExtensions?: string[] | null;
safeModeExtensionsConfig?: string[] | null;

dbDriver: string;
dbDriver: DatabaseDriver;
dbVersion: string;
dbOptions: Record<string, string>;
phpVersion: string;
queueDriver: string;
schedulerStatus: string;
Expand Down
20 changes: 20 additions & 0 deletions framework/core/js/src/admin/components/AdvancedPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { MaintenanceMode } from '../../common/Application';
import Button from '../../common/components/Button';
import classList from '../../common/utils/classList';
import ExtensionBisect from './ExtensionBisect';
import { DatabaseDriver } from '../AdminApplication';

export default class AdvancedPage<CustomAttrs extends IPageAttrs = IPageAttrs> extends AdminPage<CustomAttrs> {
searchDriverOptions: Record<string, Record<string, string>> = {};
Expand Down Expand Up @@ -68,6 +69,10 @@ export default class AdvancedPage<CustomAttrs extends IPageAttrs = IPageAttrs> e

items.add('maintenance', this.maintenance(), 90);

if (app.data.dbDriver === DatabaseDriver.PostgreSQL) {
items.add(DatabaseDriver.PostgreSQL, this.pgsqlSettings(), 80);
}

return items;
}

Expand Down Expand Up @@ -187,4 +192,19 @@ export default class AdvancedPage<CustomAttrs extends IPageAttrs = IPageAttrs> e
</FormSection>
);
}

pgsqlSettings() {
return (
<FormSection label={DatabaseDriver.PostgreSQL}>
<Form>
{this.buildSettingComponent({
type: 'select',
setting: 'pgsql_search_configuration',
options: app.data.dbOptions.search_configurations,
label: app.translator.trans('core.admin.advanced.pgsql.search_configuration'),
})}
</Form>
</FormSection>
);
}
}
2 changes: 2 additions & 0 deletions framework/core/locale/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ core:
safe_mode_extensions: Extensions allowed to boot during safe mode
safe_mode_extensions_override_help: "This setting is overridden by the <code>safe_mode_extensions</code> key in your <code>config.php</code> file. (<b>{extensions}</b>)"
section_label: Maintenance
pgsql:
search_configuration: Search configuration to use
search:
section_label: Search Drivers
driver_heading: "Search Driver: {model}"
Expand Down
1 change: 1 addition & 0 deletions framework/core/src/Admin/Content/AdminPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function __invoke(Document $document, Request $request): void
$document->payload['phpVersion'] = $this->appInfo->identifyPHPVersion();
$document->payload['dbDriver'] = $this->appInfo->identifyDatabaseDriver();
$document->payload['dbVersion'] = $this->appInfo->identifyDatabaseVersion();
$document->payload['dbOptions'] = $this->appInfo->identifyDatabaseOptions();
$document->payload['debugEnabled'] = Arr::get($this->config, 'debug');

if ($this->appInfo->scheduledTasksRegistered()) {
Expand Down
16 changes: 12 additions & 4 deletions framework/core/src/Discussion/Search/FulltextFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Flarum\Search\AbstractFulltextFilter;
use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\SearchState;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Query\Expression;
Expand All @@ -24,6 +25,11 @@
*/
class FulltextFilter extends AbstractFulltextFilter
{
public function __construct(
protected SettingsRepositoryInterface $settings
) {
}

public function search(SearchState $state, string $value): void
{
match ($state->getQuery()->getConnection()->getDriverName()) {
Expand Down Expand Up @@ -111,15 +117,17 @@ protected function mysql(SearchState $state, string $value): void

protected function pgsql(SearchState $state, string $value): void
{
$searchConfig = $this->settings->get('pgsql_search_configuration');

/** @var Builder $query */
$query = $state->getQuery();

$grammar = $query->getGrammar();

$matchCondition = "to_tsvector('english', ".$grammar->wrap('posts.content').") @@ plainto_tsquery('english', ?)";
$matchScore = "ts_rank(to_tsvector('english', ".$grammar->wrap('posts.content')."), plainto_tsquery('english', ?))";
$matchTitleCondition = "to_tsvector('english', ".$grammar->wrap('discussions.title').") @@ plainto_tsquery('english', ?)";
$matchTitleScore = "ts_rank(to_tsvector('english', ".$grammar->wrap('discussions.title')."), plainto_tsquery('english', ?))";
$matchCondition = "to_tsvector('$searchConfig', ".$grammar->wrap('posts.content').") @@ plainto_tsquery('$searchConfig', ?)";
$matchScore = "ts_rank(to_tsvector('$searchConfig', ".$grammar->wrap('posts.content')."), plainto_tsquery('$searchConfig', ?))";
$matchTitleCondition = "to_tsvector('$searchConfig', ".$grammar->wrap('discussions.title').") @@ plainto_tsquery('$searchConfig', ?)";
$matchTitleScore = "ts_rank(to_tsvector('$searchConfig', ".$grammar->wrap('discussions.title')."), plainto_tsquery('$searchConfig', ?))";
$mostRelevantPostId = 'CAST(SPLIT_PART(STRING_AGG(CAST('.$grammar->wrap('posts.id')." AS VARCHAR), ',' ORDER BY ".$matchScore.' DESC, '.$grammar->wrap('posts.number')."), ',', 1) AS INTEGER) as most_relevant_post_id";

$discussionSubquery = Discussion::select('id')
Expand Down
14 changes: 14 additions & 0 deletions framework/core/src/Foundation/ApplicationInfoProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ public function identifyDatabaseDriver(): string
};
}

public function identifyDatabaseOptions(): array
{
if ($this->config['database.driver'] === 'pgsql') {
return [
'search_configurations' => collect($this->db->select('SELECT * FROM pg_ts_config'))
->pluck('cfgname')
->mapWithKeys(fn (string $cfgname) => [$cfgname => $cfgname])
->toArray(),
];
}

return [];
}

/**
* Reports on the session driver in use based on three scenarios:
* 1. If the configured session driver is valid and in use, it will be returned.
Expand Down
1 change: 1 addition & 0 deletions framework/core/src/Settings/SettingsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function register(): void
'search_driver_Flarum\Group\Group' => 'default',
'search_driver_Flarum\Post\Post' => 'default',
'search_driver_Flarum\Http\AccessToken' => 'default',
'pgsql_search_configuration' => 'english',
]);
});

Expand Down

0 comments on commit ff96395

Please sign in to comment.