Feature: Adding Facets, Updating Indexers
This commit is contained in:
@@ -5,6 +5,7 @@ namespace Modules\Core\Catalog\Services;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Lunar\Models\Collection;
|
||||
use Lunar\Models\Product;
|
||||
use Lunar\Search\CollectionIndexer as BaseCollectionIndexer;
|
||||
|
||||
/**
|
||||
@@ -20,6 +21,15 @@ use Lunar\Search\CollectionIndexer as BaseCollectionIndexer;
|
||||
* - slugs (filterable) — every locale's Url::slug, so getBySlug() resolves from the
|
||||
* index directly, no database read
|
||||
* - thumbnail (display) — the collection's thumbnail image URL
|
||||
* - ancestors (display) — [{id, name}, ...] ordered root-first, so a breadcrumb can
|
||||
* render directly from a single indexed document with zero extra queries
|
||||
* - product_count (display) — how many products are in this collection or any of
|
||||
* its descendants, read from the *product* Meilisearch index at collection-index
|
||||
* time (via `collection_ids`, see Modules\Core\Catalog\Services\ProductIndexer) —
|
||||
* matches what ProductService::list(ProductFilters(collectionId: ...)) would
|
||||
* return, not just direct assignment. Reflects the product index's state as of
|
||||
* the last collection reindex, so re-run `lunar:search:index --refresh` after a
|
||||
* product reindex if this needs to be current.
|
||||
*
|
||||
* New fields aren't filterable/sortable in Meilisearch until `php artisan
|
||||
* lunar:meilisearch:setup` re-syncs index settings, and existing documents need
|
||||
@@ -49,7 +59,7 @@ class CollectionIndexer extends BaseCollectionIndexer
|
||||
|
||||
public function makeAllSearchableUsing(Builder $query): Builder
|
||||
{
|
||||
return parent::makeAllSearchableUsing($query)->with(['urls', 'media']);
|
||||
return parent::makeAllSearchableUsing($query)->with(['urls', 'media', 'ancestors']);
|
||||
}
|
||||
|
||||
public function toSearchableArray(Model $model): array
|
||||
@@ -63,6 +73,18 @@ class CollectionIndexer extends BaseCollectionIndexer
|
||||
$data['collection_group_id'] = $model->collection_group_id;
|
||||
$data['slugs'] = $model->urls->pluck('slug')->unique()->values()->all();
|
||||
$data['thumbnail'] = $model->getThumbnailImage() ?: null;
|
||||
$data['ancestors'] = $model->ancestors
|
||||
->sortBy('_lft')
|
||||
->map(fn ($ancestor) => [
|
||||
'id' => $ancestor->id,
|
||||
'name' => $ancestor->translateAttribute('name'),
|
||||
])
|
||||
->values()
|
||||
->all();
|
||||
$data['product_count'] = Product::search('')
|
||||
->options(['filter' => "collection_ids = \"{$model->id}\""])
|
||||
->paginateRaw(perPage: 1, page: 1)
|
||||
->total();
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user