SearchEngine::Client
A thin wrapper around the officialtypesense Ruby gem that provides single-search and federated multi-search. It enforces that cache knobs live in URL/common params (never in per-search bodies) and normalizes gem/network failures into SearchEngine::Errors.
Usage
PreferSearchEngine.client to get the configured client instance. It respects
SearchEngine.config.client when set, returns OfflineClient in test mode, or
creates a new Client instance:
SearchEngine::Client directly when you need a specific
instance:
Note: The default Typesense client connection timeout is 60 minutes, to avoid aborting long-running writes. You can override it, and you can introduce retry jitter by using a Range for
retries[:backoff]. See Configuration.Cache management
SearchEngine::Cache.clearissues aPOST /operations/cache/clearvia the client and returns the symbolized Typesense response. Call it after large imports or schema changes when you need fresh cached results immediately.- Emits
search_engine.cache.clear(see Observability) so subscribers/loggers capture cadence and duration; usesSearchEngine.clientso injected clients and test mode are honored. - Reference: Typesense cluster operations → Clear cache.
Operations helper (SearchEngine.operations)
Use the convenience wrapper when you need instrumented, redacted operational calls (no request body needed):
- Accepts optional
client:for dependency injection; otherwise usesSearchEngine.client(respects injected clients, test mode, or builds a new instance). - Emits
search_engine.operations.*events; payloads are redacted and safe for dashboards/health checks.
Client helper (SearchEngine.client)
Use SearchEngine.client to access the configured client instance. It follows
this precedence:
- Returns
SearchEngine.config.clientif explicitly set (e.g.,StubClient) - Returns
SearchEngine::Test::OfflineClientiftest_mode?is true - Otherwise creates and returns a new
SearchEngine::Clientinstance
Request flow
URL/common params vs body
| Parameter | Location |
|---|---|
use_cache | URL/Common |
cache_ttl | URL/Common |
q | Body/Params |
query_by | Body/Params |
filter_by | Body/Params |
per_page | Body/Params |
Errors
Public errors are exposed viaSearchEngine::Errors:
Timeout: request exceeded timeout budgetConnection: DNS/socket/TLS/connect failuresApi: non‑2xx Typesense responses (carriesstatusandbody)InvalidParams: wrapper pre‑call validation problems
lib/search_engine/errors.rb for details.
Internals boundary
SearchEngine::Client::RequestBuilderassembles concrete request shapes (HTTP method, path, body) from compiled params. It has no network concerns.SearchEngine::Client::HttpAdapterexecutes an assembled request using the injectedTypesense::Client. It has no knowledge of Typesense domain semantics.- These roles clarify responsibilities; there is no change to public behavior.
Troubleshooting
- Timeout / Connection: Check host/port/protocol and network reachability.
- API errors: Inspect
statusand server body. 4xx are not retried; 5xx/429 may be transient.
API Reference
Collection Management
create_collection(schema): Create a physical collection.update_collection(name, schema): PATCH an existing collection (add/drop fields) without reindexing when Typesense deems the change compatible.delete_collection(name, timeout_ms: nil): Delete a collection.retrieve_collection_schema(name): Fetch schema for a physical collection.list_collections: List all collections.upsert_alias(alias, physical): Point an alias to a physical collection.resolve_alias(alias): Get the physical name for an alias.
Document Operations
create_document(collection:, document:): Create a single document.retrieve_document(collection:, id:): Fetch a document by ID.update_document(collection:, id:, fields:): Partially update a document.delete_document(collection:, id:): Delete a document by ID.import_documents(collection:, jsonl:, action: :upsert): Bulk import.delete_documents_by_filter(collection:, filter_by:): Delete by query.update_documents_by_filter(collection:, filter_by:, fields:): Update by query.
Server Operations
health: Check server health.metrics: Retrieve metrics (JSON).stats: Retrieve stats (JSON).list_api_keys: List configured API keys.clear_cache: Clear server-side cache.
Synonyms & Stopwords
-
synonyms_upsert(collection:, id:, terms:) -
synonyms_list(collection:) -
synonyms_get(collection:, id:) -
synonyms_delete(collection:, id:) -
stopwords_upsert(collection:, id:, terms:) -
stopwords_list(collection:) -
stopwords_get(collection:, id:) -
stopwords_delete(collection:, id:)