indexing

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package indexing implements a LangChain-style indexing module that tracks which documents have already been indexed, so unchanged content is not re-written to a vector store on repeat runs. It mirrors langchain's langchain.indexes module.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContentHash

func ContentHash(doc Document) string

ContentHash computes a stable hash of a document's content and metadata. The metadata keys are sorted so the hash is order-independent.

Types

type DocHash

type DocHash struct {
	// ID of the document.
	ID string
	// ContentHash is the hash of the document content and metadata.
	ContentHash string
}

DocHash ties a document id to its content hash.

type Document

type Document struct {
	// ID is an optional stable identifier for the document.
	ID string
	// PageContent is the text of the document.
	PageContent string
	// Metadata holds arbitrary metadata used in the content hash.
	Metadata map[string]any
}

Document is a single document to be indexed.

type InMemoryRecordManager

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

InMemoryRecordManager is a thread-safe RecordManager backed by a map.

func NewInMemoryRecordManager

func NewInMemoryRecordManager() *InMemoryRecordManager

NewInMemoryRecordManager builds an empty in-memory record manager.

func (*InMemoryRecordManager) Delete

func (m *InMemoryRecordManager) Delete(_ context.Context, namespace string, ids []string) error

Delete implements RecordManager.

func (*InMemoryRecordManager) Exists

func (m *InMemoryRecordManager) Exists(_ context.Context, namespace string, id string) (bool, error)

Exists implements RecordManager.

func (*InMemoryRecordManager) GetHash

func (m *InMemoryRecordManager) GetHash(_ context.Context, namespace string, id string) (string, error)

GetHash implements RecordManager.

func (*InMemoryRecordManager) Keys

func (m *InMemoryRecordManager) Keys(_ context.Context, namespace string) ([]string, error)

Keys implements RecordManager.

func (*InMemoryRecordManager) Update

func (m *InMemoryRecordManager) Update(_ context.Context, namespace string, docs []DocHash) error

Update implements RecordManager.

type InMemoryVectorStore

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

InMemoryVectorStore is a simple VectorStore for tests.

func NewInMemoryVectorStore

func NewInMemoryVectorStore() *InMemoryVectorStore

NewInMemoryVectorStore builds an empty in-memory vector store.

func (*InMemoryVectorStore) AddDocuments

func (s *InMemoryVectorStore) AddDocuments(_ context.Context, docs []Document) ([]string, error)

AddDocuments implements VectorStore.

func (*InMemoryVectorStore) Count

func (s *InMemoryVectorStore) Count() int

Count returns the number of stored documents.

func (*InMemoryVectorStore) Delete

func (s *InMemoryVectorStore) Delete(_ context.Context, ids []string) error

Delete implements VectorStore.

type IndexResult

type IndexResult struct {
	// Added documents newly indexed.
	Added []Document
	// Updated documents whose content changed.
	Updated []Document
	// Skipped documents already up to date.
	Skipped []Document
	// Deleted documents removed from the store.
	Deleted []string
}

IndexResult reports the outcome of an indexing run.

type Indexer

type Indexer struct {
	// RecordManager tracks indexed documents.
	RecordManager RecordManager
	// Store is the destination vector store.
	Store VectorStore
	// Namespace separates different indexes.
	Namespace string
	// CleanupMode controls handling of documents no longer present. "incremental"
	// deletes store entries whose ids are no longer in the source.
	CleanupMode string
	// WithID assigns ids to documents. If nil, the content hash is used as id.
	WithID func(doc Document) string
}

Indexer coordinates document indexing against a RecordManager and a VectorStore.

func (*Indexer) Run

func (ix *Indexer) Run(ctx context.Context, documents []Document) (*IndexResult, error)

Run indexes the given documents, returning a summary. It only writes documents whose content hash changed and (optionally) prunes stale entries.

type RecordManager

type RecordManager interface {
	// Update records the given document hashes as indexed.
	Update(ctx context.Context, namespace string, docs []DocHash) error
	// Exists reports whether a document id with the given content hash is
	// already indexed.
	Exists(ctx context.Context, namespace string, id string) (bool, error)
	// GetHash returns the stored content hash for an id, or "" if absent.
	GetHash(ctx context.Context, namespace string, id string) (string, error)
	// Delete removes document ids from the index.
	Delete(ctx context.Context, namespace string, ids []string) error
	// Keys returns all indexed ids in the namespace.
	Keys(ctx context.Context, namespace string) ([]string, error)
}

RecordManager persists the set of indexed document IDs and their content hashes for a given namespace.

type VectorStore

type VectorStore interface {
	// AddDocuments stores documents and returns their ids.
	AddDocuments(ctx context.Context, docs []Document) ([]string, error)
	// Delete removes documents by id.
	Delete(ctx context.Context, ids []string) error
}

VectorStore is the minimal vector store interface the indexer writes to.

Jump to

Keyboard shortcuts

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