Overview
Search in Lunar is built on Laravel Scout, providing a flexible, driver-based search system. Scout’s database driver offers basic search out of the box, while more powerful engines like Meilisearch and Typesense can be swapped in for advanced features such as faceted filtering and relevance tuning. All search configuration lives inconfig/lunar/search.php. This file controls which models are indexed, which search engine each model uses, and which indexer class prepares the data for each model.
Configuration
Soft Deletes
By default, Scout sets thesoft_delete option to false. This must be set to true in config/scout.php, otherwise soft-deleted models will appear in search results.
Searchable Models
Themodels array in config/lunar/search.php defines which models are indexed. Lunar registers the following models by default:
Lunar\Base\Traits\Searchable trait.
Engine Mapping
By default, Scout uses the driver defined by theSCOUT_DRIVER environment variable for all models. This means if SCOUT_DRIVER is set to meilisearch, every searchable model gets indexed via Meilisearch.
This may not always be desirable. For example, indexing orders in a paid service like Algolia alongside products would unnecessarily increase record counts and cost. Lunar allows specifying a different driver per model using the engine_map configuration:
engine_map falls back to the default Scout driver.
Searchable Trait
All searchable Lunar models use theLunar\Base\Traits\Searchable trait. This trait extends Scout’s Searchable trait and delegates key behavior to an indexer class:
searchableAs()— returns the index nametoSearchableArray()— returns the data to indexshouldBeSearchable()— determines whether the model should be indexedsearchableUsing()— returns the engine based on theengine_mapconfigurationgetFilterableAttributes()— returns filterable fields for the enginegetSortableAttributes()— returns sortable fields for the engine
indexers config, falling back to the base Lunar\Search\ScoutIndexer if no specific indexer is mapped.
Indexers
Each searchable model is paired with an indexer class that controls what data is sent to the search engine. Theindexers config maps models to their indexer:
Default Indexer
The baseLunar\Search\ScoutIndexer class indexes:
- The model’s
id - Any attributes marked as
searchable
TranslatedText attribute fields, creating locale-suffixed entries (e.g., name_en, name_fr) in the index.
Product Indexer
TheLunar\Search\ProductIndexer indexes the following fields:
Sortable fields:
created_at, updated_at, skus, status
Filterable fields: __soft_deleted, skus, status
Order Indexer
TheLunar\Search\OrderIndexer indexes the following fields:
Sortable fields:
customer_id, user_id, channel_id, created_at, updated_at, total
Filterable fields: customer_id, user_id, status, placed_at, channel_id, tags, __soft_deleted
Customer Indexer
TheLunar\Search\CustomerIndexer indexes the following fields:
Sortable fields:
created_at, updated_at, name, company_name
Filterable fields: __soft_deleted, name, company_name
Brand Indexer
TheLunar\Search\BrandIndexer indexes the following fields:
Sortable fields:
created_at, updated_at, name
Filterable fields: __soft_deleted, name
Collection Indexer
TheLunar\Search\CollectionIndexer indexes the following fields:
Sortable fields:
created_at, updated_at, name
Filterable fields: __soft_deleted, name
ProductOption Indexer
TheLunar\Search\ProductOptionIndexer indexes the following fields:
Sortable fields:
created_at, updated_at
Filterable fields: __soft_deleted
Indexing Records
To import or refresh search indexes, use thelunar:search:index artisan command:
models configuration.
Command Options
Meilisearch Setup
When using Meilisearch, run the setup command to configure filterable and sortable attributes on Meilisearch indexes:Storefront Search Add-on
For storefront search features such as faceted filtering, sorting, pagination, and structured response data, install the Storefront Search add-on:Search facade with support for Meilisearch, Typesense, and database drivers, returning consistent SearchResults objects with hits, facets, and pagination.