From 0fbd815a8f7cc085548ab82f905faaad128e050e Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Thu, 11 May 2023 02:23:19 +0800 Subject: [PATCH] meilisearch single setting and default search mode --- .../SettingResource/Pages/EditSetting.php | 52 ++++++++++++++----- app/Models/SearchBox.php | 44 ++++++++++++++++ app/Repositories/MeiliSearchRepository.php | 17 ++---- include/constants.php | 2 +- nexus/Install/settings.default.php | 7 ++- public/torrents.php | 3 +- resources/lang/en/label.php | 13 +++-- resources/lang/en/search.php | 4 ++ resources/lang/zh_CN/label.php | 13 +++-- resources/lang/zh_CN/search.php | 4 ++ resources/lang/zh_TW/label.php | 13 +++-- resources/lang/zh_TW/search.php | 4 ++ 12 files changed, 135 insertions(+), 41 deletions(-) diff --git a/app/Filament/Resources/System/SettingResource/Pages/EditSetting.php b/app/Filament/Resources/System/SettingResource/Pages/EditSetting.php index f2407a57..5e724e9f 100644 --- a/app/Filament/Resources/System/SettingResource/Pages/EditSetting.php +++ b/app/Filament/Resources/System/SettingResource/Pages/EditSetting.php @@ -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; + } + } diff --git a/app/Models/SearchBox.php b/app/Models/SearchBox.php index 9416a871..08d0f241 100644 --- a/app/Models/SearchBox.php +++ b/app/Models/SearchBox.php @@ -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('', $key, $selected, $text); + } + return implode('', $options); + } + } diff --git a/app/Repositories/MeiliSearchRepository.php b/app/Repositories/MeiliSearchRepository.php index 1ae3da49..847e1e74 100644 --- a/app/Repositories/MeiliSearchRepository.php +++ b/app/Repositories/MeiliSearchRepository.php @@ -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); diff --git a/include/constants.php b/include/constants.php index 04ac8d4f..c7a3c879 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ 10240, 'max_uploaded_duration' => 0, ], + 'meilisearch' => [ + 'enabled' => 'no', + 'search_description' => 'no', + 'default_search_mode' => \App\Models\SearchBox::SEARCH_MODE_EXACT, + ], 'system' => [ 'change_username_min_interval_in_days' => '365', 'change_username_card_allow_characters_outside_the_alphabets' => 'no', 'maximum_number_of_medals_can_be_worn' => 3, 'cookie_valid_days' => 365, 'maximum_upload_speed' => 8000, - 'meilisearch_enabled' => 'no', - 'meilisearch_search_description' => 'no', ], ); diff --git a/public/torrents.php b/public/torrents.php index 83159278..a669de17 100644 --- a/public/torrents.php +++ b/public/torrents.php @@ -1211,8 +1211,7 @@ if ($allsec != 1 || $enablespecial != 'yes'){ //do not print searchbox if showin diff --git a/resources/lang/en/label.php b/resources/lang/en/label.php index 9de3576a..a0d805ff 100644 --- a/resources/lang/en/label.php +++ b/resources/lang/en/label.php @@ -85,6 +85,15 @@ return [ 'max_uploaded_duration' => 'Maximum upload volume multiplier effective time range', 'max_uploaded_duration_help' => 'Unit: hours. The maximum upload volume multiplier takes effect within this time range after the torrent is published, and does not take effect beyond this range. A setting of 0 is always in effect', ], + 'meilisearch' => [ + 'tab_header' => 'Meilisearch', + 'enabled' => 'Whether to enable Meilisearch', + 'enabled_help' => 'Please install and configure it and import the data before enabling it, otherwise there is no data for torrent search.', + 'search_description' => 'Meilisearch whether to search for descriptions', + 'search_description_help' => "Default: 'No'. If 'Yes', description containing keywords will also be returned, and the number of hits may be higher. Need to re-import immediately after change", + 'default_search_mode' => 'Default search mode', + 'default_search_mode_help' => "Default: 'Exact'. 'And' will be split, 'Exact' is not split", + ], 'system' => [ 'tab_header' => 'System', 'change_username_card_allow_characters_outside_the_alphabets' => 'Does the name change card allow characters other than English letters', @@ -93,10 +102,6 @@ return [ 'cookie_valid_days' => 'Cookie Valid days', 'maximum_upload_speed' => 'Maximum upload speed', 'maximum_upload_speed_help' => 'A single torrent upload speed exceeding this value is instantly disabled for the account, in Mbps. For example: 100 Mbps = 12.5 MB/s', - 'meilisearch_enabled' => 'Whether to enable Meilisearch', - 'meilisearch_enabled_help' => 'Please install and configure it and import the data before enabling it, otherwise there is no data for torrent search.', - 'meilisearch_search_description' => 'Meilisearch whether to search for descriptions', - 'meilisearch_search_description_help' => "Default: 'No'. If 'Yes', description containing keywords will also be returned, and the number of hits may be higher. Need to re-import immediately after change", ], ], 'user' => [ diff --git a/resources/lang/en/search.php b/resources/lang/en/search.php index 7eb78151..1c95acd6 100644 --- a/resources/lang/en/search.php +++ b/resources/lang/en/search.php @@ -10,4 +10,8 @@ return [ '3' => 'Uploader', '4' => 'IMDB URL' ], + 'search_modes' => [ + 'and' => 'And', + 'exact' => 'Exact', + ], ]; diff --git a/resources/lang/zh_CN/label.php b/resources/lang/zh_CN/label.php index b8f1e757..511d0558 100644 --- a/resources/lang/zh_CN/label.php +++ b/resources/lang/zh_CN/label.php @@ -85,6 +85,15 @@ return [ 'max_uploaded_duration' => '最大上传量倍数有效时间范围', 'max_uploaded_duration_help' => '单位:小时。种子发布后的这个时间范围内,最大上传量倍数生效,超过此范围不生效。设置为 0 一直生效', ], + 'meilisearch' => [ + 'tab_header' => 'Meilisearch', + 'enabled' => '是否启用 Meilisearch', + 'enabled_help' => '请先安装配置好并导入数据再启用,否则种子搜索无数据', + 'search_description' => '是否搜索描述', + 'search_description_help' => "默认:'否'。若为'是',描述中包含关键字也会返回,命中的结果可能较多。修改后需立即重新导入", + 'default_search_mode' => '默认搜索模式', + 'default_search_mode_help' => "默认:'准确'。'与'将进行分词,'准确'不分词", + ], 'system' => [ 'tab_header' => '系统', 'change_username_card_allow_characters_outside_the_alphabets' => '改名卡是否允许英文字母外的字符', @@ -93,10 +102,6 @@ return [ 'cookie_valid_days' => 'Cookie 有效天数', 'maximum_upload_speed' => '最大上传速度', 'maximum_upload_speed_help' => '单种上传速度超过此值账号即刻禁用,单位 Mbps。如:100 Mbps = 12.5 MB/s', - 'meilisearch_enabled' => '是否启用 Meilisearch', - 'meilisearch_enabled_help' => '请先安装配置好并导入数据再启用,否则种子搜索无数据', - 'meilisearch_search_description' => 'Meilisearch 是否搜索描述', - 'meilisearch_search_description_help' => "默认:'否'。若为'是',描述中包含关键字也会返回,命中的结果可能较多。修改后需立即重新导入", ], ], 'user' => [ diff --git a/resources/lang/zh_CN/search.php b/resources/lang/zh_CN/search.php index edac195d..6376780e 100644 --- a/resources/lang/zh_CN/search.php +++ b/resources/lang/zh_CN/search.php @@ -10,4 +10,8 @@ return [ '3' => '发布者', '4' => 'IMDB链接' ], + 'search_modes' => [ + 'and' => '与', + 'exact' => '准确', + ], ]; diff --git a/resources/lang/zh_TW/label.php b/resources/lang/zh_TW/label.php index bdef1bf4..8c3b7445 100644 --- a/resources/lang/zh_TW/label.php +++ b/resources/lang/zh_TW/label.php @@ -85,6 +85,15 @@ return [ 'max_uploaded_duration' => '最大上傳量倍數有效時間範圍', 'max_uploaded_duration_help' => '單位:小時。種子發布後的這個時間範圍內,最大上傳量倍數生效,超過此範圍不生效。設置為 0 一直生效', ], + 'meilisearch' => [ + 'tab_header' => 'Meilisearch', + 'enabled' => '是否啟用 Meilisearch', + 'enabled_help' => '請先安裝配置好並導入數據再啟用,否則種子搜索無數據', + 'search_description' => '是否搜索描述', + 'search_description_help' => "默認:'否'。若為'是',描述中包含關鍵字也會返回,命中的結果可能較多。修改後需立即重新導入", + 'default_search_mode' => '默認搜索模式', + 'default_search_mode_help' => "默認:'準確'。'與'將進行分詞,'準確'不分詞", + ], 'system' => [ 'tab_header' => '系統', 'change_username_card_allow_characters_outside_the_alphabets' => '改名卡是否允許英文字母外的字符', @@ -92,10 +101,6 @@ return [ 'maximum_number_of_medals_can_be_worn' => '勛章最大可佩戴數', 'cookie_valid_days' => 'Cookie 有效天數', 'maximum_upload_speed_help' => '單種上傳速度超過此值賬號即刻禁用,單位 Mbps。如:100 Mbps = 12.5 MB/s', - 'meilisearch_enabled' => '是否啟用 Meilisearch', - 'meilisearch_enabled_help' => '請先安裝配置好並導入數據再啟用,否則種子搜索無數據。', - 'meilisearch_search_description' => 'Meilisearch 是否搜索描述', - 'meilisearch_search_description_help' => "默認:'否'。若為'是',描述中包含關鍵字也會返回,命中的結果可能較多。修改後需立即重新導入", ], ], 'user' => [ diff --git a/resources/lang/zh_TW/search.php b/resources/lang/zh_TW/search.php index 0862f011..9b88adc6 100644 --- a/resources/lang/zh_TW/search.php +++ b/resources/lang/zh_TW/search.php @@ -10,4 +10,8 @@ return [ '3' => '發布者', '4' => 'IMDB鏈接' ], + 'search_modes' => [ + 'and' => '與', + 'exact' => '準確', + ], ];