Trains the encrypted index to optimize it for efficient similarity search queries. Training is essential for IVF-based indexes to achieve optimal query performance and accuracy.
In CyborgDB Service, training is typically handled automatically by the service. However, you can explicitly trigger training once enough vectors have been added.
func (e *EncryptedIndex) Train(ctx context.Context, params TrainParams) error

Parameters

ParameterTypeDescription
ctxcontext.ContextContext for cancellation and timeouts (training can take time)
paramsTrainParamsTraining parameters specifying optimization options

TrainParams Fields

FieldTypeDescription
BatchSize*int32(Optional) Number of vectors processed per training batch (default: 2048)
MaxIters*int32(Optional) Maximum training iterations (default: 100)
Tolerance*float64(Optional) Convergence tolerance for training (default: 1e-6)
MaxMemory*int32(Optional) Maximum memory usage in MB, 0 = no limit (default: 0)
NLists*int32(Optional) Number of IVF clusters, 0 = auto-determine (default: 0)
Training is a compute-intensive operation that may take several seconds to minutes depending on the index size and configuration.

Returns

  • error: Any error encountered during training, or nil if successful

Error Handling

Example Usage

package main

import (
    "context"
    "fmt"
    "log"
    "time"

    "github.com/cyborginc/cyborgdb-go"
)

func main() {
    client, err := cyborgdb.NewClient(cyborgdb.ClientConfig{
        BaseURL: "http://localhost:8000",
        APIKey:  "your-api-key",
    })
    if err != nil {
        log.Fatal(err)
    }

    // Load an existing index
    indexKey := "your-64-character-hex-key"
    index, err := client.LoadIndex(context.Background(), cyborgdb.LoadIndexParams{
        IndexName: "my-vector-index",
        IndexKey:  indexKey,
    })
    if err != nil {
        log.Fatal(err)
    }

    // Basic training with default parameters
    params := cyborgdb.TrainParams{}
    
    fmt.Println("Starting index training...")
    err = index.Train(context.Background(), params)
    if err != nil {
        log.Fatalf("Training failed: %v", err)
    }

    fmt.Println("Training completed successfully!")
    fmt.Printf("Index is now trained: %t\n", index.IsTrained())
}

Training with Custom Parameters

// Training with custom parameters for better control
batchSize := int32(1024)    // Smaller batches for limited memory
maxIters := int32(200)      // More iterations for better convergence
tolerance := 1e-7           // Tighter convergence tolerance
maxMemory := int32(4096)    // Limit to 4GB RAM
nLists := int32(256)        // Specific number of clusters

params := cyborgdb.TrainParams{
    BatchSize: &batchSize,
    MaxIters:  &maxIters,
    Tolerance: &tolerance,
    MaxMemory: &maxMemory,
    NLists:    &nLists,
}

// Use longer timeout for training large datasets
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()

fmt.Println("Starting training with custom parameters...")
err := index.Train(ctx, params)
if err != nil {
    if ctx.Err() == context.DeadlineExceeded {
        log.Fatal("Training timed out after 30 minutes")
    }
    log.Fatalf("Training failed: %v", err)
}

fmt.Println("Custom training completed!")