Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meilisearch VIP #765

Merged
merged 18 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,21 @@ yarn-error.log
package-lock.json
.phpstorm.meta.php

meilisearch
data.ms/instance-uid
data.ms/VERSION
.gitignore
data.ms/auth/data.mdb
.gitignore
data.ms/auth/lock.mdb
data.ms/tasks/data.mdb
data.ms/tasks/lock.mdb
meili_data/data.ms/instance-uid
.gitignore
meili_data/data.ms/VERSION
meili_data/data.ms/auth/data.mdb
.gitignore
meili_data/data.ms/tasks/data.mdb
meili_data/data.ms/auth/lock.mdb
.gitignore
meili_data/data.ms/tasks/lock.mdb
86 changes: 86 additions & 0 deletions app/Console/Commands/SetupMeilisearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace App\Console\Commands;

use App\Models\Ability;
use App\Models\Attribute;
use App\Models\Calendar;
use App\Models\Character;
use App\Models\Creature;
use App\Models\Event;
use App\Models\Family;
use App\Models\Item;
use App\Models\Journal;
use App\Models\Location;
use App\Models\Map;
use App\Models\Note;
use App\Models\Organisation;
use App\Models\Post;
use App\Models\Quest;
use App\Models\QuestElement;
use App\Models\Race;
use App\Models\Tag;
use App\Models\Timeline;
use App\Models\TimelineElement;
use Illuminate\Console\Command;
use Meilisearch\Client;

class SetupMeilisearch extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'setup:meilisearch
{--c|chunk= : The number of records to import at a time (Defaults to configuration value: `scout.chunk.searchable`)}
';


/**
* The console command description.
*
* @var string
*/
protected $description = 'Setup meilisearch';

/**
* Execute the console command.
*/
public function handle()
{
//Update Non Separator Tokens for entity mentions
$client = new Client(config('scout.meilisearch.host'), config('scout.meilisearch.key'));
$client->getKeys();
$client->index('entities')->resetSeparatorTokens();
$client->index('entities')
->updateNonSeparatorTokens([':']);
$models = [
Attribute::class,
Ability::class,
Calendar::class,
Character::class,
Creature::class,
Event::class,
Family::class,
Item::class,
Journal::class,
Location::class,
Map::class,
Note::class,
Organisation::class,
Quest::class,
Race::class,
Timeline::class,
Tag::class,
Post::class,
QuestElement::class,
TimelineElement::class,
];
foreach ($models as $model) {
$object = new $model();
$object::makeAllSearchable($this->option('chunk'));
$this->info('All [' . $model . '] records have been imported.');
}
}
}
36 changes: 36 additions & 0 deletions app/Http/Controllers/Api/v1/FullTextSearchApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Http\Controllers\Api\v1;

use App\Models\Campaign;
use App\Models\Entity;
use App\Services\Search\EntitySearchService;

class FullTextSearchApiController extends ApiController
{
protected EntitySearchService $service;

public function __construct(EntitySearchService $service)
{
$this->service = $service;
}

/**
* return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function index(Campaign $campaign)
{
$this->authorize('access', $campaign);
$term = request()->term;
$entity = Entity::where(['name' => request()->term, 'campaign_id' => $campaign->id])->first();
if ($entity) {
$term2 = $entity->type() . ':' . $entity->id;
}

$results = $this->service
->campaign($campaign)
->search($term, $term2);
return $results;
}
}
39 changes: 39 additions & 0 deletions app/Models/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;
use Laravel\Scout\Searchable;

/**
* Class Attribute
Expand All @@ -37,6 +38,7 @@ class Attribute extends Model
use Paginatable;
use Pinnable;
use Privatable;
use Searchable;

public const TYPE_CHECKBOX = 'checkbox';
public const TYPE_SECTION = 'section';
Expand Down Expand Up @@ -379,4 +381,41 @@ public function exportFields(): array
'is_hidden',
];
}

/**
* Get the value used to index the model.
*
*/
public function getScoutKey()
{
return $this->getTable() . '_' . $this->id;
}

/**
* Get the name of the index associated with the model.
*/
public function searchableAs(): string
{
return 'entities';
}

protected function makeAllSearchableUsing($query)
{
return $query
->select([$this->getTable() . '.*', 'entities.id as entity_id'])
->leftJoin('entities', $this->getTable() . '.entity_id', '=', 'entities.id')
->has('entity')
->with('entity');
}

public function toSearchableArray()
{
return [
'campaign_id' => $this->entity->campaign_id,
'entity_id' => $this->entity_id,
'name' => $this->name,
'type' => 'attribute',
'entry' => $this->value,
];
}
}
9 changes: 9 additions & 0 deletions app/Models/AttributeTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,13 @@ public function datagridSortableColumns(): array
}
return $columns;
}

public function toSearchableArray()
{
return [
'campaign_id' => $this->entity->campaign_id,
'entity_id' => $this->entity->id,
'name' => $this->name,
];
}
}
43 changes: 43 additions & 0 deletions app/Models/MiscModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Support\Facades\DB;
use Exception;
use Illuminate\Support\Str;
use Laravel\Scout\Searchable as Scout;

/**
* Class MiscModel
Expand Down Expand Up @@ -48,12 +49,14 @@ abstract class MiscModel extends Model
use LastSync;
use Orderable;
use Paginatable;
use Scout;
use Searchable;
//Tooltip,
use Sortable;
use SourceCopiable;
use SubEntityScopes;


/** @var Entity Performance based entity */
protected Entity $cachedEntity;

Expand Down Expand Up @@ -624,4 +627,44 @@ public function datagridSortableColumns(): array
}
return $columns;
}

/**
* Get the value used to index the model.
*
*/
public function getScoutKey()
{
return $this->getTable() . '_' . $this->id;
}

/**
* Get the name of the index associated with the model.
*/
public function searchableAs(): string
{
return 'entities';
}

protected function makeAllSearchableUsing($query)
{
return $query
->select([$this->getTable() . '.*', 'entities.id as entity_id'])
->leftJoin('entities', function ($join) {
$join->on('entities.entity_id', $this->getTable() . '.id')
->where('entities.type_id', $this->entityTypeId());
})
->has('entity')
->with('entity');
}

public function toSearchableArray()
{
return [
'campaign_id' => $this->entity->campaign_id,
'entity_id' => $this->entity->id,
'name' => $this->name,
'type' => $this->type,
'entry' => $this->entry,
];
}
}
39 changes: 39 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Laravel\Scout\Searchable;

/**
* Class Attribute
Expand Down Expand Up @@ -47,6 +48,7 @@ class Post extends Model
use Blameable;
use HasFactory;
use Paginatable;
use Searchable;
use VisibilityIDTrait;

/** @var string[] */
Expand Down Expand Up @@ -188,4 +190,41 @@ public function editingUsers()
->using(EntityUser::class)
->withPivot('type_id');
}

/**
* Get the value used to index the model.
*
*/
public function getScoutKey()
{
return $this->getTable() . '_' . $this->id;
}

/**
* Get the name of the index associated with the model.
*/
public function searchableAs(): string
{
return 'entities';
}

protected function makeAllSearchableUsing($query)
{
return $query
->select([$this->getTable() . '.*', 'entities.id as entity_id'])
->leftJoin('entities', $this->getTable() . '.entity_id', '=', 'entities.id')
->has('entity')
->with('entity');
}

public function toSearchableArray()
{
return [
'campaign_id' => $this->entity->campaign_id,
'entity_id' => $this->entity_id,
'name' => $this->name,
'type' => 'post',
'entry' => $this->entry,
];
}
}
Loading