Skip to main content

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

Installation

Require the composer package

The package auto-discovers its service provider, so no additional registration is needed.

Configuration

Publish and customize the configuration by creating or editing config/lunar/search.php. The add-on configuration controls which facets are available for each model:
Each facet key corresponds to a field in the searchable index. The label property is optional and defaults to the field name if not provided.
The engine_map configuration, which controls which search driver is used for each model, is defined in the core Lunar search config. See the Search reference for details.

Usage

Search models using the Search facade. By default, searches are performed against Lunar\Models\Product:
To search a different model, use the model() method:
Under the hood, the package detects which Scout driver is mapped for the given model via the 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 the driver() 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:
Facets allow users to refine search results by selecting values within categories (e.g., brand, color, size). Set active facet selections using setFacets():
The search response includes updated facet counts that reflect the current selections, allowing the storefront to show how many results match each facet value. To remove a specific facet or value:

Sorting

Sort results by a specific field:
The sort format is 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 the perPage() method. The default is 50:

Extending Queries

For advanced use cases, extend the search query using extendQuery():

Response Format

All search engines return a Lunar\Search\Data\SearchResults object with a consistent structure:

SearchHit

Each hit contains the indexed document data and any highlights (Typesense only):

SearchHitHighlight

SearchFacet

SearchFacetValue

Handling the Response

Displaying Results

Displaying Facets

Pagination

The links property contains a standard Laravel pagination view:

Accessing Pagination Metadata

TypeScript Integration

If Spatie TypeScript Transformer is being used, add the data path to the typescript-transformer.php config to generate TypeScript types for the search response classes:
The generated types are available under the Lunar.Search namespace: