typesense/typesense-php
Official PHP client for the Typesense search API. Built on HTTPlug for flexible HTTP adapters, with examples for indexing, searching, and filtering (including safe filter value escaping). Install via Composer and use with compatible Typesense Server versions.
Install the package via Composer: composer require typesense/typesense-php. Initialize the client with your Typesense server URL, API key, and optional timeout settings:
$client = new Typesense\Client([
'api_key' => 'your_api_key',
'nodes' => [
['host' => 'localhost', 'port' => 8108, 'protocol' => 'http'],
],
'connection_timeout_seconds' => 2,
]);
Start by creating a collection (schema) and indexing documents—this is the core day-to-day workflow. Check the official docs for schema structure and indexing examples. The client’s collections, documents, and search methods form the primary interface.
Typesense\Client as a shared service (e.g., via Laravel’s app()->singleton() or custom service provider) to reuse connections across requests.$client->collections['products']->documents->search($query, $params)—with query-time params like q, query_by, filter_by, sort_by, and facet_by.documents->import() with a JSONL payload (array of document strings) for high-performance batch inserts or updates.ProductSearchService) to decouple controllers from typesense-specific logic.collections['collection_name']->synonyms and collections['collection_name']->curations endpoints to manage search relevance, especially with the fix in v6.1.0-RC1 for nested item access. Always validate nested structures (e.g., replace_results, metadata) before importing to avoid silent errors..env + config files with typesense.api_key masked in phpinfo() or logs.123) are cast to strings ("123") before indexing to avoid silent failures.nodes array) with healthcheck_interval_seconds configured; the client auto-detects downed nodes.facet_by returns arrays—ensure proper type handling (e.g., collect($result['facet_counts'] ?? [])) to avoid undefined array key errors.log_level via new Client([...], 'log_level' => 'debug') or patch Guzzle middleware to inspect HTTP requests/responses.collections->update() cautiously (only for allowed changes), or create new collections + aliases for full migrations.connection_timeout_seconds and read_timeout_seconds explicitly—default HTTP behavior may cause hanging requests during high load. Consider Laravel’s retry + timeout strategies around critical operations.replace_results[0].product_id) previously caused silent failures and are now caught.How can I help you explore Laravel packages today?