Documentation
¶
Overview ¶
Package dalgo2typesense provides a bounded, read-only DALgo adapter for native documents in an existing Typesense 30.2 collection.
Documents are read as they are stored: the Typesense string id is the DALgo record key and no DALgo envelope is required or written. Native searches are exposed through the adapter-local NativeSearcher capability, reachable with dal.As. Structured DALgo queries are translated to a deliberately small and validated subset of the Typesense search API.
Every read path disables curations (enable_curations=false) and asks the server to apply filter_by to curated hits (filter_curated_hits=true). A caller cannot override either policy. The adapter never follows redirects, never creates collections, never writes documents and never exposes raw URLs, management endpoints, multi-search, joins or RPC endpoints.
Server licensing: Typesense server is GPL-3.0. This package is an independent MIT-licensed HTTP client. It does not link, embed, copy or redistribute the Typesense server or its SDK.
Index ¶
Constants ¶
const ProvenanceSearch = "typesense-native-search"
ProvenanceSearch is the only provenance the adapter reports for a native search. It makes clear that the result came from the Typesense HTTP search API, not from a sampled document or a local recomputation.
Variables ¶
var ( // ErrRequestTooLarge reports that the encoded request exceeds the // configured byte budget. ErrRequestTooLarge = errors.New("typesense: request exceeds configured byte budget") // ErrResponseTooLarge reports that a response body or the retained row data // of one operation exceeds a configured byte budget. ErrResponseTooLarge = errors.New("typesense: response exceeds configured byte budget") // ErrRowTooLarge reports that one decoded document exceeds MaxRowBytes. ErrRowTooLarge = errors.New("typesense: document exceeds configured row byte budget") // ErrTooManyRows reports that a response or operation returned more rows // than the configured row budget. ErrTooManyRows = errors.New("typesense: result exceeds configured row budget") // ErrTooManyPages reports that completing an operation would need more // search pages than the configured page budget. ErrTooManyPages = errors.New("typesense: result exceeds configured page budget") // ErrMalformedResponse reports a response that is not the expected // Typesense JSON shape. It never includes response text. ErrMalformedResponse = errors.New("typesense: malformed response") // ErrUnsafeValue reports an identifier, field, header or constant that the // adapter will not send. It never includes the rejected value. ErrUnsafeValue = errors.New("typesense: unsafe value") // ErrPartialResult reports that the server returned fewer hits than the // response's own found count says are available. It is used for caps such // as a scoped limit_hits and for search cutoffs, so a capped result is // never presented as a clean end of data. ErrPartialResult = errors.New("typesense: partial result") // ErrDuplicateID reports that a paged search repeated a native document ID. // It prevents duplicate rows silently entering a result and detects an // unstable page boundary. ErrDuplicateID = errors.New("typesense: duplicate document ID across pages") // ErrMetadataForbidden reports that a metadata request was refused by the // credential. It is never converted into an empty collection list. ErrMetadataForbidden = errors.New("typesense: metadata is not permitted by this credential") // ErrPermissionDenied reports an HTTP 401 or 403 response. The numeric // status remains available through *HTTPError. ErrPermissionDenied = errors.New("typesense: request is not permitted") // ErrRedirectRefused reports that the server attempted a redirect. The // adapter never follows a redirect because a credential must not reach a // different origin. ErrRedirectRefused = errors.New("typesense: redirect refused") // ErrHeaderProvider reports that the configured header provider failed. // The provider's own error text is not returned because it may contain a // credential. ErrHeaderProvider = errors.New("typesense: header provider failed") // ErrRequestFailed is the safe, detail-free error returned for a transport // failure that is neither a context cancellation nor a deadline. ErrRequestFailed = errors.New("typesense: request failed") // ErrClosed reports that the adapter has been closed. ErrClosed = errors.New("typesense: adapter is closed") // ErrCollectionNotConfigured reports a structured query or configured // metadata request on an adapter without a configured collection. ErrCollectionNotConfigured = errors.New("typesense: no collection is configured") // ErrSemanticMismatch reports that a native Typesense candidate does not // satisfy the exact admitted DALgo predicate. Native string folding and // array matching make filter_by a candidate producer, never the final // semantic authority; such a candidate is refused instead of silently // dropped, exposed or retried around. The same refusal covers a widened // strict numeric boundary candidate: a strict > or < is lowered to an // inclusive >= or <= so a quantised FLOAT index cannot omit eligible raw // values, and the exact verifier then rejects the boundary row. It also // unwraps to dal.ErrNotSupported because the adapter honestly cannot // produce the requested exact result under the existing bounds. ErrSemanticMismatch = errors.New("typesense: exact structured predicate cannot be matched by the native candidate") )
The adapter returns sentinel errors for conditions callers may need to distinguish without parsing text. Every message is deliberately static: endpoints, credentials, headers, query strings and response bodies are never included in an error.
var ( ErrCanceled = fmt.Errorf("typesense: request canceled: %w", context.Canceled) ErrDeadlineExceeded = fmt.Errorf("typesense: request deadline exceeded: %w", context.DeadlineExceeded) )
ErrCanceled and ErrDeadlineExceeded preserve errors.Is semantics for context.Canceled and context.DeadlineExceeded without exposing the wrapped transport error, whose text may contain the endpoint or credentials.
Functions ¶
Types ¶
type Config ¶
type Config struct {
// Endpoint is the HTTPS origin of the Typesense server, for example
// https://typesense.example.com:8108. It must not contain credentials, a
// path, a query or a fragment. Plain HTTP is accepted only for a loopback
// host when AllowLoopbackHTTP is true.
Endpoint string
// Collection optionally pins every request to one existing collection.
// When empty, native searches may name any collection the credential can
// read, while structured DALgo queries and configured metadata are refused
// because there is no configured target.
Collection string
// Headers is called for every HTTP request and returns that request's
// credential headers (typically X-TYPESENSE-API-KEY). It is the only
// credential source. Names and values must be CR/LF/NUL-safe; hop-by-hop,
// protocol and framing headers are refused. Provider error text is never
// returned to the caller.
Headers func(ctx context.Context) (map[string]string, error)
// HTTPClient optionally supplies the client. The adapter copies it, blocks
// redirects on the copy, and never mutates the caller's client.
HTTPClient *http.Client
// AllowLoopbackHTTP permits plain HTTP only for localhost, 127.0.0.1 and
// ::1. It is a development option; production endpoints must use HTTPS.
AllowLoopbackHTTP bool
// MaxRows is the largest accepted DALgo query or native search limit.
// Default 1000. Native search is one page, so it is additionally bounded by
// MaxRowsPerPage and the server's 250-hit page limit.
MaxRows int
// MaxRowsPerPage bounds one search request. Default 100, hard maximum 250.
// The structured reader uses it as its physical page size. Native search
// and exact-id GetMulti batches refuse a request larger than this bound
// instead of silently ignoring it.
MaxRowsPerPage int
// MaxPages bounds how many search requests one DALgo query may issue.
// Default 20.
MaxPages int
// MaxOffset is the largest accepted offset. Default 10000.
MaxOffset int
// MaxGetMany is the largest accepted GetMulti batch. Default 100.
MaxGetMany int
// MaxRequestBytes bounds the encoded search query sent to the server.
// Default 64 KiB.
MaxRequestBytes int64
// MaxResponseBytes bounds one HTTP response body. Default 4 MiB.
MaxResponseBytes int64
// MaxTotalBytes bounds the cumulative response bytes of one operation,
// including metadata, paging and batches. Default 16 MiB.
MaxTotalBytes int64
// MaxRowBytes bounds one decoded native document's JSON size. Every read
// path enforces it before exposing the row: the structured reader, native
// search, Get, Exists and GetMulti. Default 1 MiB.
MaxRowBytes int64
// MaxFilterBytes bounds a native FilterBy expression and a compiled DALgo
// filter. Default 8 KiB.
MaxFilterBytes int
// MaxFacetFields bounds the number of facet_by fields. Default 5.
MaxFacetFields int
// MaxFacetValues bounds max_facet_values. Default 100.
MaxFacetValues int
// MaxColumns bounds a structured projection and a query_by field list.
// Default 100.
MaxColumns int
// MaxSortFields bounds sort fields. Typesense accepts at most 3. Default 3.
MaxSortFields int
// Timeout bounds one operation, including every page and request in it.
// Default 30s.
Timeout time.Duration
}
Config configures one adapter instance. The adapter addresses an existing Typesense server; it never creates collections or writes documents.
type Facet ¶
type Facet struct {
Field string
Counts []FacetValue
Stats *FacetStats
}
Facet is a native facet result. Counts are native facet statistics, not SQL aggregates.
type FacetStats ¶
FacetStats retains the native stats object when Typesense returns one. The numeric fields keep the exact JSON class: an integral statistic becomes int64 or uint64 and a fractional one becomes float64. Stats are never presented as SQL aggregates.
type FacetValue ¶
FacetValue is one native facet value count.
type GetMultiError ¶
GetMultiError reports which item of a GetMulti batch stopped the batch. Records before Index are populated; the record at Index and all later records are not. It unwraps to the underlying cause, so errors.Is and errors.As keep working for sentinels and record.ErrRecordNotFound.
func (*GetMultiError) Error ¶
func (e *GetMultiError) Error() string
func (*GetMultiError) Unwrap ¶
func (e *GetMultiError) Unwrap() error
type HTTPError ¶
type HTTPError struct {
StatusCode int
}
HTTPError is a safe Typesense HTTP status error. Only the numeric status is retained; response headers and bodies are discarded.
type NativeSearcher ¶
type NativeSearcher interface {
Search(ctx context.Context, request SearchRequest) (SearchResult, error)
}
NativeSearcher is the adapter-local native Typesense search capability. Obtain it with dal.As[NativeSearcher](db). It is deliberately not a DALgo core interface.
type SearchRequest ¶
type SearchRequest struct {
// Collection is an existing collection. When Config.Collection is set it
// must match it exactly; an empty value uses the configured collection.
Collection string
// Q is the native query text. "*" is the wildcard query.
Q string
// QueryBy lists full-text fields. It is required unless Q is "*".
QueryBy []string
// FilterBy is a bounded native Typesense filter expression. It is not a
// URL and it is validated before any request. Currency, joins, object
// filters and other unsupported grammar are refused.
FilterBy string
// SortBy lists simple application fields and directions. Typesense accepts
// at most three. No id sort is invented.
SortBy []SortField
// FacetBy lists facet-enabled fields.
FacetBy []string
// MaxFacetValues bounds the returned facet values. Zero uses the smaller
// of 10 and Config.MaxFacetValues; it can never exceed
// Config.MaxFacetValues.
MaxFacetValues int
// Limit is the positive number of hits to return. It is bounded by
// Config.MaxRows, Config.MaxRowsPerPage and the server's 250-hit page
// limit. Zero is refused.
Limit int
// Offset is the non-negative hit offset.
Offset int
}
SearchRequest is one bounded Typesense search. It has no arbitrary option bag, raw URL, management endpoint, multi-search, join or RPC surface.
type SearchResult ¶
type SearchResult struct {
Collection string
Hits []SearchHit
Facets []Facet
Found int
OutOf int
Page int
Returned int
SearchTimeMS int
Partial bool
Complete bool
Truncated bool
Provenance string
}
SearchResult is the bounded native result. Found and OutOf are server metadata, distinct from the hits actually returned. Partial and Complete are explicit: a server cap or cutoff is never presented as a clean end of data. A Typesense search_cutoff response indicator sets Partial and clears Complete even when the cutoff page returned zero hits and found is zero.