Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Algolia with Typesense #353

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
GITHUB_CALLBACK_URL=http://novapackages.test/login/github/callback

SCOUT_DRIVER=algolia
ALGOLIA_APP_ID=
ALGOLIA_SECRET=
SCOUT_DRIVER=typesense
SCOUT_PREFIX=
TYPESENSE_API_KEY=
TYPESENSE_HOST=
TYPESENSE_PORT=
TYPESENSE_PROTOCOL=

HONEYBADGER_API_KEY=

Expand Down
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
14 changes: 13 additions & 1 deletion app/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,26 @@ public function toSearchableArray()
// Temporarily truncate to prevent algolia from throwing a size exceeded exception
$packageAttributes['readme'] = substr($packageAttributes['readme'], 0, 500);
$packageAttributes['instructions'] = substr($packageAttributes['instructions'], 0, 500);
$packageAttributes['created_at'] = $this->created_at->timestamp;

Arr::forget($packageAttributes, $this->excludeFromSearchIndex);

// Add tags so we can filter them
// @todo Make sure this is updated when tags are updated
$packageAttributes['_tags'] = $this->tags->pluck('slug')->toArray();

return $packageAttributes;
return [
'id' => (string) $packageAttributes['id'],
'name' => (string) ($packageAttributes['name'] ?? ''),
'url' => (string) ($packageAttributes['url'] ?? ''),
'instructions' => (string) ($packageAttributes['instructions'] ?? ''),
'composer_name' => (string) ($packageAttributes['composer_name'] ?? ''),
'repo_url' => (string) ($packageAttributes['repo_url'] ?? ''),
'readme' => (string) ($packageAttributes['readme'] ?? ''),
'abstract' => (string) ($packageAttributes['abstract'] ?? ''),
'_tags' => ($packageAttributes['_tags'] ?? []),
'created_at' => $packageAttributes['created_at'],
];
}

protected static function booted()
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"laravel/helpers": "^1.6",
"laravel/horizon": "^5.12",
"laravel/passport": "^11.5",
"laravel/scout": "^9.8",
"laravel/scout": "^v10.7.0",
"laravel/slack-notification-channel": "^2.5",
"laravel/socialite": "^5.6",
"laravel/telescope": "^4.12",
Expand All @@ -35,6 +35,7 @@
"spatie/laravel-sitemap": "^6.2",
"stil/gd-text": "^1.1",
"tightenco/ziggy": "^2.0",
"typesense/typesense-php": "^4.9",
"willvincent/laravel-rateable": "^3.1"
},
"require-dev": {
Expand Down
Loading
Loading