Configuration
Set a default embedding model once in your initializer. Everyembedding declaration that does not specify its own model inherits this value.
Schema: the embedding macro
Declare embedding fields on your model with the embedding macro. It registers a :vector attribute internally and builds the Typesense embed block (or num_dim for external embeddings) at schema compile time.
Basic forms
Unindexed embed sources
If an embed source field is only needed for embedding (not for keyword search, filtering, or sorting), declare it withindex: false. The schema compiler detects the from: reference and emits the field with Typesense-native "index": false instead of omitting it entirely. This avoids keyword-indexing overhead and the auto-generated *_blank hidden field.
brand_name as { "name": "brand_name", "type": "string", "index": false, "optional": true }. Typesense stores the value on disk and feeds it to the embedding model, but it consumes no in-memory index space.
External embeddings
When your application generates vectors outside Typesense, declarenum_dim: without from:. The mapper expects your map block to supply a float array of the declared dimension.
embed block is emitted in the schema; Typesense stores the field as float[] with the given dimension constraint.
Name resolution rules
Model resolution precedence: per-field
model: > config.embedding.model > raises ConfigurationError.
from: inference: when from: is omitted and the field is not external, the macro infers from: [bare_name] where bare_name is the positional argument before the _embedding suffix.
Macro signature
Semantic search
When the query string is not"*" and no explicit query: vector is provided, Typesense auto-embeds the query text with the same model used at index time and performs nearest-neighbor search.
product_embedding to query_by so Typesense performs rank fusion between keyword and vector results.
Field auto-resolution
When a model declares exactly one embedding, thefield argument can be omitted entirely. The gem resolves it automatically:
vector_search act as a behaviour modifier — just chain it to enable semantic/hybrid mode.
When k: is omitted, Typesense applies its server default (10). You only need to specify k: when you want a different value.
Hybrid search
Combine keyword and vector results with an explicit alpha weight. Typesense uses rank fusion:Find similar documents
find_similar is sugar over vector_search with id:. Typesense retrieves the stored embedding for the given document and finds its nearest neighbors.
field: can be omitted:
Historical queries
Weight multiple past queries and let Typesense blend their embeddings server-side.weights: must sum to approximately 1.0 (tolerance: 0.01). The gem validates this and raises InvalidVectorQuery if violated.External vector queries
When you generate embeddings in your own pipeline, pass the float array directly.Sort by vector distance
Useorder(vector_distance: :asc) as a secondary sort to rank keyword results by their semantic proximity.
vector_distance to the Typesense token _vector_query(product_embedding:([])):asc.
Distance threshold
Cap results by cosine distance to filter out low-relevance matches.HNSW tuning
Override HNSW search parameters per query.Auto-exclude behaviour
Embedding fields contain large float arrays (384—1536 dimensions). To avoid inflating response payloads, the compiler automatically adds the embedding field toexclude_fields unless you explicitly select it.
To include the raw vectors in the response:
Multi-search
Vector search works insidemulti_search blocks:
Compiler mapping
The compiler:- Builds the
vector_querystring:product_embedding:([], k:200, alpha:0.8) - Auto-appends the embedding field to
query_byin hybrid mode (text query + vector search, no explicitquery:array) - Auto-adds the embedding field to
exclude_fieldsunless explicitly selected - Resolves
order(vector_distance: ...)to the real Typesense sort token
DX & explain
All DX helpers include vector search state. Raw float arrays are redacted to[<N dims>] in all surfaces.
Method signatures
vector_search
Mutually exclusive modes:
query:, id:, and queries:. Providing more than one raises InvalidVectorQuery. Last vector_search call wins (Typesense supports one vector_query per search).
find_similar
vector_search(field, id: document_id, ...). When field: is omitted, the sole embedding on the model is used automatically (same resolution as vector_search).
Observability
The compiler emits asearch_engine.vector.compile event with:
Compact logging redacts raw vector arrays. OTel spans include these attributes when enabled.
See Observability for event payloads and log format.
Backlinks
- Configuration —
config.embedding.*settings - Models —
embeddingmacro declaration - Relation — immutable chaining
- Compiler —
vector_querycompilation details - DX —
explain,dry_run!,to_curl - Observability — events and redaction
- Typesense Vector Search docs