postgres

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package postgres provides a hybrid index backed by PostgreSQL, combining native full-text search (tsvector + ts_rank) with vector similarity search (pgvector, cosine distance). Results from both legs are fused with Reciprocal Rank Fusion.

The vector leg is only active when an llm.Client is provided; without one the index degrades gracefully to full-text search only. In both cases the target database must have the `vector` and `unaccent` extensions available (creating them requires sufficient privileges, e.g. the pgvector/pgvector Docker images).

Index

Constants

View Source
const DefaultVectorSize int = 768

DefaultVectorSize is the default dimension of the pgvector column, consistent with the sqlitevec backend.

Variables

This section is empty.

Functions

This section is empty.

Types

type Index

type Index struct {
	// contains filtered or unexported fields
}

func NewIndex

func NewIndex(pool *pgxpool.Pool, client llm.Client, funcs ...OptionFunc) *Index

NewIndex creates a hybrid PostgreSQL index on top of the given pool. The pool remains owned by the caller. A nil llm.Client disables the vector leg (full-text search only).

func (*Index) All

func (i *Index) All(ctx context.Context, yield func(model.SectionID) bool) error

All implements index.Index.

func (*Index) DeleteByID

func (i *Index) DeleteByID(ctx context.Context, ids ...model.SectionID) error

DeleteByID implements index.Index.

func (*Index) DeleteBySource

func (i *Index) DeleteBySource(ctx context.Context, source *url.URL) error

DeleteBySource implements index.Index.

func (*Index) GenerateSnapshot

func (i *Index) GenerateSnapshot(ctx context.Context) (io.ReadCloser, error)

GenerateSnapshot implements backup.Snapshotable.

func (*Index) Index

func (i *Index) Index(ctx context.Context, document model.Document, funcs ...index.OptionFunc) error

Index implements index.Index.

func (*Index) RestoreSnapshot

func (i *Index) RestoreSnapshot(ctx context.Context, r io.Reader) error

RestoreSnapshot implements backup.Snapshotable.

func (*Index) Search

func (i *Index) Search(ctx context.Context, query string, opts index.SearchOptions) ([]*index.SearchResult, error)

Search implements index.Index.

type OptionFunc

type OptionFunc func(opts *Options)

func WithEmbeddingsModel

func WithEmbeddingsModel(model string) OptionFunc

func WithMaxWords

func WithMaxWords(maxWords int) OptionFunc

func WithTextSearchConfig

func WithTextSearchConfig(config string) OptionFunc

func WithVectorSize

func WithVectorSize(size int) OptionFunc

type Options

type Options struct {
	// EmbeddingsModel identifies the embeddings model; snapshots generated
	// with a different model are rejected on restore.
	EmbeddingsModel string
	// VectorSize is the dimension of the pgvector column. Larger embeddings
	// are truncated then re-normalized (matryoshka-style).
	VectorSize int
	// MaxWords bounds the size of the chunks sent to the embeddings model.
	MaxWords int
	// TextSearchConfig is the regconfig used when language detection is
	// inconclusive (default "simple").
	TextSearchConfig string
}

func NewOptions

func NewOptions(funcs ...OptionFunc) *Options

type SnapshottedMetadata

type SnapshottedMetadata struct {
	Model      string
	VectorSize int
}

type SnapshottedRecord

type SnapshottedRecord struct {
	Source     string
	SectionID  string
	ChunkIndex int
	Content    string
	Lang       string
	// Embedding is the pgvector text literal ("[0.1,...]"), empty when the
	// chunk has no embedding.
	Embedding   string
	Collections []string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL