jikan-rest/app/Services/MongoSearchService.php
pushrbx fbc3b8277d wip - more fixes
- added user animelist/mangalist endpoints back
- fixed issues with the container image
- improved club model factory
- fixed ordering while searching when search engine is disabled (mongodb based search)
2023-05-21 11:18:02 +01:00

37 lines
1021 B
PHP

<?php
namespace App\Services;
use Jenssegers\Mongodb\Query\Builder as MongoBuilder;
final class MongoSearchService extends SearchServiceBase
{
public function search(string $searchTerms, ?string $orderByFields = null, bool $sortDirectionDescending = false): \Laravel\Scout\Builder|\Illuminate\Database\Eloquent\Builder
{
/**
* @var MongoBuilder $query
*/
$query = $this->query();
/** @noinspection PhpParamsInspection */
$builder = $query->whereRaw([
'$text' => [
'$search' => $searchTerms
],
], [
'textMatchScore' => [
'$meta' => 'textScore'
]
])->orderBy('textMatchScore', 'desc');
if ($orderByFields !== null) {
$order = explode(",", $orderByFields);
foreach ($order as $o) {
$builder = $builder->orderBy($o, $sortDirectionDescending ? 'desc' : 'asc');
}
}
return $builder;
}
}