From d9540a252275869f58cdbee18084c2a874adacc5 Mon Sep 17 00:00:00 2001 From: "H. C. Kruse" Date: Tue, 2 Jan 2024 19:21:36 +0100 Subject: [PATCH] feat: Add Fulltext indices to translation tables --- .../Rsi/CommLink/TranslateCommLinks.php | 3 +- .../Rsi/CommLink/CommLinkSearchController.php | 32 +++++----- ...2024_01_02_175450_add_fulltext_indices.php | 63 +++++++++++++++++++ lang/en.json | 2 + .../views/web/rsi/comm_links/index.blade.php | 8 ++- .../views/web/rsi/comm_links/search.blade.php | 28 ++++++++- routes/web/user.php | 2 +- 7 files changed, 116 insertions(+), 22 deletions(-) create mode 100644 database/migrations/update/2024_01_02_175450_add_fulltext_indices.php diff --git a/app/Console/Commands/Rsi/CommLink/TranslateCommLinks.php b/app/Console/Commands/Rsi/CommLink/TranslateCommLinks.php index d3716a1d..ce6bf824 100644 --- a/app/Console/Commands/Rsi/CommLink/TranslateCommLinks.php +++ b/app/Console/Commands/Rsi/CommLink/TranslateCommLinks.php @@ -43,7 +43,8 @@ public function handle(): int $this->info('Including all Comm-Links'); } - TranslateCommLinksJob::dispatch($this->filterDirectories('comm_links', $modifiedTime)->toArray());; + TranslateCommLinksJob::dispatch($this->filterDirectories('comm_links', $modifiedTime)->toArray()); + ; return CommLinkCommand::SUCCESS; } diff --git a/app/Http/Controllers/Web/Rsi/CommLink/CommLinkSearchController.php b/app/Http/Controllers/Web/Rsi/CommLink/CommLinkSearchController.php index 3102218d..8af4d0fe 100644 --- a/app/Http/Controllers/Web/Rsi/CommLink/CommLinkSearchController.php +++ b/app/Http/Controllers/Web/Rsi/CommLink/CommLinkSearchController.php @@ -9,10 +9,10 @@ use App\Http\Requests\Rsi\CommLink\ReverseImageLinkSearchRequest; use App\Http\Requests\Rsi\CommLink\ReverseImageSearchRequest; use App\Models\Rsi\CommLink\CommLink; +use App\Models\Rsi\CommLink\CommLinkTranslation; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\RedirectResponse; use Illuminate\Support\Facades\Auth; @@ -97,39 +97,35 @@ public function reverseImageSearchPost(ReverseImageSearchRequest $request) } /** - * Search for images based on text content of the comm-link + * Search for comm-links based on the relevance of the input * * @param CommLinkSearchRequest $request * * @return Application|Factory|View */ - public function imageTextSearchPost(CommLinkSearchRequest $request) + public function textSearchPost(CommLinkSearchRequest $request) { $this->middleware('auth'); $data = $request->validated(); - $data = $data['query'] ?? $data['keyword']; + $data = $data['query']; - $images = CommLink::query() - ->whereHas('translations', function (Builder $query) use ($data) { - return $query->where('translation', 'LIKE', sprintf('%%%s%%', $data)); + $links = CommLink::query() + ->whereRelation('translations', function ($query) use ($data) { + $query->whereFullText('translation', $data); }) - ->orderByDesc('created_at') - ->get() - ->flatMap(function (CommLink $commLink) { - return $commLink->images; - }) - ->unique('url'); + ->limit(50) + ->get(); - if ($images->isEmpty()) { - return $this->handleSearchResult($images, 'web.rsi.comm_links.images.index'); + if ($links->isEmpty()) { + return $this->handleSearchResult($links, 'web.rsi.comm_links.index'); } return view( - 'web.rsi.comm_links.images.index', + 'web.rsi.comm_links.index', [ - 'images' => $images, - 'keyword' => $data, + 'commLinks' => $links, + 'relevanceSorted' => true, ] ); } diff --git a/database/migrations/update/2024_01_02_175450_add_fulltext_indices.php b/database/migrations/update/2024_01_02_175450_add_fulltext_indices.php new file mode 100644 index 00000000..1cbdd521 --- /dev/null +++ b/database/migrations/update/2024_01_02_175450_add_fulltext_indices.php @@ -0,0 +1,63 @@ +index('user_id'); + }); + + Schema::table('galactapedia_article_translations', static function (Blueprint $table) { + $table->mediumText('translation')->change(); + }); + + Schema::table('comm_link_translations', static function (Blueprint $table) { + $table->mediumText('translation')->change(); + }); + + if (config('database.default') === 'mysql') { + Schema::table('galactapedia_article_translations', static function (Blueprint $table) { + $table->fullText('translation'); + }); + + Schema::table('comm_link_translations', static function (Blueprint $table) { + $table->fullText('translation'); + }); + } + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('model_changelogs', static function (Blueprint $table) { + $table->dropIndex(['user_id']); + }); + + if (config('database.default') === 'mysql') { + Schema::table('galactapedia_article_translations', static function (Blueprint $table) { + $table->dropFullText(['translation']); + }); + + Schema::table('comm_link_translations', static function (Blueprint $table) { + $table->dropFullText(['translation']); + }); + + DB::statement('ALTER TABLE galactapedia_article_translations MODIFY translation LONGBLOB;'); + DB::statement('ALTER TABLE comm_link_translations MODIFY translation LONGBLOB;'); + } else { + DB::statement('ALTER TABLE galactapedia_article_translations MODIFY translation BLOB;'); + DB::statement('ALTER TABLE comm_link_translations MODIFY translation BLOB;'); + } + } +}; diff --git a/lang/en.json b/lang/en.json index d58edb91..861ed25d 100644 --- a/lang/en.json +++ b/lang/en.json @@ -269,6 +269,7 @@ "Raumschiff": "Spaceship", "Raumschiffe": "Spaceships", "Raumstationen": "Space Stations", + "Relevanz": "Relevance", "RSI API": "RSI API", "Schiff": "Ship", @@ -301,6 +302,7 @@ "Stichwort": "Keyword", "Suche": "Search", "Suche nach Bildern": "Search for images", + "Suche nach Comm-Link Inhalt": "Search for Comm-Links by content", "Suche nach Comm-Links mit Bild": "Search for Comm-Links with image", "Suche nach Comm-Links mit Bildlink": "Search for Comm-Links with image link", "Suche nach Comm-Links mit Medienurl": "Search for Comm-Links containing a media url", diff --git a/resources/views/web/rsi/comm_links/index.blade.php b/resources/views/web/rsi/comm_links/index.blade.php index 8e46d061..b9285867 100644 --- a/resources/views/web/rsi/comm_links/index.blade.php +++ b/resources/views/web/rsi/comm_links/index.blade.php @@ -20,7 +20,13 @@ @endunless
- +
@can('web.internals.view') diff --git a/resources/views/web/rsi/comm_links/search.blade.php b/resources/views/web/rsi/comm_links/search.blade.php index d074ba58..65db612d 100644 --- a/resources/views/web/rsi/comm_links/search.blade.php +++ b/resources/views/web/rsi/comm_links/search.blade.php @@ -23,7 +23,33 @@ @include('components.errors') - + +
+ + +
+
+

+ {{ __('Suche nach Comm-Link Inhalt') }} +

+
+
+ @component('components.forms.form', [ + 'action' => route('web.rsi.comm-links.text-search.post'), + 'class' => 'd-flex h-100 flex-column', + ]) + @component('components.forms.form-group', [ + 'inputType' => 'text', + 'label' => __('Inhalt'), + 'id' => 'query', + ]) + @endcomponent + + + @endcomponent +
+
+
diff --git a/routes/web/user.php b/routes/web/user.php index 8c539a26..d815608e 100644 --- a/routes/web/user.php +++ b/routes/web/user.php @@ -231,8 +231,8 @@ static function () { Route::get('search', 'CommLinkSearchController@search')->name('search'); Route::post('reverse-image-link-search', 'CommLinkSearchController@reverseImageLinkSearchPost')->name('reverse-image-link-search.post'); Route::post('reverse-image-search', 'CommLinkSearchController@reverseImageSearchPost')->name('reverse-image-search.post'); - Route::post('image-text-search', 'CommLinkSearchController@imageTextSearchPost')->name('image-text-search.post'); + Route::post('text-search', 'CommLinkSearchController@textSearchPost')->name('text-search.post'); Route::post('search', 'CommLinkSearchController@searchByTitle')->name('search-by-title.post'); } );