From 5bcd931754fdd49c2ed66800b7b0fd0e5e5a657c Mon Sep 17 00:00:00 2001 From: Omar Rida Date: Sun, 25 Aug 2024 08:48:35 -0500 Subject: [PATCH] Apply changes for Typesense Refactor package listing to use Typesense syntax and add typesense instructions in the readme. --- app/Http/Livewire/PackageList.php | 14 ++++++++++---- config/scout.php | 2 +- readme.md | 9 ++++----- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/Http/Livewire/PackageList.php b/app/Http/Livewire/PackageList.php index 9b82de81..40662397 100644 --- a/app/Http/Livewire/PackageList.php +++ b/app/Http/Livewire/PackageList.php @@ -11,6 +11,7 @@ use Illuminate\Support\Facades\Cookie; use Livewire\Component; use Livewire\WithPagination; +use Typesense\Documents; class PackageList extends Component { @@ -51,14 +52,19 @@ public function renderPopularAndRecent() public function renderPackageList() { if ($this->search) { - $packages = Package::search($this->search, function (SearchIndex $algolia, string $query, array $options) { - $options['advancedSyntax'] = true; + $packages = Package::search($this->search, function (Documents $documents, string $query) { + + $searchParams = [ + 'q' => $query, + // TODO: Figure out why this isn't using the default query_by in scout.php and instead needs to be specified each time. + 'query_by' => config('scout.typesense.model-settings.' . Package::class . '.search-parameters.query_by'), + ]; if (! in_array($this->tag, ['all', 'popular', 'nova_current'])) { - $options['tagFilters'] = [$this->tag]; + $searchParams['filter_by'] = '_tags: [' . $this->tag . ']'; } - return $algolia->search($query, $options); + return $documents->search($searchParams); })->query(function (Builder $builder) { // Ensure search results use the same query scopes as non-filtered results return $builder->filter($this->tag); diff --git a/config/scout.php b/config/scout.php index 56179936..ea7b635c 100644 --- a/config/scout.php +++ b/config/scout.php @@ -144,7 +144,7 @@ ], ], 'search-parameters' => [ - 'query_by' => 'name,composer_name,instructions,readme,abstract,url,repo_url', + 'query_by' => 'name, composer_name, instructions, readme, abstract, url, repo_url', ], ], ], diff --git a/readme.md b/readme.md index 2b4add79..4a36b324 100644 --- a/readme.md +++ b/readme.md @@ -7,12 +7,11 @@ 1. `php artisan passport:keys` 2. `php artisan passport:client --personal` -## Setting up Scout +## Setting up Scout with Typesense -1. Make a new [Algolia app](https://www.algolia.com/) -2. From your Algolia app, copy the Admin API key and ID into your `.env`. If you see a "Not enough rights to update an object near line:1" error when seeding your database, you're using the Search-Only key—swap it out for the Admin key. -3. In your Algolia app, create a new index called "packages". -4. After seeding your database, run `php artisan scout:import "App\Package"` +1. Set up a Typesense instance locally or on a server. For local development, you can use https://github.com/tighten/takeout to run Typesense. +2. Update the `.env` variables for your Typesense instance. +3. After seeding your database, run `php artisan scout:import "App\Package"` ## Seeding the Database