|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace App\Services\Packages; |
| 5 | + |
| 6 | +use App\Models\Package; |
| 7 | +use App\Values\Packages\PackageSearchRequest; |
| 8 | +use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
| 9 | +use Illuminate\Database\Eloquent\Builder; |
| 10 | +use Illuminate\Support\Facades\DB; |
| 11 | + |
| 12 | +/** |
| 13 | + * Searches packages using Postgres full-text search with trigram fallback. |
| 14 | + * |
| 15 | + * Search strategy with a query: try tsvector full-text search (including tag matching) |
| 16 | + * first, fall back to trigram similarity on name/slug if no FTS results. |
| 17 | + * Without a query: return all packages of the given type, newest first. |
| 18 | + */ |
| 19 | +class PackageSearchService |
| 20 | +{ |
| 21 | + /** @var list<string> Relations to eager load to avoid N+1 queries when building FAIR metadata. */ |
| 22 | + private const EAGER_LOAD = ['releases', 'authors', 'tags', 'metas']; |
| 23 | + |
| 24 | + /** |
| 25 | + * Search packages by type with optional query and version requirements. |
| 26 | + * |
| 27 | + * @return LengthAwarePaginator<int, Package> |
| 28 | + */ |
| 29 | + public function search(PackageSearchRequest $request): LengthAwarePaginator |
| 30 | + { |
| 31 | + if ($request->q === null || $request->q === '') { |
| 32 | + $query = Package::with(self::EAGER_LOAD) |
| 33 | + ->where('type', $request->type) |
| 34 | + ->orderByDesc('created_at'); |
| 35 | + |
| 36 | + $this->applyRequiresFilter($query, $request); |
| 37 | + |
| 38 | + return $query->paginate(perPage: $request->per_page, page: $request->page); |
| 39 | + } |
| 40 | + |
| 41 | + // Try full-text search first |
| 42 | + $results = $this->fullTextSearch($request); |
| 43 | + |
| 44 | + if ($results->total() > 0) { |
| 45 | + return $results; |
| 46 | + } |
| 47 | + |
| 48 | + // Fall back to trigram similarity |
| 49 | + return $this->trigramSearch($request); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Search using Postgres tsvector full-text search, ranked by ts_rank. |
| 54 | + * |
| 55 | + * Also matches packages whose tags match the query via a subquery join. |
| 56 | + * |
| 57 | + * @return LengthAwarePaginator<int, Package> |
| 58 | + */ |
| 59 | + private function fullTextSearch(PackageSearchRequest $request): LengthAwarePaginator |
| 60 | + { |
| 61 | + $tsQuery = "plainto_tsquery('english', ?)"; |
| 62 | + |
| 63 | + $query = Package::with(self::EAGER_LOAD) |
| 64 | + ->where('type', $request->type) |
| 65 | + ->where(function ($q) use ($tsQuery, $request) { |
| 66 | + $q->whereRaw("search_vector @@ {$tsQuery}", [$request->q]) |
| 67 | + ->orWhereExists(function ($sub) use ($request) { |
| 68 | + $sub->select(DB::raw(1)) |
| 69 | + ->from('package_package_tag') |
| 70 | + ->join('package_tags', 'package_tags.id', '=', 'package_package_tag.package_tag_id') |
| 71 | + ->whereColumn('package_package_tag.package_id', 'packages.id') |
| 72 | + ->whereRaw("to_tsvector('english', package_tags.name) @@ plainto_tsquery('english', ?)", [$request->q]); |
| 73 | + }); |
| 74 | + }) |
| 75 | + ->orderByRaw("ts_rank(search_vector, {$tsQuery}) DESC", [$request->q]); |
| 76 | + |
| 77 | + $this->applyRequiresFilter($query, $request); |
| 78 | + |
| 79 | + return $query->paginate(perPage: $request->per_page, page: $request->page); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Fallback search using pg_trgm trigram similarity on name and slug. |
| 84 | + * |
| 85 | + * Used when full-text search returns no results, to catch fuzzy/partial matches. |
| 86 | + * |
| 87 | + * @return LengthAwarePaginator<int, Package> |
| 88 | + */ |
| 89 | + private function trigramSearch(PackageSearchRequest $request): LengthAwarePaginator |
| 90 | + { |
| 91 | + $query = Package::with(self::EAGER_LOAD) |
| 92 | + ->where('type', $request->type) |
| 93 | + ->whereRaw('(similarity(name, ?) > 0.1 OR similarity(slug, ?) > 0.1)', [$request->q, $request->q]) |
| 94 | + ->orderByRaw('GREATEST(similarity(name, ?), similarity(slug, ?)) DESC', [$request->q, $request->q]); |
| 95 | + |
| 96 | + $this->applyRequiresFilter($query, $request); |
| 97 | + |
| 98 | + return $query->paginate(perPage: $request->per_page, page: $request->page); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Filter packages to those having at least one release compatible with the given requirements. |
| 103 | + * |
| 104 | + * Compares dotted version strings as integer arrays, e.g. ?requires[typo3]=12.4 finds |
| 105 | + * packages with a release requiring typo3 <= 12.4. Multiple requirements are ANDed together. |
| 106 | + * |
| 107 | + * @param Builder<Package> $query |
| 108 | + */ |
| 109 | + private function applyRequiresFilter(Builder $query, PackageSearchRequest $request): void |
| 110 | + { |
| 111 | + if (empty($request->requires)) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + $query->whereExists(function ($sub) use ($request) { |
| 116 | + $sub->select(DB::raw(1)) |
| 117 | + ->from('package_releases') |
| 118 | + ->whereColumn('package_releases.package_id', 'packages.id'); |
| 119 | + |
| 120 | + foreach ($request->requires as $key => $version) { |
| 121 | + $sub->whereRaw( |
| 122 | + "package_releases.requires->>? IS NOT NULL AND string_to_array(package_releases.requires->>?, '.')::int[] <= string_to_array(?, '.')::int[]", |
| 123 | + [$key, $key, $version], |
| 124 | + ); |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | +} |
0 commit comments