Feature: Adding Facets, Updating Indexers
This commit is contained in:
@@ -21,6 +21,8 @@ nowhere near enough for a storefront category page or a nav tree.
|
||||
| `collection_group_id` | `$model->collection_group_id` | Filterable. Mirrors `Collection::scopeInGroup()`. |
|
||||
| `slugs` | `$model->urls->pluck('slug')` | Filterable. Every locale's `Url::slug`, so `getBySlug()` resolves purely from the index. |
|
||||
| `thumbnail` | `$model->getThumbnailImage()` | Display only. `null` if the collection has no thumbnail image. |
|
||||
| `ancestors` | `$model->ancestors` | Display only. Array of `{id, name}`, ordered root-first — a breadcrumb (`Home > Apparel > Keychains`) can render directly from a single `getById()`/`getBySlug()` call, no extra queries. Empty array for a top-level collection. |
|
||||
| `product_count` | Queried from the *product* Meilisearch index at collection-index time | Display only. How many products are in this collection **or any of its descendants** — matches what `ProductService::list(ProductFilters(collectionId: ...))` would return, not just direct assignment. Computed via `Product::search('')->options(['filter' => "collection_ids = \"{id}\""])`, so it depends on the product index already being current — reindex products *before* collections (see "Gotchas" below). |
|
||||
|
||||
`name`/`description` (and any other `TranslatedText` attribute) are indexed per-locale
|
||||
by Lunar's base indexer and resolved by `CollectionService` exactly like
|
||||
@@ -87,6 +89,19 @@ lunar:meilisearch:setup` re-syncs index settings, and existing documents need
|
||||
the queue worker also needs restarting after deploying changes to the indexer class —
|
||||
see `docs/lunar.md` "Gotchas".
|
||||
|
||||
**`product_count` needs the product index reindexed first.** `config/lunar/search.php`'s
|
||||
`indexers` array is typically ordered `Collection` before `Product`, so a plain
|
||||
`lunar:search:index --refresh` computes `product_count` against whatever the product
|
||||
index held *before* this run — stale if products changed too. `lunar:search:index`
|
||||
takes an explicit model list as its argument (`--ignore` restricts it to only those),
|
||||
so reindex products first, then collections, when both need a fresh `--refresh` in the
|
||||
same deploy:
|
||||
|
||||
```
|
||||
php artisan lunar:search:index "Lunar\Models\Product" --ignore --refresh
|
||||
php artisan lunar:search:index "Lunar\Models\Collection" --ignore --refresh
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## When to still use Eloquent directly
|
||||
|
||||
+34
-2
@@ -33,9 +33,9 @@ $service = app(ProductService::class);
|
||||
// "Meilisearch driver quirk" below), so it behaves like any other Laravel paginator.
|
||||
$products = $service->list(perPage: 24, page: 1);
|
||||
|
||||
// Filter by collection, brand, and/or price range
|
||||
// Filter by collection, brand, price range, and/or stock
|
||||
$products = $service->list(
|
||||
filters: new ProductFilters(collectionId: 17, minPrice: 10.0, maxPrice: 50.0),
|
||||
filters: new ProductFilters(collectionId: 17, minPrice: 10.0, maxPrice: 50.0, inStockOnly: true),
|
||||
perPage: 24,
|
||||
page: 1,
|
||||
);
|
||||
@@ -56,10 +56,41 @@ $product = $service->getById(367); // array, or null if not found
|
||||
|
||||
// Single product, by URL slug (any locale — slugs are indexed across all languages)
|
||||
$product = $service->getBySlug('erotika-mprelok'); // array, or null if not found
|
||||
|
||||
// Facet counts for a sidebar — value => matching product count, scoped to whatever
|
||||
// $filters is passed. Does NOT exclude the faceted field itself from $filters — see
|
||||
// facets()'s docblock for why, and how to build a standard "every option's count,
|
||||
// unaffected by that option's own currently-selected value" sidebar.
|
||||
$brandCounts = $service->facets('brand', filters: new ProductFilters(collectionId: 17));
|
||||
// ['3Dealer.gr - 3D printed creations' => 48, 'Kraniou Topos - 3D printed creations' => 135]
|
||||
|
||||
// Min/max price across matching products, for sizing a price-range slider.
|
||||
// minPrice/maxPrice are ALWAYS excluded from the filter driving this (unlike
|
||||
// facets(), which doesn't auto-exclude) — the slider's own bounds shouldn't shrink
|
||||
// to whatever range is currently selected on it. Other filters (collectionId,
|
||||
// brand, inStockOnly) still apply normally.
|
||||
$range = $service->priceRange(new ProductFilters(collectionId: 17));
|
||||
// ['min' => 0.0, 'max' => 120.0]
|
||||
```
|
||||
|
||||
All `ProductFilters` fields are optional; only the ones set are added to the Meilisearch query.
|
||||
|
||||
`facets()` only makes sense on discrete-value filterable fields (`brand`, `in_stock`) — a numeric
|
||||
field like `price` would return one "facet" per exact price, not a usable range bucket. Use
|
||||
`priceRange()` for `price` instead, which reads Meilisearch's `facetStats` (min/max), a different
|
||||
feature from `facetDistribution`.
|
||||
|
||||
---
|
||||
|
||||
## Stock goes stale between orders
|
||||
|
||||
`in_stock` reflects `ProductVariant::stock`/`purchasable` as of the **last reindex**, not live
|
||||
inventory. Nothing in this codebase currently reindexes a product when an order decrements its
|
||||
stock — that's a cart/checkout concern, not something `ProductIndexer` can solve on its own (see
|
||||
`Modules\Core\Catalog\Observers\ProductOptionReindexObserver` for the equivalent pattern once an
|
||||
order → stock → reindex pipeline exists to hook into). Until then, `in_stock`/`product_count` can
|
||||
drift from the database the same way every other indexed field already can between writes.
|
||||
|
||||
---
|
||||
|
||||
## Fields this depends on: `Modules\Core\Catalog\Services\ProductIndexer`
|
||||
@@ -81,6 +112,7 @@ needs, listing and detail alike:
|
||||
| `media` | `$product->media` | Full gallery (id/url/thumb per image), not just the single thumbnail Lunar's base indexer sends. |
|
||||
| `variants` | `$product->variants` | Per variant: `id`, `sku`, `stock`, `purchasable`, `options` (option/value names, in the current locale), `prices` (per currency/customer group), `media` (variant-specific images). |
|
||||
| `reviews` | `Modules\Core\Review\Models\ProductReview` | `{items, count, average_rating}` — see "Reviews" below. |
|
||||
| `in_stock` | `$model->variants` | Filterable boolean. `true` if ANY variant currently passes `ProductVariant::canBeFulfilledAtQuantity(1)` — Lunar's own purchasability rule (`purchasable === 'always'` ignores stock entirely; `in_stock` checks `stock` alone; anything else checks `stock + backorder`). Only as fresh as the last reindex — see "Stock goes stale" below. |
|
||||
|
||||
`name`/`description` (and any other `TranslatedText` attribute) are indexed per-locale — see
|
||||
"Locale resolution" below for how `ProductService` resolves them down to one value per request.
|
||||
|
||||
Reference in New Issue
Block a user