songRepository), self::excerptScoutSearch($keywords, $count, $this->artistRepository), self::excerptScoutSearch($keywords, $count, $this->albumRepository), self::excerptScoutSearch($keywords, $count, $this->podcastRepository), ); } /** * @param SongRepository|AlbumRepository|ArtistRepository|PodcastRepository $repository * * @return Collection|array */ private static function excerptScoutSearch(string $keywords, int $count, Repository $repository): Collection { try { return $repository->getMany( ids: $repository->model::search($keywords)->get()->take($count)->pluck('id')->all(), // @phpstan-ignore-line preserveOrder: true, ); } catch (Throwable $e) { Log::error('Scout search failed', ['exception' => $e]); return new Collection(); } } /** @return Collection|array */ public function searchSongs( string $keywords, ?User $scopedUser = null, int $limit = self::DEFAULT_MAX_SONG_RESULT_COUNT ): Collection { return Song::search($keywords) ->query( static fn (SongBuilder $builder) => $builder ->forUser($scopedUser ?? auth()->user()) ->withMeta() ->limit($limit) ) ->get(); } }