mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-23 19:37:23 +08:00
meilisearch single setting and default search mode
This commit is contained in:
@@ -5,8 +5,10 @@ namespace App\Filament\Resources\System\SettingResource\Pages;
|
||||
use App\Filament\OptionsTrait;
|
||||
use App\Filament\Resources\System\SettingResource;
|
||||
use App\Models\HitAndRun;
|
||||
use App\Models\SearchBox;
|
||||
use App\Models\Setting;
|
||||
use App\Models\Tag;
|
||||
use App\Repositories\MeiliSearchRepository;
|
||||
use Filament\Facades\Filament;
|
||||
use Filament\Forms\ComponentContainer;
|
||||
use Filament\Forms\Concerns\InteractsWithForms;
|
||||
@@ -115,6 +117,13 @@ class EditSetting extends Page implements Forms\Contracts\HasForms
|
||||
Forms\Components\TextInput::make('seed_box.max_uploaded_duration')->label(__('label.setting.seed_box.max_uploaded_duration'))->helperText(__('label.setting.seed_box.max_uploaded_duration_help'))->integer(),
|
||||
])->columns(2);
|
||||
|
||||
$id = "meilisearch";
|
||||
$tabs[] = Forms\Components\Tabs\Tab::make(__("label.setting.$id.tab_header"))
|
||||
->id($id)
|
||||
->schema($this->getTabMeilisearchSchema($id))
|
||||
->columns(2)
|
||||
;
|
||||
|
||||
$tabs[] = Forms\Components\Tabs\Tab::make(__('label.setting.system.tab_header'))
|
||||
->id('system')
|
||||
->schema([
|
||||
@@ -140,18 +149,6 @@ class EditSetting extends Page implements Forms\Contracts\HasForms
|
||||
->label(__('label.setting.system.maximum_upload_speed'))
|
||||
->helperText(__('label.setting.system.maximum_upload_speed_help'))
|
||||
,
|
||||
Forms\Components\Radio::make('system.meilisearch_enabled')
|
||||
->options(self::$yesOrNo)
|
||||
->inline(true)
|
||||
->label(__('label.setting.system.meilisearch_enabled'))
|
||||
->helperText(__('label.setting.system.meilisearch_enabled_help'))
|
||||
,
|
||||
Forms\Components\Radio::make('system.meilisearch_search_description')
|
||||
->options(self::$yesOrNo)
|
||||
->inline(true)
|
||||
->label(__('label.setting.system.meilisearch_search_description'))
|
||||
->helperText(__('label.setting.system.meilisearch_search_description_help'))
|
||||
,
|
||||
])->columns(2);
|
||||
|
||||
$tabs = apply_filter('nexus_setting_tabs', $tabs);
|
||||
@@ -170,4 +167,35 @@ class EditSetting extends Page implements Forms\Contracts\HasForms
|
||||
return apply_filter("hit_and_run_setting_schema", $default);
|
||||
}
|
||||
|
||||
private function getTabMeilisearchSchema($id)
|
||||
{
|
||||
$schema = [];
|
||||
|
||||
$name = "$id.enabled";
|
||||
$schema[] = Forms\Components\Radio::make($name)
|
||||
->options(self::$yesOrNo)
|
||||
->inline(true)
|
||||
->label(__('label.enabled'))
|
||||
->helperText(__("label.setting.{$name}_help"))
|
||||
;
|
||||
|
||||
$name = "$id.search_description";
|
||||
$schema[] = Forms\Components\Radio::make($name)
|
||||
->options(self::$yesOrNo)
|
||||
->inline(true)
|
||||
->label(__("label.setting.$name"))
|
||||
->helperText(__("label.setting.{$name}_help"))
|
||||
;
|
||||
|
||||
$name = "$id.default_search_mode";
|
||||
$schema[] = Forms\Components\Radio::make($name)
|
||||
->options(SearchBox::listSearchModes())
|
||||
->inline(true)
|
||||
->label(__("label.setting.$name"))
|
||||
->helperText(__("label.setting.{$name}_help"))
|
||||
;
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,6 +31,14 @@ class SearchBox extends NexusModel
|
||||
'section_name' => 'json',
|
||||
];
|
||||
|
||||
const SEARCH_MODE_AND = '0';
|
||||
const SEARCH_MODE_EXACT = '2';
|
||||
|
||||
public static array $searchModes = [
|
||||
self::SEARCH_MODE_AND => ['text' => 'and'],
|
||||
self::SEARCH_MODE_EXACT => ['text' => 'exact'],
|
||||
];
|
||||
|
||||
const EXTRA_TAXONOMY_LABELS = 'taxonomy_labels';
|
||||
const SECTION_BROWSE = 'browse';
|
||||
const SECTION_SPECIAL = 'special';
|
||||
@@ -174,6 +182,15 @@ class SearchBox extends NexusModel
|
||||
}
|
||||
}
|
||||
|
||||
public static function listSearchModes(): array
|
||||
{
|
||||
$result = [];
|
||||
foreach (self::$searchModes as $key => $value) {
|
||||
$result[$key] = nexus_trans("search.search_modes.{$value['text']}");
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function isSpecialEnabled(): bool
|
||||
{
|
||||
return Setting::get('main.spsct') == 'yes';
|
||||
@@ -230,5 +247,32 @@ class SearchBox extends NexusModel
|
||||
return $this->hasMany(Processing::class, 'mode');
|
||||
}
|
||||
|
||||
public static function getDefaultSearchMode()
|
||||
{
|
||||
$meiliConf = get_setting("meilisearch");
|
||||
if ($meiliConf['enabled'] == 'yes') {
|
||||
return $meiliConf['default_search_mode'];
|
||||
} else {
|
||||
return self::SEARCH_MODE_AND;
|
||||
}
|
||||
}
|
||||
|
||||
public static function listSelectModeOptions($selectedValue): string
|
||||
{
|
||||
$options = [];
|
||||
if (!is_numeric($selectedValue)) {
|
||||
//set default
|
||||
$selectedValue = self::getDefaultSearchMode();
|
||||
}
|
||||
foreach (self::listSearchModes() as $key => $text) {
|
||||
$selected = "";
|
||||
if ((string)$key === (string)$selectedValue) {
|
||||
$selected = " selected";
|
||||
}
|
||||
$options[] = sprintf('<option value="%s"%s>%s</option>', $key, $selected, $text);
|
||||
}
|
||||
return implode('', $options);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -21,14 +21,6 @@ class MeiliSearchRepository extends BaseRepository
|
||||
|
||||
const INDEX_NAME = 'torrents';
|
||||
|
||||
const SEARCH_MODE_AND = '0';
|
||||
const SEARCH_MODE_EXACT = '2';
|
||||
|
||||
private static array $searchModes = [
|
||||
self::SEARCH_MODE_AND => ['text' => 'and'],
|
||||
self::SEARCH_MODE_EXACT => ['text' => 'exact'],
|
||||
];
|
||||
|
||||
const SEARCH_AREA_TITLE = '0';
|
||||
const SEARCH_AREA_DESC = '1';
|
||||
const SEARCH_AREA_OWNER = '3';
|
||||
@@ -64,6 +56,7 @@ class MeiliSearchRepository extends BaseRepository
|
||||
'9' => 'owner',
|
||||
];
|
||||
|
||||
|
||||
private static array $filterableAttributes = [
|
||||
"id", "category", "source", "medium", "codec", "standard", "processing", "team", "audiocodec", "owner",
|
||||
"sp_state", "visible", "banned", "approval_status", "size", "leechers", "seeders", "times_completed", "added",
|
||||
@@ -120,7 +113,7 @@ class MeiliSearchRepository extends BaseRepository
|
||||
$swapResult = $client->swapIndexes([[self::INDEX_NAME, $indexName]]);
|
||||
$times = 0;
|
||||
while (true) {
|
||||
if ($times == 60) {
|
||||
if ($times == 600) {
|
||||
$msg = "total: $total, swap too long, times: $times, return false";
|
||||
do_log($msg);
|
||||
throw new NexusException($msg);
|
||||
@@ -468,12 +461,12 @@ class MeiliSearchRepository extends BaseRepository
|
||||
private function getQuery(array $params): string
|
||||
{
|
||||
$q = trim($params['search']);
|
||||
$searchMode = self::SEARCH_MODE_AND;
|
||||
if (isset($params['search_mode'], self::$searchModes[$params['search_mode']])) {
|
||||
$searchMode = SearchBox::getDefaultSearchMode();
|
||||
if (isset($params['search_mode'], SearchBox::$searchModes[$params['search_mode']])) {
|
||||
$searchMode = $params['search_mode'];
|
||||
}
|
||||
do_log("search mode: " . self::$searchModes[$searchMode]['text']);
|
||||
if ($searchMode == self::SEARCH_MODE_AND) {
|
||||
if ($searchMode == SearchBox::SEARCH_MODE_AND) {
|
||||
return $q;
|
||||
}
|
||||
return sprintf('"%s"', $q);
|
||||
|
||||
Reference in New Issue
Block a user