Skip to content

Commit

Permalink
Merge pull request #213 from SlovakNationalGallery/KOMARCH-289-index-…
Browse files Browse the repository at this point in the history
…documents

make documents searchable
  • Loading branch information
igor-kamil authored Oct 2, 2024
2 parents b0fe398 + aa35d05 commit 660219c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 19 deletions.
2 changes: 2 additions & 0 deletions app/Console/Commands/ImportSearchableEntitiesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Post;
use App\Models\Work;
use App\Models\Contest;
use App\Models\Document;
use App\Models\Architect;
use Illuminate\Console\Command;

Expand All @@ -15,6 +16,7 @@ class ImportSearchableEntitiesCommand extends Command
Architect::class,
Work::class,
Contest::class,
Document::class,
];

protected $signature = 'komarch:search:import';
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function index(Request $request)

// search
if ($request->filled('q')) {
$documents->where('name', 'like', '%' . $request->input('q') . '%');
$documents->whereIn('id', Document::search($request->query('q'))->keys());
}

// sort
Expand Down
42 changes: 28 additions & 14 deletions app/Http/Controllers/Api/SearchSuggestionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Architect;
use App\Models\Contest;
use App\Models\Page;
use App\Models\Post;
use App\Models\Work;
use Illuminate\Http\Request;
use App\Models\Contest;
use App\Models\Document;
use App\Models\Architect;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\DocumentResource;

class SearchSuggestionController extends Controller
{
Expand All @@ -30,42 +32,43 @@ public function index(Request $request)
'contests' => $this->contests($search),
'posts' => $this->posts($search),
'pages' => $this->pages($search),
'documents' => $this->documents($search),
];
}

private function architects(string $search)
{
$architects = Architect::search("*{$search}*")->take($this->limit)->get();
$architects = Architect::search($search)->take($this->limit)->get();
return $architects->map(function ($architect) {
return [
'id' => $architect->id,
'url' => $architect->url,
'title' => $architect->full_name,
'id' => $architect->id,
'url' => $architect->url,
'title' => $architect->full_name,
];
});
}

private function works(string $search)
{
$works = Work::search("*{$search}*")->take($this->limit)->get();
$works = Work::search($search)->take($this->limit)->get();
return $works->map(function ($work) {
return [
'id' => $work->id,
'url' => $work->url,
'title' => $work->name,
'id' => $work->id,
'url' => $work->url,
'title' => $work->name,
];
});
}

private function contests(string $search)
{
$contests = Contest::search("*{$search}*")->take($this->limit)->get();
$contests = Contest::search($search)->take($this->limit)->get();
return $contests->map->only('id', 'url', 'title');
}

private function posts(string $search)
{
$posts = Post::search("*{$search}*")->take($this->limit)->get();
$posts = Post::search($search)->take($this->limit)->get();
return $posts->map->only('id', 'url', 'title');
}

Expand All @@ -75,4 +78,15 @@ private function pages(string $search)
return $pages->map->only('id', 'url', 'title');
}

private function documents(string $search)
{
$documents = Document::search($search)->take($this->limit)->get();
return $documents->map(function ($document) {
return [
'id' => $document->id,
'url' => $document->url,
'title' => $document->name
];
});
}
}
20 changes: 16 additions & 4 deletions app/Models/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
use Spatie\Tags\HasTags;
use Spatie\Tags\Tag;
use App\Traits\CreatedBy;
use Laravel\Scout\Searchable;

class Document extends Model implements HasMedia
{
use HasTags,
InteractsWithMedia,
CrudTrait,
Searchable,
CreatedBy;

/*
Expand Down Expand Up @@ -56,7 +58,7 @@ public function registerMediaCollections(): void
public function registerMediaConversions(Media $media = null): void
{
$this->addMediaConversion('thumb')
->width(800)->performOnCollections('file');
->width(800)->performOnCollections('file');
}

/*
Expand Down Expand Up @@ -98,6 +100,11 @@ public function getFileAttribute()
return $this->getFirstMedia('file');
}

public function getUrlAttribute(): string
{
return asset($this->file->getUrl());
}


/*
|--------------------------------------------------------------------------
Expand All @@ -109,14 +116,19 @@ public function setFileAttribute($uploaded_file)
{

$this->clearMediaCollection('file');
if (isSet($uploaded_file)) {
if (isset($uploaded_file)) {
$this
->addMedia($uploaded_file)
// ->usingFileName($uploaded_file->hashName())
->toMediaCollection('file');
}
}



public function toSearchableArray()
{
return [
'id' => $this->id,
'name' => $this->name,
];
}
}
1 change: 1 addition & 0 deletions resources/lang/en/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
'contests' => 'Contests',
'posts' => 'Posts',
'pages' => 'Pages',
'documents' => 'Documents',
];
1 change: 1 addition & 0 deletions resources/lang/sk/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
'contests' => 'Súťaže',
'posts' => 'Informácie SKA',
'pages' => 'Stránky',
'documents' => 'Dokumenty',
];

0 comments on commit 660219c

Please sign in to comment.