Overview
The Storefront Search add-on provides a unified API for performing search operations across multiple search engines. It wraps Laravel Scout and adds support for faceted search, filtering, sorting, and consistent response formatting using Spatie Laravel Data.Supported Engines
| Engine | Driver Name | Features |
|---|---|---|
| Database | database | Basic search via Scout’s database driver |
| Meilisearch | meilisearch | Faceted search, filtering, sorting |
| Typesense | typesense | Faceted search, filtering, sorting, highlights, vector search |
Installation
Require the composer package
Configuration
Publish and customize the configuration by creating or editingconfig/lunar/search.php. The add-on configuration controls which facets are available for each model:
label property is optional and defaults to the field name if not provided.
Usage
Basic Search
Search models using theSearch facade. By default, searches are performed against Lunar\Models\Product:
model() method:
engine_map configuration and performs the search using that driver. To improve performance, results are not hydrated from the database — instead, the raw indexed data is returned directly from the search provider.
Specifying a Driver
To explicitly use a specific search driver, call thedriver() method:
Filtering
Apply filters to narrow down search results. Filters are passed as key-value pairs where the key is the field name and the value is the filter value:Faceted Search
Facets allow users to refine search results by selecting values within categories (e.g., brand, color, size). Set active facet selections usingsetFacets():
Sorting
Sort results by a specific field:field:direction where direction is either asc or desc. The field must be configured as sortable in the search engine.
For Typesense, a raw sort expression can also be used:
Pagination
Control the number of results per page using theperPage() method. The default is 50:
Extending Queries
For advanced use cases, extend the search query usingextendQuery():
Response Format
All search engines return aLunar\Search\Data\SearchResults object with a consistent structure:
| Property | Type | Description |
|---|---|---|
query | ?string | The search query that was executed |
count | int | Total number of matching results |
page | int | Current page number |
perPage | int | Number of results per page |
totalPages | int | Total number of pages |
hits | SearchHit[] | Array of search result hits |
facets | SearchFacet[] | Array of available facets with counts |
links | View | Pagination links (Laravel paginator view) |
SearchHit
Each hit contains the indexed document data and any highlights (Typesense only):| Property | Type | Description |
|---|---|---|
highlights | SearchHitHighlight[] | Matched field highlights |
document | array | The raw indexed document data |
SearchHitHighlight
| Property | Type | Description |
|---|---|---|
field | string | The field that matched |
matches | string[] | The matched tokens |
snippet | ?string | A highlighted snippet of the match |
SearchFacet
| Property | Type | Description |
|---|---|---|
label | string | Display label for the facet |
field | string | The index field name |
values | SearchFacetValue[] | Available values with counts |
hierarchy | bool | Whether this facet is hierarchical |
SearchFacetValue
| Property | Type | Description |
|---|---|---|
label | string | Display label for the value |
value | string | The actual value for filtering |
count | int | Number of results matching this value |
active | bool | Whether this value is currently selected |
children | SearchFacetValue[] | Child values for hierarchical facets |
Handling the Response
Displaying Results
Displaying Facets
Pagination
Thelinks property contains a standard Laravel pagination view:
Accessing Pagination Metadata
TypeScript Integration
If Spatie TypeScript Transformer is being used, add the data path to thetypescript-transformer.php config to generate TypeScript types for the search response classes:
Lunar.Search namespace: