Skip to content

Commit

Permalink
Merge pull request #355 from tighten/develop
Browse files Browse the repository at this point in the history
Migrate from Algolia to Typesense Cloud
  • Loading branch information
techenby authored Oct 6, 2024
2 parents add7246 + cb8dc13 commit a6a8a0f
Show file tree
Hide file tree
Showing 12 changed files with 527 additions and 55 deletions.
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
4 changes: 1 addition & 3 deletions app/Http/Controllers/Api/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ public function __invoke(Request $request)
private function searchFor($q)
{
return Cache::remember(CacheKeys::packageSearchResults($q), self::CACHE_LENGTH, function () use ($q) {
return Package::search($q, function (SearchIndex $algolia, string $query, array $options) {
return $algolia->search($query, array_merge($options, ['advancedSyntax' => true]));
})->get()->load(['tags', 'author']);
return Package::search($q)->get()->load(['tags', 'author']);
});
}
}
13 changes: 8 additions & 5 deletions app/Http/Livewire/PackageList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Livewire;

use Algolia\AlgoliaSearch\SearchIndex;
use App\CacheKeys;
use App\Package;
use App\Tag;
Expand All @@ -11,6 +10,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 +51,17 @@ 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,
'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
18 changes: 14 additions & 4 deletions app/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,27 @@ public function scopeNovaCurrent($query)
public function toSearchableArray()
{
$packageAttributes = $this->toArray();
// 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

0 comments on commit a6a8a0f

Please sign in to comment.