index

package
v0.0.0-...-683b713 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package index defines a top-level index interface for executing search queries against a collection of documents. Contains helpers for searching multiple indexes at once.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTurbopufferClient

func NewTurbopufferClient(apiKey string) *turbopuffer.Client

NewTurbopufferClient creates a new Turbopuffer client from an API key.

Types

type AttributesSelector

type AttributesSelector []string

AttributesSelector is a list of attributes to include in search results. If nil, all attributes are included. If empty, no attributes are included.

type DeleteSelector

type DeleteSelector struct {
	IDs    []DocumentID
	Filter *Filter
}

DeleteSelector is a selector for deleting documents from an index.

type DistanceMetric

type DistanceMetric int

DistanceMetric is an enumeration over the various supported distance metrics.

const (
	CosineDistance DistanceMetric = iota
)

Supported distance metrics.

func (DistanceMetric) ComputeDistance

func (dm DistanceMetric) ComputeDistance(a, b []float32) float32

type Document

type Document struct {
	ID         DocumentID
	Vector     []float32
	Attributes map[string]DocumentAttribute
}

Document is a single document within an index.

func (*Document) GetAttributes

func (d *Document) GetAttributes(selector AttributesSelector) map[string]DocumentAttribute

GetAttributes returns a map of attributes contained within a document, chosen by a selector.

func (*Document) MatchesFilter

func (d *Document) MatchesFilter(f *Filter) bool

MatchesFilter checks if a document matches a provided filter. Note that this implementation may not match the behaviour of other implementations, i.e. those provided by indexing providers.

func (*Document) MatchesFilterCondition

func (d *Document) MatchesFilterCondition(fc *FilterCondition) bool

MatchesFilterCondition checks if a document matches a provided filter condition.

type DocumentAttribute

type DocumentAttribute struct {
	String *string
	UInt64 *uint64
}

DocumentAttribute is an attached attribute to a document.

func NewDocumentAttributeString

func NewDocumentAttributeString(s string) DocumentAttribute

NewDocumentAttributeString creates a new string document attribute.

type DocumentID

type DocumentID uint64

ID is a unique identifier for a document within an index.

type Filter

type Filter struct {
	Condition *FilterCondition

	And []Filter
	Or  []Filter
}

Filter is a filter for searching documents in an index.

type FilterCondition

type FilterCondition struct {
	Key   string
	Op    FilterOp
	Input DocumentAttribute
}

FilterCondition is a single condition within a filter.

type FilterOp

type FilterOp int

An enumeration over the various supported filter operations.

const (
	FilterOpEqual FilterOp = iota
	FilterOpNotEqual
)

Supported filter operations.

type InMemory

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

func NewInMemory

func NewInMemory(dims int, distMetric DistanceMetric) *InMemory

NewInMemory creates a new in-memory index. All documents are stored in memory and are exhaustively searched for each query. Mostly used for testing etc.

func (*InMemory) Delete

func (im *InMemory) Delete(_ context.Context, selector DeleteSelector) error

func (*InMemory) Query

func (im *InMemory) Query(_ context.Context, query Query) ([]ScoredDocument, error)

func (*InMemory) Upsert

func (im *InMemory) Upsert(_ context.Context, docs []Document) error

type Index

type Index interface {
	// Upsert adds or updates the given documents in the index, replacing any existing documents
	// with the same ID. Upsert is atomic: if an error is returned, the index is unchanged.
	Upsert(ctx context.Context, docs []Document) error

	// Deletes documents according to a selector from the index.
	Delete(ctx context.Context, selector DeleteSelector) error

	// Query searches the index for documents matching the given query.
	Query(ctx context.Context, query Query) ([]ScoredDocument, error)
}

An Index contains a collection of documents and provides a way to search them.

type MultiIndex

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

MultiIndex is an index that combines multiple indexes into a single index. Upserts are sent to all indexes, as are deletes. Queries are sent to all indexes, and the results are combined and re-sorted.

func NewMultiIndex

func NewMultiIndex(indexes ...Index) *MultiIndex

NewMultiIndex creates a new multi-index that combines the given indexes into a single index.

func (*MultiIndex) Delete

func (mi *MultiIndex) Delete(ctx context.Context, selector DeleteSelector) error

func (*MultiIndex) Query

func (mi *MultiIndex) Query(ctx context.Context, query Query) ([]ScoredDocument, error)

func (*MultiIndex) Upsert

func (mi *MultiIndex) Upsert(ctx context.Context, docs []Document) error

type Query

type Query struct {
	MaxResults        int
	Vector            []float32
	Text              *string
	Filter            *Filter
	IncludeVector     bool
	IncludeAttributes AttributesSelector
}

Query is a search query for an index.

type ScoredDocument

type ScoredDocument struct {
	Document
	Score float32
}

ScoredDocument returns a document with an associated score.

type TurbopufferIndex

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

TurbopufferIndex is an indexed back by turbopuffer (https://turbopuffer.com).

func NewTurbopufferIndex

func NewTurbopufferIndex(
	client *turbopuffer.Client,
	name string,
	distMetric DistanceMetric,
	fullTextAttribute *string,
) *TurbopufferIndex

NewTurbopufferIndex creates a new TurbopufferIndex from an API client and its name.

func (*TurbopufferIndex) Delete

func (ti *TurbopufferIndex) Delete(ctx context.Context, selector DeleteSelector) error

func (*TurbopufferIndex) Query

func (ti *TurbopufferIndex) Query(ctx context.Context, query Query) ([]ScoredDocument, error)

func (*TurbopufferIndex) Upsert

func (ti *TurbopufferIndex) Upsert(ctx context.Context, docs []Document) error

Jump to

Keyboard shortcuts

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