Adds new vectors to the index or updates existing ones. Vector data is encrypted end-to-end before being stored.
func (e *EncryptedIndex) Upsert(ctx context.Context, items []VectorItem) error

Parameters

ParameterTypeDescription
ctxcontext.ContextContext for cancellation/timeouts
items[]VectorItemSlice of vector items to add or update

VectorItem

type VectorItem struct {
    Id       string                 `json:"id"`                // Unique identifier
    Vector   []float32              `json:"vector,omitempty"`  // Vector data
    Contents NullableContents       `json:"contents,omitempty"` // Optional text content
    Metadata map[string]interface{} `json:"metadata,omitempty"` // Optional metadata
}

Returns

TypeDescription
errorAny error encountered during the upsert operation

Exceptions

Example Usage

package main

import (
    "context"
    "encoding/hex"
    "log"
    
    "github.com/cyborginc/cyborgdb-go"
)

func main() {
    // Load existing index
    client, _ := cyborgdb.NewClient("http://localhost:8000", "your-api-key")
    key := []byte("example-index-key-1234567890") // Replace with your actual encryption key
    index, _ := client.LoadIndex(context.Background(), "my-documents", key)
    
    // Create vector items
    items := []cyborgdb.VectorItem{
        {
            Id:     "doc1",
            Vector: []float32{0.1, 0.2, 0.3, 0.4, 0.5},
        },
        {
            Id:     "doc2",
            Vector: []float32{0.6, 0.7, 0.8, 0.9, 1.0},
        },
    }
    
    // Upsert vectors
    ctx := context.Background()
    err := index.Upsert(ctx, items)
    if err != nil {
        log.Fatal("Upsert failed:", err)
    }
    
    log.Printf("Successfully upserted %d vectors", len(items))
}

Upsert with Metadata

func upsertDocuments(index *cyborgdb.EncryptedIndex) error {
    items := []cyborgdb.VectorItem{
        {
            Id:     "article_123",
            Vector: []float32{0.1, 0.2, 0.3, 0.4}, // Document embedding
            Metadata: map[string]interface{}{
                "title":     "Go Programming Guide",
                "category":  "programming",
                "author":    "John Doe",
                "published": "2024-01-15",
                "tags":      []string{"golang", "tutorial", "beginner"},
            },
        },
        {
            Id:     "article_124", 
            Vector: []float32{0.5, 0.6, 0.7, 0.8},
            Metadata: map[string]interface{}{
                "title":     "Advanced Go Patterns",
                "category":  "programming", 
                "author":    "Jane Smith",
                "published": "2024-01-20",
                "tags":      []string{"golang", "advanced", "patterns"},
            },
        },
    }
    
    ctx := context.Background()
    return index.Upsert(ctx, items)
}