DatabaseEngine
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
Pluck and return the primary keys of the given results.
Get the total count from a raw result returned by the engine.
Create a search index.
Delete a search index.
Limit vector import concurrency to the imported models' connection pool.
Assign the operation runner to this engine.
Delete the given models without observing an external index operation.
Run a Builder operation through the operation runner.
Pluck and return the primary keys of the given results using the given key name.
Paginate a hybrid search using a bounded fused result window.
Simply paginate a hybrid search using a bounded fused result window.
Resolve and validate a hybrid pagination window.
Paginate a semantic search while respecting Scout's result limit.
Simply paginate a semantic search while respecting Scout's result limit.
Resolve the current pagination values.
Update the searchable embeddings for the given models.
Get hybrid search results using weighted reciprocal rank fusion.
Fuse text and semantic results using weighted reciprocal rank fusion.
Apply Scout's non-search constraints to a query.
Generate embeddings using the optional Hypervel AI SDK.
Ensure semantic search is supported by the model's database.
Paginate the given search on the engine using database pagination.
Paginate the given search on the engine using simple pagination.
Paginate the given search on the engine using simple database pagination.
Get the Eloquent models for the given builder.
Build the initial text search database query for all relevant columns.
Add text search constraints to the given query.
Add an "order by" clause that orders by relevance (Postgres only).
Add additional, developer defined constraints to the search query.
Ensure that soft delete constraints are properly applied to the query.
Get the columns marked with a given attribute.
Details
at line 560
void
update(Collection $models)
Update the given models in the search index.
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.
at line 58
mixed
search(Builder $builder)
Perform a search against the engine.
at line 80
mixed
paginate(Builder $builder, int $perPage, int $page)
Perform a paginated search against the engine.
at line 516
Collection
mapIds(mixed $results)
Pluck and return the primary keys of the given results.
at line 531
Collection
map(Builder $builder, mixed $results, Model $model)
Map the given results to instances of the given model.
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.
at line 550
int
getTotalCount(mixed $results)
Get the total count from a raw result returned by the engine.
at line 626
void
flush(Model $model)
Flush all of the model's records from the engine.
at line 642
mixed
createIndex(string $name, array $options = [])
Create a search index.
at line 651
mixed
deleteIndex(string $name)
Delete a search index.
at line 568
int
importConcurrency(Model $model, int $configured)
Limit vector import concurrency to the imported models' connection pool.
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.
in
Engine at line 127
protected bool
hasObservableOperations()
Determine if this engine has operations worth observing.
at line 586
void
runUpdate(Collection $models)
Observe embedding updates while bypassing no-op database indexing.
at line 618
void
runDelete(Collection $models)
Delete the given models without observing an external index operation.
in
Engine at line 215
mixed
runPaginate(Builder $builder, int $perPage, int $page)
Paginate through the operation runner.
at line 634
void
runFlush(Model $model)
Flush the given model without observing an external index operation.
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.
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.
in
Engine at line 299
Collection
keys(Builder $builder)
Get the results of the query as a Collection of primary keys.
in
Engine at line 307
Collection
get(Builder $builder)
Get the results of the given query mapped onto models.
in
Engine at line 319
LazyCollection
cursor(Builder $builder)
Get a lazy collection for the given query mapped onto models.
in
PaginatesDatabaseVectorSearch at line 17
protected LengthAwarePaginator
paginateHybridSearch(Builder $builder, int|null $perPage, string $pageName, int|null $page)
Paginate a hybrid search using a bounded fused result window.
in
PaginatesDatabaseVectorSearch at line 40
protected Paginator
simplePaginateHybridSearch(Builder $builder, int|null $perPage, string $pageName, int|null $page)
Simply paginate a hybrid search using a bounded fused result window.
in
PaginatesDatabaseVectorSearch at line 66
protected array
hybridPaginationWindow(Builder $builder, int|null $perPage, string $pageName, int|null $page)
Resolve and validate a hybrid pagination window.
in
PaginatesDatabaseVectorSearch at line 85
protected LengthAwarePaginator
paginateSemanticSearch(Builder $builder, int|null $perPage, string $pageName, int|null $page)
Paginate a semantic search while respecting Scout's result limit.
in
PaginatesDatabaseVectorSearch at line 119
protected Paginator
simplePaginateSemanticSearch(Builder $builder, int|null $perPage, string $pageName, int|null $page)
Simply paginate a semantic search while respecting Scout's result limit.
in
PaginatesDatabaseVectorSearch at line 152
protected array
resolvePaginationState(Builder $builder, int|null $perPage, string $pageName, int|null $page)
Resolve the current pagination values.
in
PaginatesDatabaseVectorSearch at line 165
protected int
resolveResultLimit(Builder $builder)
Resolve the maximum number of results for the search.
in
PerformsDatabaseVectorSearch at line 23
protected void
updateSearchableEmbeddings(Collection $models)
Update the searchable embeddings for the given models.
in
PerformsDatabaseVectorSearch at line 84
protected Collection
hybridSearchModels(Builder $builder, int $limit)
Get hybrid search results using weighted reciprocal rank fusion.
in
PerformsDatabaseVectorSearch at line 144
protected Collection
fuseSearchResults(Builder $builder, Collection $textModels, Collection $semanticModels)
Fuse text and semantic results using weighted reciprocal rank fusion.
in
PerformsDatabaseVectorSearch at line 179
protected Builder
buildSemanticSearchQuery(Builder $builder)
Build a semantic search query.
in
PerformsDatabaseVectorSearch at line 205
protected float
minimumSimilarity(Builder $builder)
Get the minimum similarity for a semantic search.
in
PerformsDatabaseVectorSearch at line 219
protected Builder
constrainSearchQuery(Builder $builder, Builder $query)
Apply Scout's non-search constraints to a query.
in
PerformsDatabaseVectorSearch at line 230
protected array
generateEmbeddings(array $inputs)
Generate embeddings using the optional Hypervel AI SDK.
in
PerformsDatabaseVectorSearch at line 250
protected string
embeddingColumn(Model $model)
Get the model's embedding column.
in
PerformsDatabaseVectorSearch at line 266
protected bool
shouldPerformHybridSearch(Builder $builder)
Determine if a hybrid search should use vector search.
in
PerformsDatabaseVectorSearch at line 276
protected bool
supportsVectorSearch(Model $model)
Determine if the model's database supports vector queries.
in
PerformsDatabaseVectorSearch at line 284
protected void
ensureSemanticSearchIsSupported(Builder $builder)
Ensure semantic search is supported by the model's database.
at line 88
LengthAwarePaginator
paginateUsingDatabase(Builder $builder, int $perPage, string $pageName, int $page)
Paginate the given search on the engine using database pagination.
at line 120
Paginator
simplePaginate(Builder $builder, int $perPage, int $page)
Paginate the given search on the engine using simple pagination.
at line 128
Paginator
simplePaginateUsingDatabase(Builder $builder, int $perPage, string $pageName, int $page)
Paginate the given search on the engine using simple database pagination.
at line 162
protected Collection
searchModels(Builder $builder, int|null $page = null, int|null $perPage = null)
Get the Eloquent models for the given builder.
at line 195
protected Builder
buildSearchQuery(Builder $builder)
Initialize / build the search query for the given Scout 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.
at line 235
protected Builder
newSearchQuery(Builder $builder)
Create the model query used for a Scout search.
at line 249
protected Builder
addTextSearchConstraints(Builder $query, Builder $builder, array $columns, array $prefixColumns = [], array $fullTextColumns = [])
Add text search constraints to the given query.
at line 327
protected bool
shouldOrderByRelevance(Builder $builder)
Determine if the query should be ordered by relevance.
at line 339
protected Builder
orderByRelevance(Builder $builder, Builder $query)
Add an "order by" clause that orders by relevance (Postgres only).
at line 370
protected Builder
addAdditionalConstraints(Builder $builder, Builder $query)
Add additional, developer defined constraints to the search query.
at line 401
protected Builder
constrainForSoftDeletes(Builder $builder, Builder $query)
Ensure that soft delete constraints are properly applied to the query.
at line 434
protected array
getFullTextColumns(Builder $builder)
Get the full-text columns for the query.
at line 444
protected array
getPrefixColumns(Builder $builder)
Get the prefix search columns for the query.
at line 455
protected array
getAttributeColumns(Builder $builder, string $attributeClass)
Get the columns marked with a given attribute.
at line 492
protected array
getFullTextOptions(Builder $builder)
Get the full-text search options for the query.