class TypesenseEngine extends Engine implements DeletesByFilter, SupportsSemanticSearch

Typesense search engine implementation.

Provides full-text, semantic, and hybrid search using Typesense as the backend.

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

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)

Get the import concurrency allowed for the given model.

from  Engine
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)

Update the given models through the operation runner.

from  Engine
void
runDelete(Collection $models)

Delete the given models through the operation runner.

from  Engine
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 through the operation runner.

from  Engine
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
__construct(Client $typesense, int $maxTotalResults, array $config = [])

Create a new TypesenseEngine instance.

array
addEmbeddingsToRecords(array $records, array $settings)

Add embeddings to the given searchable records.

importDocuments(Collection $collectionIndex, array $documents, string|null $action = null)

Import the given documents into the index.

createImportSortingDataObject(array $document)

Create an import sorting data object for a given document.

array
deleteDocument(Collection $collectionIndex, mixed $modelId)

Delete a document from the index.

mixed
performSearch(Builder $builder, array $options = [])

Perform the given search on the engine.

mixed
executeSearch(Builder $builder, Documents $documents, array $options)

Execute the given search using the appropriate Typesense endpoint.

TypesenseClientError
marshalMultiSearchException(array $result)

Convert a multi-search error result into a Typesense exception.

array
performPaginatedSearch(Builder $builder)

Perform a paginated search on the engine.

array
buildSearchParameters(Builder $builder, int $page, int|null $perPage)

Build the search parameters for a given Scout query builder.

array
applySemanticSearchParameters(Builder $builder, array $parameters)

Apply semantic and hybrid search parameters to the search parameters.

array
applyNativeSemanticQueryBy(array $parameters, string $attribute)

Target the embedding field for a semantic search using native embeddings.

array
applyHybridQueryBy(array $parameters, array $settings)

Prepare the "query_by" fields and their per-field parameters for a hybrid search.

string|null
buildVectorQueryParameter(Builder $builder, array $settings)

Build the "vector_query" parameter for the search.

float|int
distanceThreshold(float|int $similarity)

Get the maximum vector distance for the given minimum similarity.

string
appendField(string $fields, string $field)

Append a field to a comma-separated field list if not already present.

string
combineFilters(string $applicationFilters, string $builderFilters)

Combine application and Builder filters without changing their precedence.

string
filters(Builder $builder)

Prepare the filters for a given search query.

array|string|int|float
parseFilterValue(array|string|bool|int|float|BackedEnum $value)

Parse the given filter value.

string
parseWhereFilter(array|string|int|float $value, string $key, string $operator = '=')

Create a "where" filter string.

string
parseWhereInFilter(array $value, string $key)

Create a "where in" filter string.

string
parseWhereNotInFilter(array $value, string $key)

Create a "where not in" filter string.

string
parseOrderBy(array $orders)

Parse the order by fields for the query.

void
deleteByFilter(Builder $builder)

Delete every document matching the prepared Builder filters.

void
createCollectionFromModel(Model $model, string $collectionName)

Create the model's Typesense collection.

array
modelSettings(Model $model)

Get the configured settings for a model.

array
embeddingSettings(Model $model)

Get the validated embedding settings for a model.

bool
usesNativeEmbeddings(array $settings)

Determine if Typesense should generate embeddings natively.

array
generateEmbeddings(array $inputs, array $settings)

Generate and validate embeddings using the optional Hypervel AI SDK.

Collection
collection(string $name)

Get a detached Typesense collection handle.

bool
usesSoftDelete(Model $model)

Determine if model uses soft deletes.

Client
getTypesenseClient()

Get the underlying Typesense client.

mixed
getConfig(string $key, mixed $default = null)

Get a Scout configuration value.

mixed
__call(string $method, array $parameters)

Dynamically proxy missing methods to the Typesense client instance.

Details

at line 67
void update(Collection $models)

Update the given models in the search index.

Parameters

Collection $models

Return Value

void

Exceptions

TypesenseClientError

at line 229
void delete(Collection $models)

Remove the given models from the search index.

Parameters

Collection $models

Return Value

void

Exceptions

TypesenseClientError

Perform a search against the engine.

Parameters

Builder $builder

Return Value

mixed

Exceptions

TypesenseClientError

at line 290
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

Exceptions

TypesenseClientError

at line 795
Collection mapIds(mixed $results)

Pluck and return the primary keys of the given results.

Parameters

mixed $results

Return Value

Collection

at line 807
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 845
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 874
int getTotalCount(mixed $results)

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

Parameters

mixed $results

Return Value

int

at line 884
void flush(Model $model)

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

Parameters

Model $model

Return Value

void

Exceptions

TypesenseClientError

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

Create a search index.

Parameters

string $name
array $options

Return Value

mixed

Exceptions

NotSupportedException

at line 945
mixed deleteIndex(string $name)

Delete a search index.

Parameters

string $name

Return Value

mixed

Exceptions

TypesenseClientError
ObjectNotFound

in Engine at line 98
int importConcurrency(Model $model, int $configured)

Get the import concurrency allowed for the given model.

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

in Engine at line 137
void runUpdate(Collection $models)

Update the given models through the operation runner.

Parameters

Collection $models

Return Value

void

in Engine at line 167
void runDelete(Collection $models)

Delete the given models through the operation runner.

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

in Engine at line 235
void runFlush(Model $model)

Flush the given model through the operation runner.

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

at line 54
__construct(Client $typesense, int $maxTotalResults, array $config = [])

Create a new TypesenseEngine instance.

Parameters

Client $typesense
int $maxTotalResults
array $config

at line 134
protected array addEmbeddingsToRecords(array $records, array $settings)

Add embeddings to the given searchable records.

Parameters

array $records
array $settings

Return Value

array

at line 183
protected Collection importDocuments(Collection $collectionIndex, array $documents, string|null $action = null)

Import the given documents into the index.

Parameters

Collection $collectionIndex
array $documents
string|null $action

Return Value

Collection

Exceptions

TypesenseClientError

at line 211
protected stdClass createImportSortingDataObject(array $document)

Create an import sorting data object for a given document.

Parameters

array $document

Return Value

stdClass

at line 253
protected array deleteDocument(Collection $collectionIndex, mixed $modelId)

Delete a document from the index.

Returns an empty array if the document doesn't exist (idempotent delete). Other errors (network, auth, etc.) are allowed to bubble up.

Parameters

Collection $collectionIndex
mixed $modelId

Return Value

array

Exceptions

TypesenseClientError

at line 312
protected mixed performSearch(Builder $builder, array $options = [])

Perform the given search on the engine.

Parameters

Builder $builder
array $options

Return Value

mixed

Exceptions

TypesenseClientError

at line 336
protected mixed executeSearch(Builder $builder, Documents $documents, array $options)

Execute the given search using the appropriate Typesense endpoint.

Parameters

Builder $builder
Documents $documents
array $options

Return Value

mixed

Exceptions

TypesenseClientError

at line 365
protected TypesenseClientError marshalMultiSearchException(array $result)

Convert a multi-search error result into a Typesense exception.

Parameters

array $result

Return Value

TypesenseClientError

at line 386
protected array performPaginatedSearch(Builder $builder)

Perform a paginated search on the engine.

Parameters

Builder $builder

Return Value

array

Exceptions

TypesenseClientError

at line 435
array buildSearchParameters(Builder $builder, int $page, int|null $perPage)

Build the search parameters for a given Scout query builder.

Parameters

Builder $builder
int $page
int|null $perPage

Return Value

array

at line 493
protected array applySemanticSearchParameters(Builder $builder, array $parameters)

Apply semantic and hybrid search parameters to the search parameters.

Parameters

Builder $builder
array $parameters

Return Value

array

at line 530
protected array applyNativeSemanticQueryBy(array $parameters, string $attribute)

Target the embedding field for a semantic search using native embeddings.

Parameters

array $parameters
string $attribute

Return Value

array

at line 555
protected array applyHybridQueryBy(array $parameters, array $settings)

Prepare the "query_by" fields and their per-field parameters for a hybrid search.

Parameters

array $parameters
array $settings

Return Value

array

at line 602
protected string|null buildVectorQueryParameter(Builder $builder, array $settings)

Build the "vector_query" parameter for the search.

Parameters

Builder $builder
array $settings

Return Value

string|null

at line 640
protected float|int distanceThreshold(float|int $similarity)

Get the maximum vector distance for the given minimum similarity.

Parameters

float|int $similarity

Return Value

float|int

at line 652
protected string appendField(string $fields, string $field)

Append a field to a comma-separated field list if not already present.

Parameters

string $fields
string $field

Return Value

string

at line 666
protected string combineFilters(string $applicationFilters, string $builderFilters)

Combine application and Builder filters without changing their precedence.

Parameters

string $applicationFilters
string $builderFilters

Return Value

string

at line 682
protected string filters(Builder $builder)

Prepare the filters for a given search query.

Parameters

Builder $builder

Return Value

string

at line 714
protected array|string|int|float parseFilterValue(array|string|bool|int|float|BackedEnum $value)

Parse the given filter value.

Parameters

array|string|bool|int|float|BackedEnum $value

Return Value

array|string|int|float

at line 736
protected string parseWhereFilter(array|string|int|float $value, string $key, string $operator = '=')

Create a "where" filter string.

Parameters

array|string|int|float $value
string $key
string $operator

Return Value

string

at line 761
protected string parseWhereInFilter(array $value, string $key)

Create a "where in" filter string.

Parameters

array $value
string $key

Return Value

string

at line 771
protected string parseWhereNotInFilter(array $value, string $key)

Create a "where not in" filter string.

Parameters

array $value
string $key

Return Value

string

at line 781
protected string parseOrderBy(array $orders)

Parse the order by fields for the query.

Parameters

array $orders

Return Value

string

at line 897
void deleteByFilter(Builder $builder)

Delete every document matching the prepared Builder filters.

Parameters

Builder $builder

Return Value

void

at line 956
protected void createCollectionFromModel(Model $model, string $collectionName)

Create the model's Typesense collection.

Parameters

Model $model
string $collectionName

Return Value

void

Exceptions

TypesenseClientError

at line 982
protected array modelSettings(Model $model)

Get the configured settings for a model.

Parameters

Model $model

Return Value

array

at line 992
protected array embeddingSettings(Model $model)

Get the validated embedding settings for a model.

Parameters

Model $model

Return Value

array

at line 1032
protected bool usesNativeEmbeddings(array $settings)

Determine if Typesense should generate embeddings natively.

Parameters

array $settings

Return Value

bool

at line 1044
protected array generateEmbeddings(array $inputs, array $settings)

Generate and validate embeddings using the optional Hypervel AI SDK.

Parameters

array $inputs
array $settings

Return Value

array

at line 1067
protected Collection collection(string $name)

Get a detached Typesense collection handle.

Parameters

string $name

Return Value

Collection

at line 1079
protected bool usesSoftDelete(Model $model)

Determine if model uses soft deletes.

Parameters

Model $model

Return Value

bool

at line 1087
Client getTypesenseClient()

Get the underlying Typesense client.

Return Value

Client

at line 1095
protected mixed getConfig(string $key, mixed $default = null)

Get a Scout configuration value.

Parameters

string $key
mixed $default

Return Value

mixed

at line 1105
mixed __call(string $method, array $parameters)

Dynamically proxy missing methods to the Typesense client instance.

Parameters

string $method
array $parameters

Return Value

mixed