class DatabaseEngine extends Engine implements PaginatesEloquentModelsUsingDatabase, SupportsSemanticSearch

Database search engine implementation.

Searches model tables directly, with vector search available through PostgreSQL's pgvector extension.

Traits

Properties

protected EngineOperationRunner|null $operationRunner

The operation runner assigned to this engine.

from  Engine
protected string $operationRunnerEngineName

The configured name of this engine.

from  Engine
protected Model>, array<class-string, string[]>> $attributeColumns

Cached searchable attribute columns by model class and attribute class.

protected Model>, array<string, mixed>> $fullTextOptions

Cached full-text options by model class.

Methods

void
update(Collection $models)

Update the given models in the search index.

void
delete(Collection $models)

Remove the given models from the search index.

mixed
search(Builder $builder)

Perform a search against the engine.

mixed
paginate(Builder $builder, int $perPage, int $page)

Perform a paginated search against the engine.

mapIds(mixed $results)

Pluck and return the primary keys of the given results.

map(Builder $builder, mixed $results, Model $model)

Map the given results to instances of the given model.

lazyMap(Builder $builder, mixed $results, Model $model)

Map the given results to instances of the given model via a lazy collection.

int
getTotalCount(mixed $results)

Get the total count from a raw result returned by the engine.

void
flush(Model $model)

Flush all of the model's records from the engine.

mixed
createIndex(string $name, array $options = [])

Create a search index.

mixed
deleteIndex(string $name)

Delete a search index.

int
importConcurrency(Model $model, int $configured)

Limit vector import concurrency to the imported models' connection pool.

setOperationRunner(EngineOperationRunner $runner, string $engineName)

Assign the operation runner to this engine.

from  Engine
bool
hasObservableOperations()

Determine if this engine has operations worth observing.

from  Engine
void
runUpdate(Collection $models)

Observe embedding updates while bypassing no-op database indexing.

void
runDelete(Collection $models)

Delete the given models without observing an external index operation.

mixed
runSearch(Builder $builder)

Search through the operation runner.

from  Engine
mixed
runPaginate(Builder $builder, int $perPage, int $page)

Paginate through the operation runner.

from  Engine
void
runFlush(Model $model)

Flush the given model without observing an external index operation.

mixed
runOperation(string $operation, Builder $builder, Closure $callback, string|null $index = null)

Run a Builder operation through the operation runner.

from  Engine
mapIdsFrom(mixed $results, string $key)

Pluck and return the primary keys of the given results using the given key name.

from  Engine
keys(Builder $builder)

Get the results of the query as a Collection of primary keys.

from  Engine
get(Builder $builder)

Get the results of the given query mapped onto models.

from  Engine
cursor(Builder $builder)

Get a lazy collection for the given query mapped onto models.

from  Engine
paginateHybridSearch(Builder $builder, int|null $perPage, string $pageName, int|null $page)

Paginate a hybrid search using a bounded fused result window.

simplePaginateHybridSearch(Builder $builder, int|null $perPage, string $pageName, int|null $page)

Simply paginate a hybrid search using a bounded fused result window.

array
hybridPaginationWindow(Builder $builder, int|null $perPage, string $pageName, int|null $page)

Resolve and validate a hybrid pagination window.

paginateSemanticSearch(Builder $builder, int|null $perPage, string $pageName, int|null $page)

Paginate a semantic search while respecting Scout's result limit.

simplePaginateSemanticSearch(Builder $builder, int|null $perPage, string $pageName, int|null $page)

Simply paginate a semantic search while respecting Scout's result limit.

array
resolvePaginationState(Builder $builder, int|null $perPage, string $pageName, int|null $page)

Resolve the current pagination values.

int
resolveResultLimit(Builder $builder)

Resolve the maximum number of results for the search.

void
updateSearchableEmbeddings(Collection $models)

Update the searchable embeddings for the given models.

hybridSearchModels(Builder $builder, int $limit)

Get hybrid search results using weighted reciprocal rank fusion.

fuseSearchResults(Builder $builder, Collection $textModels, Collection $semanticModels)

Fuse text and semantic results using weighted reciprocal rank fusion.

buildSemanticSearchQuery(Builder $builder)

Build a semantic search query.

float
minimumSimilarity(Builder $builder)

Get the minimum similarity for a semantic search.

constrainSearchQuery(Builder $builder, Builder $query)

Apply Scout's non-search constraints to a query.

array
generateEmbeddings(array $inputs)

Generate embeddings using the optional Hypervel AI SDK.

string
embeddingColumn(Model $model)

Get the model's embedding column.

bool
shouldPerformHybridSearch(Builder $builder)

Determine if a hybrid search should use vector search.

bool
supportsVectorSearch(Model $model)

Determine if the model's database supports vector queries.

void
ensureSemanticSearchIsSupported(Builder $builder)

Ensure semantic search is supported by the model's database.

paginateUsingDatabase(Builder $builder, int $perPage, string $pageName, int $page)

Paginate the given search on the engine using database pagination.

simplePaginate(Builder $builder, int $perPage, int $page)

Paginate the given search on the engine using simple pagination.

simplePaginateUsingDatabase(Builder $builder, int $perPage, string $pageName, int $page)

Paginate the given search on the engine using simple database pagination.

searchModels(Builder $builder, int|null $page = null, int|null $perPage = null)

Get the Eloquent models for the given builder.

buildSearchQuery(Builder $builder)

Initialize / build the search query for the given Scout builder.

initializeSearchQuery(Builder $builder, array $columns, array $prefixColumns = [], array $fullTextColumns = [])

Build the initial text search database query for all relevant columns.

newSearchQuery(Builder $builder)

Create the model query used for a Scout search.

addTextSearchConstraints(Builder $query, Builder $builder, array $columns, array $prefixColumns = [], array $fullTextColumns = [])

Add text search constraints to the given query.

bool
shouldOrderByRelevance(Builder $builder)

Determine if the query should be ordered by relevance.

orderByRelevance(Builder $builder, Builder $query)

Add an "order by" clause that orders by relevance (Postgres only).

addAdditionalConstraints(Builder $builder, Builder $query)

Add additional, developer defined constraints to the search query.

constrainForSoftDeletes(Builder $builder, Builder $query)

Ensure that soft delete constraints are properly applied to the query.

array
getFullTextColumns(Builder $builder)

Get the full-text columns for the query.

array
getPrefixColumns(Builder $builder)

Get the prefix search columns for the query.

array
getAttributeColumns(Builder $builder, string $attributeClass)

Get the columns marked with a given attribute.

array
getFullTextOptions(Builder $builder)

Get the full-text search options for the query.

Details

at line 560
void update(Collection $models)

Update the given models in the search index.

Parameters

Collection $models

Return Value

void

at line 608
void delete(Collection $models)

Remove the given models from the search index.

The database engine doesn't need to remove from an external index.

Parameters

Collection $models

Return Value

void

Perform a search against the engine.

Parameters

Builder $builder

Return Value

mixed

at line 80
mixed paginate(Builder $builder, int $perPage, int $page)

Perform a paginated search against the engine.

Parameters

Builder $builder
int $perPage
int $page

Return Value

mixed

at line 516
Collection mapIds(mixed $results)

Pluck and return the primary keys of the given results.

Parameters

mixed $results

Return Value

Collection

at line 531
Collection map(Builder $builder, mixed $results, Model $model)

Map the given results to instances of the given model.

Parameters

Builder $builder
mixed $results
Model $model

Return Value

Collection

at line 539
LazyCollection lazyMap(Builder $builder, mixed $results, Model $model)

Map the given results to instances of the given model via a lazy collection.

Parameters

Builder $builder
mixed $results
Model $model

Return Value

LazyCollection

at line 550
int getTotalCount(mixed $results)

Get the total count from a raw result returned by the engine.

Parameters

mixed $results

Return Value

int

at line 626
void flush(Model $model)

Flush all of the model's records from the engine.

Parameters

Model $model

Return Value

void

at line 642
mixed createIndex(string $name, array $options = [])

Create a search index.

Parameters

string $name
array $options

Return Value

mixed

at line 651
mixed deleteIndex(string $name)

Delete a search index.

Parameters

string $name

Return Value

mixed

at line 568
int importConcurrency(Model $model, int $configured)

Limit vector import concurrency to the imported models' connection pool.

Parameters

Model $model
int $configured

Return Value

int

in Engine at line 111
Engine setOperationRunner(EngineOperationRunner $runner, string $engineName)

Assign the operation runner to this engine.

The runner persists on a cached engine for the worker lifetime. Engines without observable operations retain no runner.

Parameters

EngineOperationRunner $runner
string $engineName

Return Value

Engine

in Engine at line 127
protected bool hasObservableOperations()

Determine if this engine has operations worth observing.

Return Value

bool

at line 586
void runUpdate(Collection $models)

Observe embedding updates while bypassing no-op database indexing.

Parameters

Collection $models

Return Value

void

at line 618
void runDelete(Collection $models)

Delete the given models without observing an external index operation.

Parameters

Collection $models

Return Value

void

in Engine at line 195
mixed runSearch(Builder $builder)

Search through the operation runner.

Parameters

Builder $builder

Return Value

mixed

in Engine at line 215
mixed runPaginate(Builder $builder, int $perPage, int $page)

Paginate through the operation runner.

Parameters

Builder $builder
int $perPage
int $page

Return Value

mixed

at line 634
void runFlush(Model $model)

Flush the given model without observing an external index operation.

Parameters

Model $model

Return Value

void

in Engine at line 265
mixed runOperation(string $operation, Builder $builder, Closure $callback, string|null $index = null)

Run a Builder operation through the operation runner.

An omitted index uses the Builder's configured or searchable index. Pass an explicit index when the operation targets another dataset.

Parameters

string $operation
Builder $builder
Closure $callback
string|null $index

Return Value

mixed

in Engine at line 291
Collection mapIdsFrom(mixed $results, string $key)

Pluck and return the primary keys of the given results using the given key name.

Parameters

mixed $results
string $key

Return Value

Collection

in Engine at line 299
Collection keys(Builder $builder)

Get the results of the query as a Collection of primary keys.

Parameters

Builder $builder

Return Value

Collection

in Engine at line 307
Collection get(Builder $builder)

Get the results of the given query mapped onto models.

Parameters

Builder $builder

Return Value

Collection

in Engine at line 319
LazyCollection cursor(Builder $builder)

Get a lazy collection for the given query mapped onto models.

Parameters

Builder $builder

Return Value

LazyCollection

protected LengthAwarePaginator paginateHybridSearch(Builder $builder, int|null $perPage, string $pageName, int|null $page)

Paginate a hybrid search using a bounded fused result window.

Parameters

Builder $builder
int|null $perPage
string $pageName
int|null $page

Return Value

LengthAwarePaginator

protected Paginator simplePaginateHybridSearch(Builder $builder, int|null $perPage, string $pageName, int|null $page)

Simply paginate a hybrid search using a bounded fused result window.

Parameters

Builder $builder
int|null $perPage
string $pageName
int|null $page

Return Value

Paginator

protected array hybridPaginationWindow(Builder $builder, int|null $perPage, string $pageName, int|null $page)

Resolve and validate a hybrid pagination window.

Parameters

Builder $builder
int|null $perPage
string $pageName
int|null $page

Return Value

array

protected LengthAwarePaginator paginateSemanticSearch(Builder $builder, int|null $perPage, string $pageName, int|null $page)

Paginate a semantic search while respecting Scout's result limit.

Parameters

Builder $builder
int|null $perPage
string $pageName
int|null $page

Return Value

LengthAwarePaginator

protected Paginator simplePaginateSemanticSearch(Builder $builder, int|null $perPage, string $pageName, int|null $page)

Simply paginate a semantic search while respecting Scout's result limit.

Parameters

Builder $builder
int|null $perPage
string $pageName
int|null $page

Return Value

Paginator

protected array resolvePaginationState(Builder $builder, int|null $perPage, string $pageName, int|null $page)

Resolve the current pagination values.

Parameters

Builder $builder
int|null $perPage
string $pageName
int|null $page

Return Value

array

protected int resolveResultLimit(Builder $builder)

Resolve the maximum number of results for the search.

Parameters

Builder $builder

Return Value

int

protected void updateSearchableEmbeddings(Collection $models)

Update the searchable embeddings for the given models.

Parameters

Collection $models

Return Value

void

protected Collection hybridSearchModels(Builder $builder, int $limit)

Get hybrid search results using weighted reciprocal rank fusion.

Parameters

Builder $builder
int $limit

Return Value

Collection

protected Collection fuseSearchResults(Builder $builder, Collection $textModels, Collection $semanticModels)

Fuse text and semantic results using weighted reciprocal rank fusion.

Parameters

Builder $builder
Collection $textModels
Collection $semanticModels

Return Value

Collection

protected Builder buildSemanticSearchQuery(Builder $builder)

Build a semantic search query.

Parameters

Builder $builder

Return Value

Builder

protected float minimumSimilarity(Builder $builder)

Get the minimum similarity for a semantic search.

Parameters

Builder $builder

Return Value

float

protected Builder constrainSearchQuery(Builder $builder, Builder $query)

Apply Scout's non-search constraints to a query.

Parameters

Builder $builder
Builder $query

Return Value

Builder

protected array generateEmbeddings(array $inputs)

Generate embeddings using the optional Hypervel AI SDK.

Parameters

array $inputs

Return Value

array

protected string embeddingColumn(Model $model)

Get the model's embedding column.

Parameters

Model $model

Return Value

string

protected bool shouldPerformHybridSearch(Builder $builder)

Determine if a hybrid search should use vector search.

Parameters

Builder $builder

Return Value

bool

protected bool supportsVectorSearch(Model $model)

Determine if the model's database supports vector queries.

Parameters

Model $model

Return Value

bool

protected void ensureSemanticSearchIsSupported(Builder $builder)

Ensure semantic search is supported by the model's database.

Parameters

Builder $builder

Return Value

void

at line 88
LengthAwarePaginator paginateUsingDatabase(Builder $builder, int $perPage, string $pageName, int $page)

Paginate the given search on the engine using database pagination.

Parameters

Builder $builder
int $perPage
string $pageName
int $page

Return Value

LengthAwarePaginator

at line 120
Paginator simplePaginate(Builder $builder, int $perPage, int $page)

Paginate the given search on the engine using simple pagination.

Parameters

Builder $builder
int $perPage
int $page

Return Value

Paginator

at line 128
Paginator simplePaginateUsingDatabase(Builder $builder, int $perPage, string $pageName, int $page)

Paginate the given search on the engine using simple database pagination.

Parameters

Builder $builder
int $perPage
string $pageName
int $page

Return Value

Paginator

at line 162
protected Collection searchModels(Builder $builder, int|null $page = null, int|null $perPage = null)

Get the Eloquent models for the given builder.

Parameters

Builder $builder
int|null $page
int|null $perPage

Return Value

Collection

at line 195
protected Builder buildSearchQuery(Builder $builder)

Initialize / build the search query for the given Scout builder.

Parameters

Builder $builder

Return Value

Builder

at line 217
protected Builder initializeSearchQuery(Builder $builder, array $columns, array $prefixColumns = [], array $fullTextColumns = [])

Build the initial text search database query for all relevant columns.

Parameters

Builder $builder
array $columns
array $prefixColumns
array $fullTextColumns

Return Value

Builder

at line 235
protected Builder newSearchQuery(Builder $builder)

Create the model query used for a Scout search.

Parameters

Builder $builder

Return Value

Builder

at line 249
protected Builder addTextSearchConstraints(Builder $query, Builder $builder, array $columns, array $prefixColumns = [], array $fullTextColumns = [])

Add text search constraints to the given query.

Parameters

Builder $query
Builder $builder
array $columns
array $prefixColumns
array $fullTextColumns

Return Value

Builder

at line 327
protected bool shouldOrderByRelevance(Builder $builder)

Determine if the query should be ordered by relevance.

Parameters

Builder $builder

Return Value

bool

at line 339
protected Builder orderByRelevance(Builder $builder, Builder $query)

Add an "order by" clause that orders by relevance (Postgres only).

Parameters

Builder $builder
Builder $query

Return Value

Builder

at line 370
protected Builder addAdditionalConstraints(Builder $builder, Builder $query)

Add additional, developer defined constraints to the search query.

Parameters

Builder $builder
Builder $query

Return Value

Builder

at line 401
protected Builder constrainForSoftDeletes(Builder $builder, Builder $query)

Ensure that soft delete constraints are properly applied to the query.

Parameters

Builder $builder
Builder $query

Return Value

Builder

at line 434
protected array getFullTextColumns(Builder $builder)

Get the full-text columns for the query.

Parameters

Builder $builder

Return Value

array

at line 444
protected array getPrefixColumns(Builder $builder)

Get the prefix search columns for the query.

Parameters

Builder $builder

Return Value

array

at line 455
protected array getAttributeColumns(Builder $builder, string $attributeClass)

Get the columns marked with a given attribute.

Parameters

Builder $builder
string $attributeClass

Return Value

array

at line 492
protected array getFullTextOptions(Builder $builder)

Get the full-text search options for the query.

Parameters

Builder $builder

Return Value

array