Documentation
¶
Overview ¶
Package typesense ranks precomputed vectors in a provisioned Typesense collection. Include SpaceField(space) beside the stored content, opaque metadata and cosine embedding fields. An index without this exact marker must be rebuilt; native automatic embedding is rejected.
Query uses native semantic ranking or native lexical/vector fusion. Supply lexical text explicitly in hybrid mode. Ordinary queries do not export the collection. Native filters on opaque Core JSON cannot guarantee equivalent results; Query therefore rejects them before model or provider I/O. QueryByScan instead requires explicit authority and a record, byte and time budget. The export and ranking calls do not share snapshot isolation.
Upsert preflights the complete batch before importing. It requests native IDs in each acknowledgment, validates them against the actual submitted keys, and keeps accepted, rejected and uncertain records distinct. An acknowledgment does not imply that a read replica can search the write.
Index ¶
- Constants
- Variables
- func SpaceField(space vectorstore.SpaceID) api.Field
- type APIClient
- type Store
- func (s *Store) CheckDocuments(ctx context.Context, space vectorstore.SpaceID, docs []*document.Document) error
- func (s *Store) CheckQuery(ctx context.Context, space vectorstore.SpaceID, opts vectorstore.SearchOptions) error
- func (s *Store) DeleteIDs(ctx context.Context, ids []string) error
- func (s *Store) Query(ctx context.Context, q *vectorstore.Query) (*vectorstore.SearchResponse, error)
- func (s *Store) QueryByScan(ctx context.Context, q *vectorstore.Query, budget vectorstore.ScanBudget) (*vectorstore.SearchResponse, error)
- func (s *Store) Upsert(ctx context.Context, batch *vectorstore.Batch) error
- type StoreConfig
Examples ¶
Constants ¶
const ( Provider = "Typesense" DefaultCollectionName = "scope_vector_store" DefaultMaxResponseBytes = int64(16 * 1024 * 1024) MaxResultsPerPage = 250 )
Variables ¶
var ErrIncompatibleCollection = errors.New("typesense: collection is incompatible")
Functions ¶
func SpaceField ¶ added in v0.59.0
func SpaceField(space vectorstore.SpaceID) api.Field
SpaceField binds a provisioned collection to its immutable encoder space.
Types ¶
type APIClient ¶ added in v0.43.0
type APIClient interface {
GetCollection(context.Context, string, ...api.RequestEditorFn) (*http.Response, error)
ExportDocuments(context.Context, string, *api.ExportDocumentsParams, ...api.RequestEditorFn) (*http.Response, error)
ImportDocumentsWithBody(context.Context, string, *api.ImportDocumentsParams, string, io.Reader, ...api.RequestEditorFn) (*http.Response, error)
MultiSearchWithBody(context.Context, *api.MultiSearchParams, string, io.Reader, ...api.RequestEditorFn) (*http.Response, error)
DeleteDocuments(context.Context, string, *api.DeleteDocumentsParams, ...api.RequestEditorFn) (*http.Response, error)
}
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
func NewStore ¶
func NewStore(ctx context.Context, config StoreConfig) (*Store, error)
Example ¶
package main
import (
"context"
"net/http"
"os"
"time"
"github.com/typesense/typesense-go/v3/typesense/api"
"github.com/Tangerg/scope/core/document"
"github.com/Tangerg/scope/core/vectorstore"
"github.com/Tangerg/scope/vectorstores/typesense"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
transport := http.DefaultTransport.(*http.Transport).Clone()
defer transport.CloseIdleConnections()
key := os.Getenv("SCOPE_TYPESENSE_API_KEY")
client, err := api.NewClient(os.Getenv("SCOPE_TYPESENSE_ENDPOINT"), api.WithHTTPClient(&http.Client{Transport: transport, Timeout: 30 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}), api.WithRequestEditorFn(func(_ context.Context, request *http.Request) error {
request.Header.Set("X-TYPESENSE-API-KEY", key)
return nil
}))
if err != nil {
panic(err)
}
const space vectorstore.SpaceID = "encoder-v1"
store, err := typesense.NewStore(ctx, typesense.StoreConfig{Client: client, CollectionName: "documents", Space: space})
if err != nil {
panic(err)
}
if err = store.Upsert(ctx, &vectorstore.Batch{Space: space, Records: []vectorstore.Record{{Document: &document.Document{ID: "one", Text: "example"}, Values: []float64{1, 0}}}}); err != nil {
panic(err)
}
if _, err = store.Query(ctx, &vectorstore.Query{Space: space, Values: []float64{1, 0}}); err != nil {
panic(err)
}
if err = store.DeleteIDs(ctx, []string{"one"}); err != nil {
panic(err)
}
}
Output:
func (*Store) CheckDocuments ¶ added in v0.59.0
func (*Store) CheckQuery ¶ added in v0.59.0
func (s *Store) CheckQuery(ctx context.Context, space vectorstore.SpaceID, opts vectorstore.SearchOptions) error
func (*Store) Query ¶ added in v0.59.0
func (s *Store) Query(ctx context.Context, q *vectorstore.Query) (*vectorstore.SearchResponse, error)
func (*Store) QueryByScan ¶ added in v0.60.0
func (s *Store) QueryByScan(ctx context.Context, q *vectorstore.Query, budget vectorstore.ScanBudget) (*vectorstore.SearchResponse, error)
QueryByScan explicitly authorizes an export of the entire collection to compute exact membership. MaxBytes bounds the JSONL source payload, not ANN pages. The collection can change between export and ranking; membership is rechecked.
type StoreConfig ¶
type StoreConfig struct {
Client APIClient
CollectionName string
Space vectorstore.SpaceID
HybridAlpha *float32
MaxResponseBytes int64
}
func (StoreConfig) Validate ¶
func (s StoreConfig) Validate() error