cmsig/seal-typesense-adapter
Typesense adapter for the SEAL search engine abstraction. Index and update documents in a Typesense server, create an Engine with a Typesense client, or configure via a typesense:// DSN (including optional TLS). Part of the cmsig/search project.
Install Dependencies
composer require cmsig/seal cmsig/seal-typesense-adapter
Configure Typesense Client Create a Typesense client instance with your API key, nodes, and HTTP client:
use Typesense\Client;
use Http\Client\Curl\Client as CurlClient;
use Http\Discovery\Psr17FactoryDiscovery;
$client = new Client([
'api_key' => env('TYPESENSE_API_KEY'),
'nodes' => [
['host' => env('TYPESENSE_HOST'), 'port' => env('TYPESENSE_PORT'), 'protocol' => env('TYPESENSE_PROTOCOL', 'http')],
],
'client' => new CurlClient(
Psr17FactoryDiscovery::findResponseFactory(),
Psr17FactoryDiscovery::findStreamFactory()
),
]);
Initialize SEAL Engine Pass the Typesense adapter and your schema to the SEAL engine:
use CmsIg\Seal\Adapter\Typesense\TypesenseAdapter;
use CmsIg\Seal\Engine;
$adapter = new TypesenseAdapter($client);
$engine = new Engine($adapter, $schema);
First Use Case: Indexing Documents
$engine->index('products', [
['id' => 1, 'name' => 'Laptop', 'price' => 999.99],
['id' => 2, 'name' 'Phone', 'price' => 699.99],
]);
Schema Management Define your schema in SEAL format and let the adapter handle Typesense-specific mappings:
$schema = new \CmsIg\Seal\Schema\Schema();
$schema->addField('name', 'text', ['facet' => true]);
$schema->addField('price', 'float');
Search Integration Use SEAL’s query builder to interact with Typesense:
$results = $engine->search('products', 'Laptop', [
'query_by' => 'name',
'per_page' => 10,
]);
Bulk Operations Leverage Typesense’s bulk API via SEAL’s batch methods:
$engine->batch('products', [
['id' => 3, 'name' => 'Tablet', 'price' => 399.99],
['id' => 4, 'name' => 'Monitor', 'price' => 249.99],
], ['action' => 'upsert']);
Environment Configuration
Use .env for Typesense credentials (e.g., TYPESENSE_API_KEY, TYPESENSE_HOST).
Example DSN:
TYPESENSE_DSN=typesense://S3CR3T@127.0.0.1:8108?tls=true
Dependency Injection Register the adapter and engine in Laravel’s service container:
$this->app->bind(TypesenseAdapter::class, function ($app) {
return new TypesenseAdapter(new Client([
'api_key' => $app['config']['typesense.api_key'],
'nodes' => [['host' => $app['config']['typesense.host']]],
]));
});
Schema Validation
Validate SEAL schemas against Typesense’s supported field types (e.g., avoid unsupported types like geopoint if not configured in Typesense).
Schema Mismatches
type, optional). SEAL schemas must align with Typesense’s field types.TypesenseAdapter::validateSchema() or handle exceptions during engine->index().Connection Issues
.env) can cause deployment failures.config() or environment variables for dynamic configuration.Bulk Operation Limits
batch_size parameter.TLS/HTTPS Misconfigurations
tls=true in the DSN for HTTPS endpoints may fail silently.protocol: 'https' and tls: true in the client config.Enable HTTP Logging
Use Http\Client\Common\Plugin\LoggerPlugin to log Typesense API requests:
$client->addPlugin(new LoggerPlugin(new \Psr\Log\NullLogger()));
Check Typesense Server Logs
For adapter-specific errors, inspect Typesense’s logs at http://<host>:<port>/debug.
Custom Field Mappings
Override TypesenseAdapter::mapSchema() to transform SEAL fields into Typesense-specific formats:
protected function mapSchema(\CmsIg\Seal\Schema\Schema $schema): array {
$mapped = parent::mapSchema($schema);
$mapped['fields']['price']['type'] = 'int64'; // Custom mapping
return $mapped;
}
Query Modifiers
Extend SEAL’s query builder to support Typesense-specific features (e.g., group_by):
$engine->search('products', '', [
'group_by' => 'category',
'group_limit' => 5,
]);
Retry Logic
Implement exponential backoff for transient failures (e.g., network timeouts) using Http\Client\Common\Plugin\RetryPlugin.
Use Typesense’s Playground
Test queries interactively at http://<host>:<port>/collections/<collection>/documents/playground to debug SEAL-generated queries.
Leverage SEAL’s Abstraction Switch search backends (e.g., Elasticsearch) without changing application logic by relying on SEAL’s interface.
Monitor Performance
Typesense’s metrics endpoint can help optimize SEAL queries (e.g., avoid * wildcards in query_by).
How can I help you explore Laravel packages today?