Skip to content

Commit

Permalink
Apply changes for Typesense
Browse files Browse the repository at this point in the history
Refactor package listing to use Typesense syntax and add typesense instructions in the readme.
  • Loading branch information
Omar Rida committed Aug 25, 2024
1 parent 895cbf3 commit 5bcd931
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
14 changes: 10 additions & 4 deletions app/Http/Livewire/PackageList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Facades\Cookie;
use Livewire\Component;
use Livewire\WithPagination;
use Typesense\Documents;

class PackageList extends Component
{
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion config/scout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
],
Expand Down
9 changes: 4 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 5bcd931

Please sign in to comment.