Skip to content

Commit

Permalink
feat: Add Fulltext indices to translation tables
Browse files Browse the repository at this point in the history
  • Loading branch information
octfx committed Jan 2, 2024
1 parent 764230e commit d9540a2
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 22 deletions.
3 changes: 2 additions & 1 deletion app/Console/Commands/Rsi/CommLink/TranslateCommLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
32 changes: 14 additions & 18 deletions app/Http/Controllers/Web/Rsi/CommLink/CommLinkSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
]
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('model_changelogs', static function (Blueprint $table) {
$table->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;');
}
}
};
2 changes: 2 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
"Raumschiff": "Spaceship",
"Raumschiffe": "Spaceships",
"Raumstationen": "Space Stations",
"Relevanz": "Relevance",
"RSI API": "RSI API",

"Schiff": "Ship",
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 7 additions & 1 deletion resources/views/web/rsi/comm_links/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
@endunless
</div>
<div class="card-body px-0 table-responsive">
<table class="table table-striped mb-0" data-order='[[ 0, "desc" ]]' data-page-length="50" data-length-menu='[ [25, 50, 100, -1], [25, 50, 100, "@lang('Alle')"] ]'>
<table class="table table-striped mb-0"
@unless(isset($relevanceSorted))
data-order='[[ 0, "desc" ]]'
@else
data-order=''
@endif
data-page-length="50" data-length-menu='[ [25, 50, 100, -1], [25, 50, 100, "@lang('Alle')"] ]'>
<thead>
<tr>
@can('web.internals.view')
Expand Down
28 changes: 27 additions & 1 deletion resources/views/web/rsi/comm_links/search.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,33 @@

@include('components.errors')

<div id="cl-live-search"><comm-link-live-search api-token="{{ $apiToken }}"></comm-link-live-search></div>

<div class="card-deck">
<div id="cl-live-search"><comm-link-live-search api-token="{{ $apiToken }}"></comm-link-live-search></div>

<div class="card mb-3">
<div class="card-header">
<h4>
{{ __('Suche nach Comm-Link Inhalt') }}
</h4>
</div>
<div class="card-body">
@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

<button class="btn btn-block btn-outline-secondary mt-auto">@lang('Suche')</button>
@endcomponent
</div>
</div>
</div>

<div class="card-deck">
<div class="card mb-3">
Expand Down
2 changes: 1 addition & 1 deletion routes/web/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
);
Expand Down

0 comments on commit d9540a2

Please sign in to comment.