Rychlý start: Vektorový index s Go v Azure DocumentDB

Tento rychlý start vás provede vytvořením aplikace Go, která porovnává všechny tři algoritmy indexu vektorů (DiskANN, HNSW a IVF) vedle různých funkcí podobnosti, které vám pomůžou zvolit nejlepší konfiguraci pro vaši úlohu. Ukázka používá datovou sadu hotelů s předem vypočítanými embeddingy z modelu text-embedding-3-small.

Najděte vzorový kód na GitHubu.

Prerequisites

  • Předplatné služby Azure. Pokud ještě nemáte předplatné Azure, vytvořte si bezplatný účet.
  • Azure Developer CLI (volitelné). Slouží azd up k nasazení všech požadovaných Azure prostředků v jednom příkazu.

  • Go 1.24 nebo vyšší

Vytvoření projektu Go

  1. Vytvořte pro projekt nový adresář a otevřete ho v editoru Visual Studio Code:

    mkdir select-algorithm-go
    cd select-algorithm-go
    code .
    

  1. Inicializace nového modulu Go:

    go mod init documentdb-vector-samples
    

    Ověřte, že se modul inicializoval:

    cat go.mod
    

  1. Nainstalujte požadované balíčky:

    go get github.com/Azure/azure-sdk-for-go/sdk/azcore@v1.20.0
    go get github.com/Azure/azure-sdk-for-go/sdk/azidentity@v1.13.1
    go get github.com/openai/openai-go/v3@v3.12.0
    go get go.mongodb.org/mongo-driver@v1.17.6
    go mod tidy
    
    • azcore: Základní funkce Azure SDK pro Go.
    • azidentity: Knihovna Azure Identity pro ověřování bez hesla pomocí DefaultAzureCredential.
    • openai-go/v3: Klientská knihovna OpenAI s podpora Azure pro generování embeddingů.
    • mongo-driver: Oficiální ovladač MongoDB pro Go pro práci s DocumentDB.

    Ověřte, že jsou balíčky nainstalované:

    go list -m all | grep mongo
    

  1. Vytvořte src adresář:

    mkdir src
    

Vytvoření datového souboru s vektory

  1. Vytvořte nový datový adresář pro datový soubor hotelů:

    mkdir data
    

  1. Stáhněte si nezpracovaný Hotels_Vector.jsondatový soubor s vektory do adresáře data :

    curl -o data/Hotels_Vector.json https://raw.githubusercontent.com/Azure-Samples/documentdb-samples/refs/heads/main/ai/data/Hotels_Vector.json
    

Ověřte, že se soubor stáhl:

ls data/Hotels_Vector.json

Měli byste vidět Hotels_Vector.json v adresáři data .

Konfigurace proměnných prostředí

Před spuštěním ukázky nastavte požadované proměnné prostředí v aktuální relaci prostředí:

export DOCUMENTDB_CLUSTER_NAME=<your-cluster-name>
export AZURE_OPENAI_EMBEDDING_ENDPOINT=https://<your-resource>.openai.azure.com
export AZURE_OPENAI_EMBEDDING_MODEL=text-embedding-3-small
export AZURE_DOCUMENTDB_DATABASENAME=Hotels
export DATA_FILE_WITH_VECTORS=data/Hotels_Vector.json
export EMBEDDED_FIELD=DescriptionVector
export EMBEDDING_DIMENSIONS=1536

Pro ověřování bez hesla v tomto článku nahraďte zástupné hodnoty v aktuální relaci prostředí vlastními informacemi:

  • AZURE_OPENAI_EMBEDDING_ENDPOINT: Adresa URL koncového bodu prostředku Azure OpenAI
  • DOCUMENTDB_CLUSTER_NAME: Název clusteru DocumentDB Azure

Upřednostněte ověřování bez hesla. Další informace o nastavení spravované identity a úplného rozsahu možností ověřování najdete v tématu Ověřování aplikací Go do Azure služeb pomocí Azure SDK for Go.

Vytvořte soubory kódu

Vytvořte hlavní soubor aplikace:

touch src/main.go

Po dokončení by struktura projektu měla vypadat takto:

select-algorithm-go/
├── data/
│   └── Hotels_Vector.json
├── src/
│   ├── compare_all.go
│   ├── main.go
│   └── utils.go
└── go.mod

Vytvořte srovnávací kód algoritmu

V adresáři src vytvořte následující zdrojové soubory.

src/main.go

package main

import (
    "context"
    "fmt"
    "log"
)

func main() {
    fmt.Println("Starting vector algorithm comparison...")

    ctx := context.Background()
    config, err := LoadConfig()
    if err != nil {
        log.Fatalf("Invalid configuration: %v", err)
    }

    fmt.Println("\nInitializing clients with passwordless authentication...")
    mongoClient, azureOpenAIClient, err := GetClientsPasswordless(ctx, config)
    if err != nil {
        log.Fatalf("Failed to initialize clients: %v", err)
    }
    defer mongoClient.Disconnect(ctx)

    err = RunCompareAll(ctx, config, mongoClient, azureOpenAIClient)
    if err != nil {
        log.Fatalf("Compare all failed: %v", err)
    }

    fmt.Println("\nComparison completed successfully!")
}

src/compare_all.go

package main

import (
    "context"
    "fmt"
    "math"
    "strconv"
    "strings"
    "time"

    "github.com/openai/openai-go/v3"
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo"
)

// CompareResult holds the result of a single algorithm+metric search
type CompareResult struct {
    Algorithm string
    Metric    string
    Results   []SearchResult
    Top1Name  string
    Top1Score float64
    Top2Name  string
    Top2Score float64
    Error     error
}

// indexSpec defines one of the 9 combinations
type indexSpec struct {
    Algorithm string
    Kind      string
    Metric    string
    IndexName string
    Options   bson.D
}

// RunCompareAll executes all 9 algorithm×metric combinations on a single collection
func RunCompareAll(ctx context.Context, config *Config, dbClient *mongo.Client, aiClient openai.Client) error {
    queryText := getEnvOrDefault("QUERY_TEXT", "luxury hotel near the beach")
    topK, _ := strconv.Atoi(getEnvOrDefault("TOP_K", "5"))

    fmt.Println("\n" + strings.Repeat("=", 70))
    fmt.Println("  COMPARE ALL: 3 Algorithms × 3 Similarity Metrics (9 combinations)")
    fmt.Println(strings.Repeat("=", 70))
    fmt.Printf("Query:  %q\n", queryText)
    fmt.Printf("Top-K:  %d\n", topK)

    // 1. Drop collection for clean comparison, then load data
    database := dbClient.Database(config.DatabaseName)
    collection := database.Collection("hotels")

    // Drop existing collection for a clean comparison
    if err := collection.Drop(ctx); err != nil {
        fmt.Printf("Note: could not drop collection (may not exist): %v\n", err)
    } else {
        fmt.Println("Dropped existing 'hotels' collection")
    }

    // Ensure cleanup on exit
    defer func() {
        fmt.Println("\nCleanup: dropping comparison collection...")
        if dropErr := collection.Drop(ctx); dropErr != nil {
            fmt.Printf("Cleanup warning: %v\n", dropErr)
        } else {
            fmt.Println("Cleanup: dropped collection 'hotels'")
        }
    }()

    fmt.Printf("\nLoading data from %s...\n", config.DataFile)
    data, err := ReadFileReturnJSON(config.DataFile)
    if err != nil {
        return fmt.Errorf("failed to load data: %v", err)
    }

    documentsWithEmbeddings := FilterDocumentsWithEmbeddings(data, config.VectorField)
    if len(documentsWithEmbeddings) == 0 {
        return fmt.Errorf("no documents found with embeddings in field '%s'", config.VectorField)
    }
    fmt.Printf("Loaded %d documents with embeddings\n", len(documentsWithEmbeddings))

    stats, err := PrepareCollection(ctx, collection, documentsWithEmbeddings, config.BatchSize)
    if err != nil {
        return err
    }
    fmt.Printf("Insertion completed: %d inserted, %d failed\n", stats.Inserted, stats.Failed)

    // 2. Generate ONE embedding for the query (reused for all 9 searches)
    fmt.Printf("\nGenerating embedding for query: %q\n", queryText)
    queryEmbedding, err := GenerateEmbedding(ctx, aiClient, queryText, config.ModelName)
    if err != nil {
        return fmt.Errorf("failed to generate query embedding: %v", err)
    }
    fmt.Printf("Embedding generated (%d dimensions)\n", len(queryEmbedding))

    // 3. Define all 9 index specs
    metrics := []string{"COS", "L2", "IP"}
    specs := buildIndexSpecs(config.VectorField, config.Dimensions, metrics)

    // 4. Create→search→drop each index sequentially (DocumentDB only allows one vector index per field)
    fmt.Printf("\nRunning %d vector index comparisons (create→search→drop)...\n", len(specs))
    var results []CompareResult
    successfulComparisons := 0
    failedComparisons := 0

    for _, spec := range specs {
        // Drop all existing vector indexes on this field
        DropVectorIndexes(ctx, collection, config.VectorField)

        // Create this specific index with retry (drop may still be in progress)
        var createErr error
        for attempt := 0; attempt < 3; attempt++ {
            if attempt > 0 {
                time.Sleep(3 * time.Second)
            }
            createErr = createNamedVectorIndex(ctx, collection, config.VectorField, spec)
            if createErr == nil {
                break
            }
        }
        if createErr != nil {
            results = append(results, CompareResult{
                Algorithm: spec.Algorithm,
                Metric:    spec.Metric,
                Error:     createErr,
            })
            failedComparisons++
            fmt.Printf("  ⚠ %s: %v\n", spec.IndexName, createErr)
            continue
        }
        fmt.Printf("  ✓ %s created\n", spec.IndexName)

        // Search using simple cosmosSearch with bounded retry for index readiness.
        searchResults, searchErr := runVectorSearchWithRetry(ctx, collection, queryEmbedding, config.VectorField, topK)

        top1Name, top1Score := extractResult(searchResults, 0)
        top2Name, top2Score := extractResult(searchResults, 1)

        cr := CompareResult{
            Algorithm: spec.Algorithm,
            Metric:    spec.Metric,
            Results:   searchResults,
            Top1Name:  top1Name,
            Top1Score: top1Score,
            Top2Name:  top2Name,
            Top2Score: top2Score,
            Error:     searchErr,
        }
        results = append(results, cr)
        if searchErr != nil {
            failedComparisons++
        } else {
            successfulComparisons++
        }
    }

    // 6. Print comparison table
    fmt.Println()
    printComparisonTable(results)
    fmt.Printf("\nSummary: %d succeeded, %d failed\n", successfulComparisons, failedComparisons)
    if successfulComparisons == 0 {
        return fmt.Errorf("all %d comparisons failed", failedComparisons)
    }

    return nil
}

func runVectorSearchWithRetry(ctx context.Context, collection *mongo.Collection, queryEmbedding []float64, vectorField string, topK int) ([]SearchResult, error) {
    const maxAttempts = 6
    const retryDelay = 2 * time.Second

    var searchResults []SearchResult
    var searchErr error

    for attempt := 1; attempt <= maxAttempts; attempt++ {
        searchResults, searchErr = vectorSearchSimple(ctx, collection, queryEmbedding, vectorField, topK)
        if searchErr == nil {
            if len(searchResults) > 0 {
                return searchResults, nil
            }
            searchErr = fmt.Errorf("search returned no results")
        }

        if attempt < maxAttempts {
            time.Sleep(retryDelay)
        }
    }

    return searchResults, searchErr
}

// buildIndexSpecs creates the 9 index specifications
func buildIndexSpecs(vectorField string, dimensions int, metrics []string) []indexSpec {
    var specs []indexSpec

    type algoConfig struct {
        name    string
        kind    string
        options bson.D
    }

    algos := []algoConfig{
        {"IVF", "vector-ivf", bson.D{{"numLists", 1}}},
        {"HNSW", "vector-hnsw", bson.D{{"m", 16}, {"efConstruction", 64}}},
        {"DiskANN", "vector-diskann", bson.D{{"maxDegree", 32}, {"lBuild", 50}}},
    }

    for _, algo := range algos {
        for _, metric := range metrics {
            metricLower := strings.ToLower(metric)
            opts := bson.D{
                {"kind", algo.kind},
                {"dimensions", dimensions},
                {"similarity", metric},
            }
            for _, o := range algo.options {
                opts = append(opts, o)
            }

            specs = append(specs, indexSpec{
                Algorithm: algo.name,
                Kind:      algo.kind,
                Metric:    metric,
                IndexName: fmt.Sprintf("vector_%s_%s", strings.ToLower(algo.name), metricLower),
                Options:   opts,
            })
        }
    }

    return specs
}

// createNamedVectorIndex creates a single named vector index
func createNamedVectorIndex(ctx context.Context, collection *mongo.Collection, vectorField string, spec indexSpec) error {
    indexCommand := bson.D{
        {"createIndexes", collection.Name()},
        {"indexes", []bson.D{
            {
                {"name", spec.IndexName},
                {"key", bson.D{
                    {vectorField, "cosmosSearch"},
                }},
                {"cosmosSearchOptions", spec.Options},
            },
        }},
    }

    var result bson.M
    err := collection.Database().RunCommand(ctx, indexCommand).Decode(&result)
    if err != nil {
        if strings.Contains(err.Error(), "already exists") || strings.Contains(err.Error(), "IndexAlreadyExists") {
            return nil
        }
        return err
    }
    return nil
}

// vectorSearchSimple performs a vector search using the active vector index
func vectorSearchSimple(ctx context.Context, collection *mongo.Collection, embedding []float64, vectorField string, topK int) ([]SearchResult, error) {
    pipeline := []bson.M{
        {
            "$search": bson.M{
                "cosmosSearch": bson.M{
                    "vector": embedding,
                    "path":   vectorField,
                    "k":      topK,
                },
            },
        },
        {
            "$project": bson.M{
                "document": "$$ROOT",
                "score":    bson.M{"$meta": "searchScore"},
            },
        },
    }

    cursor, err := collection.Aggregate(ctx, pipeline)
    if err != nil {
        return nil, err
    }
    defer cursor.Close(ctx)

    var results []SearchResult
    for cursor.Next(ctx) {
        var result SearchResult
        if err := cursor.Decode(&result); err != nil {
            continue
        }
        results = append(results, result)
    }

    if err := cursor.Err(); err != nil {
        return nil, err
    }

    return results, nil
}

// extractResult returns the name and score of the result at the given index
func extractResult(results []SearchResult, idx int) (string, float64) {
    if idx >= len(results) {
        return "(no results)", 0
    }
    doc := results[idx].Document.(bson.D)
    var name string
    for _, elem := range doc {
        if elem.Key == "HotelName" {
            name = fmt.Sprintf("%v", elem.Value)
            break
        }
    }
    if name == "" {
        name = "Unknown"
    }
    return name, results[idx].Score
}

// printComparisonTable outputs a formatted table of results
func printComparisonTable(results []CompareResult) {
    fmt.Println("┌──────────┬────────┬────────────────────────────┬────────┬────────────────────────────┬────────┬───────┐")
    fmt.Printf("│ %-8s │ %-6s │ %-26s │ %-6s │ %-26s │ %-6s │ %-5s │\n",
        "Algorithm", "Metric", "Top 1 Result", "Score", "Top 2 Result", "Score", "Diff")
    fmt.Println("├──────────┼────────┼────────────────────────────┼────────┼────────────────────────────┼────────┼───────┤")

    for _, r := range results {
        if r.Error != nil {
            fmt.Printf("│ %-8s │ %-6s │ %-26s │ %-6s │ %-26s │ %-6s │ %-5s │\n",
                r.Algorithm, r.Metric, "ERROR", "-", "-", "-", "-")
            continue
        }

        top1 := r.Top1Name
        if len(top1) > 26 {
            top1 = top1[:26]
        }
        top2 := r.Top2Name
        if len(top2) > 26 {
            top2 = top2[:26]
        }
        diff := math.Abs(r.Top1Score - r.Top2Score)

        fmt.Printf("│ %-8s │ %-6s │ %-26s │ %6.4f │ %-26s │ %6.4f │%6.4f │\n",
            r.Algorithm, r.Metric, top1, r.Top1Score, top2, r.Top2Score, diff)
    }

    fmt.Println("└──────────┴────────┴────────────────────────────┴────────┴────────────────────────────┴────────┴───────┘")
}

src/utils.go

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "os"
    "strconv"
    "strings"
    "time"

    "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
    "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    "github.com/openai/openai-go/v3"
    "github.com/openai/openai-go/v3/azure"
    "github.com/openai/openai-go/v3/option"
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

// Config holds the application configuration
type Config struct {
    ClusterName  string
    DatabaseName string
    DataFile     string
    VectorField  string
    ModelName    string
    Dimensions   int
    BatchSize    int
    Similarity   string
    Algorithm    string
}

// SearchResult represents a search result document
type SearchResult struct {
    Document interface{} `bson:"document"`
    Score    float64     `bson:"score"`
}

// InsertStats holds statistics about data insertion
type InsertStats struct {
    Total    int `json:"total"`
    Inserted int `json:"inserted"`
    Failed   int `json:"failed"`
}

// LoadConfig loads configuration from environment variables
func LoadConfig() (*Config, error) {
    dimensions, err := parsePositiveIntEnv("EMBEDDING_DIMENSIONS", "1536")
    if err != nil {
        return nil, err
    }

    batchSize, err := parsePositiveIntEnv("LOAD_SIZE_BATCH", "100")
    if err != nil {
        return nil, err
    }

    return &Config{
        ClusterName:  getEnvOrDefault("DOCUMENTDB_CLUSTER_NAME", ""),
        DatabaseName: getEnvOrDefault("AZURE_DOCUMENTDB_DATABASENAME", "Hotels"),
        DataFile:     getEnvOrDefault("DATA_FILE_WITH_VECTORS", "data/Hotels_Vector.json"),
        VectorField:  getEnvOrDefault("EMBEDDED_FIELD", "DescriptionVector"),
        ModelName:    getEnvOrDefault("AZURE_OPENAI_EMBEDDING_MODEL", "text-embedding-3-small"),
        Dimensions:   dimensions,
        BatchSize:    batchSize,
        Similarity:   getEnvOrDefault("SIMILARITY", ""),
        Algorithm:    strings.ToLower(getEnvOrDefault("ALGORITHM", "")),
    }, nil
}

func parsePositiveIntEnv(key, defaultValue string) (int, error) {
    value := getEnvOrDefault(key, defaultValue)
    parsedValue, err := strconv.Atoi(value)
    if err != nil {
        return 0, fmt.Errorf("%s must be a positive integer, got %q", key, value)
    }
    if parsedValue <= 0 {
        return 0, fmt.Errorf("%s must be greater than 0, got %q", key, value)
    }
    return parsedValue, nil
}

// getEnvOrDefault returns environment variable value or default if not set
func getEnvOrDefault(key, defaultValue string) string {
    if value := os.Getenv(key); value != "" {
        return value
    }
    return defaultValue
}

// GetClientsPasswordless creates MongoDB and Azure OpenAI clients with passwordless authentication
func GetClientsPasswordless(ctx context.Context, config *Config) (*mongo.Client, openai.Client, error) {
    if config.ClusterName == "" {
        return nil, openai.Client{}, fmt.Errorf("DOCUMENTDB_CLUSTER_NAME environment variable is required")
    }

    // Create Azure credential
    credential, err := azidentity.NewDefaultAzureCredential(nil)
    if err != nil {
        return nil, openai.Client{}, fmt.Errorf("failed to create Azure credential: %v", err)
    }

    // Connect to DocumentDB with OIDC authentication
    mongoURI := fmt.Sprintf("mongodb+srv://%s.global.mongocluster.cosmos.azure.com/", config.ClusterName)

    fmt.Println("Attempting OIDC authentication...")
    mongoClient, err := connectWithOIDC(ctx, mongoURI, credential)
    if err != nil {
        return nil, openai.Client{}, fmt.Errorf("OIDC authentication failed: %v", err)
    }
    fmt.Println("OIDC authentication successful!")

    // Get Azure OpenAI endpoint
    azureOpenAIEndpoint := os.Getenv("AZURE_OPENAI_EMBEDDING_ENDPOINT")
    if azureOpenAIEndpoint == "" {
        return nil, openai.Client{}, fmt.Errorf("AZURE_OPENAI_EMBEDDING_ENDPOINT environment variable is required")
    }

    // Create Azure OpenAI client with credential-based authentication
    openAIClient := openai.NewClient(
        option.WithBaseURL(fmt.Sprintf("%s/openai/v1", azureOpenAIEndpoint)),
        azure.WithTokenCredential(credential))

    return mongoClient, openAIClient, nil
}

// connectWithOIDC attempts to connect using OIDC authentication
func connectWithOIDC(ctx context.Context, mongoURI string, credential *azidentity.DefaultAzureCredential) (*mongo.Client, error) {
    oidcCallback := func(ctx context.Context, args *options.OIDCArgs) (*options.OIDCCredential, error) {
        scope := "https://ossrdbms-aad.database.windows.net/.default"
        fmt.Printf("Getting token with scope: %s\n", scope)
        token, err := credential.GetToken(ctx, policy.TokenRequestOptions{
            Scopes: []string{scope},
        })
        if err != nil {
            return nil, fmt.Errorf("failed to get token with scope %s: %v", scope, err)
        }

        fmt.Printf("Successfully obtained token\n")

        return &options.OIDCCredential{
            AccessToken: token.Token,
        }, nil
    }

    clientOptions := options.Client().
        ApplyURI(mongoURI).
        SetConnectTimeout(30 * time.Second).
        SetServerSelectionTimeout(30 * time.Second).
        SetRetryWrites(false).
        SetAuth(options.Credential{
            AuthMechanism: "MONGODB-OIDC",
            AuthMechanismProperties: map[string]string{
                "TOKEN_RESOURCE": "https://ossrdbms-aad.database.windows.net",
            },
            OIDCMachineCallback: oidcCallback,
        })

    mongoClient, err := mongo.Connect(ctx, clientOptions)
    if err != nil {
        return nil, err
    }

    return mongoClient, nil
}

// ReadFileReturnJSON reads a JSON file and returns the data as a slice of maps
func ReadFileReturnJSON(filePath string) ([]map[string]interface{}, error) {
    file, err := os.ReadFile(filePath)
    if err != nil {
        return nil, fmt.Errorf("error reading file '%s': %v", filePath, err)
    }

    var data []map[string]interface{}
    err = json.Unmarshal(file, &data)
    if err != nil {
        return nil, fmt.Errorf("error parsing JSON in file '%s': %v", filePath, err)
    }

    return data, nil
}

// InsertData inserts data into a MongoDB collection in batches
func InsertData(ctx context.Context, collection *mongo.Collection, data []map[string]interface{}, batchSize int) (*InsertStats, error) {
    totalDocuments := len(data)
    insertedCount := 0
    failedCount := 0

    fmt.Printf("Starting batch insertion of %d documents...\n", totalDocuments)

    for i := 0; i < totalDocuments; i += batchSize {
        end := i + batchSize
        if end > totalDocuments {
            end = totalDocuments
        }

        batch := data[i:end]
        batchNum := (i / batchSize) + 1

        documents := make([]interface{}, len(batch))
        for j, doc := range batch {
            documents[j] = doc
        }

        result, err := collection.InsertMany(ctx, documents, options.InsertMany().SetOrdered(false))
        if err != nil {
            if bulkErr, ok := err.(mongo.BulkWriteException); ok {
                errorCount := len(bulkErr.WriteErrors)
                insertedCount += len(batch) - errorCount
                failedCount += errorCount
                fmt.Printf("Batch %d had errors: %d inserted, %d failed\n", batchNum, len(batch)-errorCount, errorCount)
                for _, writeErr := range bulkErr.WriteErrors {
                    fmt.Printf("  Error: %s\n", writeErr.Message)
                }
            } else {
                failedCount += len(batch)
                fmt.Printf("Batch %d failed completely: %v\n", batchNum, err)
            }
        } else {
            insertedCount += len(result.InsertedIDs)
            fmt.Printf("Batch %d completed: %d documents inserted\n", batchNum, len(result.InsertedIDs))
        }

        time.Sleep(100 * time.Millisecond)
    }

    return &InsertStats{
        Total:    totalDocuments,
        Inserted: insertedCount,
        Failed:   failedCount,
    }, nil
}

// DropVectorIndexes drops existing vector indexes on the specified field
func DropVectorIndexes(ctx context.Context, collection *mongo.Collection, vectorField string) error {
    cursor, err := collection.Indexes().List(ctx)
    if err != nil {
        return fmt.Errorf("could not list indexes: %v", err)
    }
    defer cursor.Close(ctx)

    var vectorIndexes []string
    for cursor.Next(ctx) {
        var index bson.M
        if err := cursor.Decode(&index); err != nil {
            continue
        }

        if key, ok := index["key"].(bson.M); ok {
            if indexType, exists := key[vectorField]; exists && indexType == "cosmosSearch" {
                if name, ok := index["name"].(string); ok {
                    vectorIndexes = append(vectorIndexes, name)
                }
            }
        }
    }

    for _, indexName := range vectorIndexes {
        fmt.Printf("Dropping existing vector index: %s\n", indexName)
        _, err := collection.Indexes().DropOne(ctx, indexName)
        if err != nil {
            fmt.Printf("Warning: Could not drop index %s: %v\n", indexName, err)
        }
    }

    if len(vectorIndexes) > 0 {
        fmt.Printf("Dropped %d existing vector index(es)\n", len(vectorIndexes))
    } else {
        fmt.Println("No existing vector indexes found to drop")
    }

    return nil
}

// PerformVectorSearch performs a vector search using the cosmosSearch aggregation pipeline
func PerformVectorSearch(ctx context.Context, collection *mongo.Collection, client openai.Client, query, vectorField, model string, topK int) ([]SearchResult, error) {
    fmt.Printf("Performing vector search for: '%s'\n", query)

    queryEmbedding, err := GenerateEmbedding(ctx, client, query, model)
    if err != nil {
        return nil, fmt.Errorf("error generating embedding: %v", err)
    }

    pipeline := []bson.M{
        {
            "$search": bson.M{
                "cosmosSearch": bson.M{
                    "vector": queryEmbedding,
                    "path":   vectorField,
                    "k":      topK,
                },
            },
        },
        {
            "$project": bson.M{
                "document": "$$ROOT",
                "score":    bson.M{"$meta": "searchScore"},
            },
        },
    }

    cursor, err := collection.Aggregate(ctx, pipeline)
    if err != nil {
        return nil, fmt.Errorf("error performing vector search: %v", err)
    }
    defer cursor.Close(ctx)

    var results []SearchResult
    for cursor.Next(ctx) {
        var result SearchResult
        if err := cursor.Decode(&result); err != nil {
            fmt.Printf("Warning: Could not decode result: %v\n", err)
            continue
        }
        results = append(results, result)
    }

    if err := cursor.Err(); err != nil {
        return nil, fmt.Errorf("cursor error: %v", err)
    }

    return results, nil
}

// GenerateEmbedding generates an embedding for the given text using Azure OpenAI
func GenerateEmbedding(ctx context.Context, client openai.Client, text, modelName string) ([]float64, error) {
    resp, err := client.Embeddings.New(ctx, openai.EmbeddingNewParams{
        Input: openai.EmbeddingNewParamsInputUnion{
            OfString: openai.String(text),
        },
        Model: modelName,
    })
    if err != nil {
        return nil, fmt.Errorf("failed to generate embedding: %v", err)
    }

    if len(resp.Data) == 0 {
        return nil, fmt.Errorf("no embedding data received")
    }

    embedding := make([]float64, len(resp.Data[0].Embedding))
    for i, v := range resp.Data[0].Embedding {
        embedding[i] = float64(v)
    }

    return embedding, nil
}

// PrintSearchResults prints search results in a formatted way
func PrintSearchResults(results []SearchResult, algorithm string) {
    if len(results) == 0 {
        fmt.Println("No search results found.")
        return
    }

    fmt.Printf("\n%s Search Results (top %d):\n", strings.ToUpper(algorithm), len(results))
    fmt.Println(strings.Repeat("=", 80))

    for i, result := range results {
        doc := result.Document.(bson.D)
        var hotelName string
        for _, elem := range doc {
            if elem.Key == "HotelName" {
                hotelName = fmt.Sprintf("%v", elem.Value)
                break
            }
        }

        fmt.Printf("%d. HotelName: %s, Score: %.4f\n", i+1, hotelName, result.Score)
    }
}

// FilterDocumentsWithEmbeddings returns only documents that contain the vector field
func FilterDocumentsWithEmbeddings(data []map[string]interface{}, vectorField string) []map[string]interface{} {
    var filtered []map[string]interface{}
    for _, doc := range data {
        if _, exists := doc[vectorField]; exists {
            filtered = append(filtered, doc)
        }
    }
    return filtered
}

// PrepareCollection clears existing data and inserts new documents
func PrepareCollection(ctx context.Context, collection *mongo.Collection, data []map[string]interface{}, batchSize int) (*InsertStats, error) {
    fmt.Printf("Preparing collection '%s'...\n", collection.Name())

    deleteResult, err := collection.DeleteMany(ctx, bson.M{})
    if err != nil {
        return nil, fmt.Errorf("failed to clear existing data: %v", err)
    }
    if deleteResult.DeletedCount > 0 {
        fmt.Printf("Cleared %d existing documents from collection\n", deleteResult.DeletedCount)
    }

    stats, err := InsertData(ctx, collection, data, batchSize)
    if err != nil {
        return nil, fmt.Errorf("failed to insert data: %v", err)
    }

    return stats, nil
}

Tento kód poskytuje aplikaci pro porovnání vektorových algoritmů s těmito klíčovými funkcemi:

  • Ověřování bez hesla: Používá DefaultAzureCredential pro Azure OpenAI i DocumentDB prostřednictvím OIDC.
  • Tři vektorové algoritmy: Implementuje DiskANN, HNSW a IVF s parametry ladění specifické pro algoritmus.
  • Tři funkce podobnosti: Podporuje COS (kosinus), L2 (Euclidean) a IP (vnitřní produkt).
  • Jediný vstupní bod pro porovnání všeho: Vždy spustí všech 9 kombinací algoritmu a podobnosti v jednom průchodu.
  • Automatizace životního cyklu indexu: Vytváří, dotazuje a zahodí každý vektorový index v sekvenci.
  • Výstup porovnání: Vygeneruje formátovanou tabulku zobrazující nejlepší dva výsledky a mezeru skóre pro každou kombinaci.
  • Vzory připravené pro produkční prostředí: Zahrnuje dávkové vkládání, zpracování chyb a sdružování připojení.

Note

Ukázka Jazyka Go konfiguruje připojení DocumentDB s retryWrites=false, které je vyžadováno pro operace hledání vektorů DocumentDB.

Spuštění kódu

Po nastavení proměnných prostředí ve svém shellovém sezení spusťte aplikaci:

go run ./src/

Aplikace provádí následující:

  1. Připojení k Azure DocumentDB a Azure OpenAI pomocí ověřování bez hesla
  2. Načtěte data o hotelech a vložte je do kolekce hotels.
  3. Vygenerujte embedding pro vyhledávací dotaz
  4. Spusťte všech 9 porovnání vektorových indexů vytvořením, dotazováním a vyřazením každého indexu v sekvenci
  5. Zobrazte srovnávací tabulku s nejlepšími dvěma výsledky a mezerou skóre pro každou kombinaci
  6. hotels Odstranění kolekce během čištění

Očekávaný výstup:

======================================================================
  COMPARE ALL: 3 Algorithms × 3 Similarity Metrics (9 combinations)
======================================================================
Query:  "luxury hotel near the beach"
Top-K:  5

Loading data from data/Hotels_Vector.json...
Loaded 50 documents with embeddings
Insertion completed: 50 inserted, 0 failed

Generating embedding for query: "luxury hotel near the beach"
Embedding generated (1536 dimensions)

Running 9 vector index comparisons (create→search→drop)...
  ✓ vector_ivf_cos created
  ✓ vector_ivf_l2 created
  ✓ vector_ivf_ip created
  ✓ vector_hnsw_cos created
  ✓ vector_hnsw_l2 created
  ✓ vector_hnsw_ip created
  ✓ vector_diskann_cos created
  ✓ vector_diskann_l2 created
  ✓ vector_diskann_ip created

┌──────────┬────────┬────────────────────────────┬────────┬────────────────────────────┬────────┬───────┐
│ Algorithm│ Metric │ Top 1 Result               │ Score  │ Top 2 Result               │ Score  │ Diff  │
├──────────┼────────┼────────────────────────────┼────────┼────────────────────────────┼────────┼───────┤
│ IVF      │ COS    │ Ocean Water Resort & Spa   │ 0.6184 │ Windy Ocean Motel          │ 0.5056 │ 0.1128│
│ IVF      │ L2     │ Ocean Water Resort & Spa   │ 0.8736 │ Windy Ocean Motel          │ 0.9943 │ 0.1208│
│ IVF      │ IP     │ Ocean Water Resort & Spa   │ 0.6184 │ Windy Ocean Motel          │ 0.5056 │ 0.1128│
│ HNSW     │ COS    │ Ocean Water Resort & Spa   │ 0.6184 │ Windy Ocean Motel          │ 0.5056 │ 0.1128│
│ HNSW     │ L2     │ Ocean Water Resort & Spa   │ 0.8736 │ Windy Ocean Motel          │ 0.9943 │ 0.1208│
│ HNSW     │ IP     │ Ocean Water Resort & Spa   │ 0.6184 │ Windy Ocean Motel          │ 0.5056 │ 0.1128│
│ DiskANN  │ COS    │ Ocean Water Resort & Spa   │ 0.6184 │ Windy Ocean Motel          │ 0.5056 │ 0.1128│
│ DiskANN  │ L2     │ Ocean Water Resort & Spa   │ 0.8736 │ Windy Ocean Motel          │ 0.9943 │ 0.1208│
│ DiskANN  │ IP     │ Ocean Water Resort & Spa   │ 0.6184 │ Windy Ocean Motel          │ 0.5056 │ 0.1128│
└──────────┴────────┴────────────────────────────┴────────┴────────────────────────────┴────────┴───────┘

Summary: 9 succeeded, 0 failed

Cleanup: dropped collection 'hotels'

Sloupec Rozdíl zobrazuje rozdíl ve skóre mezi výsledkem na 1. a 2. místě. Menší rozdíl indikuje, že algoritmus našel výsledky s více podobnými skóre relevance.

Vysvětlení výsledků

Tabulka porovnání ukazuje, jak různé algoritmy fungují ve stejné datové sadě se stejným dotazem:

  • Algoritmus: DiskANN, HNSW nebo IVF
  • Metrika: Metrika podobnosti (COS, L2 nebo IP)
  • Top 1 Result: Nejvýše hodnocený hotel pro daný algoritmus a metriku
  • Skóre: Skóre relevantnosti pro odpovídající výsledek
  • 2. výsledek: Druhý nejlépe hodnocený hotel pro tento algoritmus a metriku
  • Rozdíl: Rozdíl skóre mezi hlavními dvěma výsledky

Výběr správného algoritmu

Important

V případě produkčních úloh začněte s DiskANN v clusteru M30+. DiskANN podporuje vyšší dimenze embeddingu, využívá méně paměti clusteru a je méně pravděpodobné, že bude při vývoji vašich modelů vyžadovat přepracování indexu.

Pomocí této tabulky s rychlými referenčními informacemi vyberte správný algoritmus pro vaši úlohu:

Scenario Algorithm Úroveň clusteru Maximální rozměry
Vývoj/testování, ukázky, malé datové sady IVF M10+ 2,000
Produkční (výchozí) DiskANN M30+ 16,000
Produkce (maximální priorita recallu) HNSW M30+ 8,000

IVF (invertovaný souborový index):

  • Nejvhodnější pro: Testovací prostředí, ukázky a malé clustery
  • Výhody: Rychlé sestavování, nízké požadavky na prostředky
  • Nevýhody: Nižší úplnost oproti algoritmům založeným na grafech ve velkém měřítku
  • Ladění: Zvýšení numLists u větších datových sad, zvýšení nProbes pro lepší úspěšnost vyhledání

DiskANN (přibližný nejbližší soused založený na disku) – doporučeno pro produkci:

  • Nejvhodnější pro: Produkční úlohy v clusterech M30+
  • Profesionálové: Podporuje vkládání až 16 000 dimenzí, udržuje většinu indexových dat na disku, který uvolní paměť clusteru pro čtení a zápisy, lehčí aktualizace indexů, jednodušší zálohování, rychlejší obnovení
  • Nevýhody: Vyžaduje úroveň clusteru M30+
  • Ladění: Zvyšte maxDegree a lBuild pro lepší přesnost, zvyšte lSearch pro lepší vybavitelnost
  • Proč je výchozí: Při vývoji modelů vkládání (některé už překračují 8 000 dimenzí) DiskANN zabraňuje nákladným úpravám indexů. Jeho disková architektura také znamená, že paměť clusteru zůstane dostupná pro provozní úlohy spíše než pro ukládání indexu.

HNSW (hierarchický navigovatelný malý svět):

  • Nejvhodnější pro: Produkční úlohy v clusterech M30+, kde je maximální recall nejvyšší prioritou
  • Výhody: Vynikající recall, rychlé dotazy
  • Nevýhody: Vyžaduje úroveň clusteru M30+ a podporuje vkládání až 8 000 dimenzí (vs. 16 000 pro DiskANN), vyšší využití paměti, protože celý graf žije v paměti RAM.
  • Ladění: Zvyšte m a efConstruction pro lepší kvalitu indexu, zvyšte efSearch pro lepší rekall

Volba správné funkce podobnosti

Function Význam skóre Nejlepší pro
COS (Kosinus) Vyšší = více podobné (0–1) Textové embeddingy (normalizované vektory)
L2 (Euclidean) Nižší = podobnější (vzdálenost) Když je velikost důležitá
IP (vnitřní produkt) Vyšší = více podobných Ekvivalent COS pro normalizované vektory

Pro model použitý v tomto rychlém startu text-embedding-3-smallse doporučuje COS (kosinová podobnost), protože embeddingy OpenAI jsou normalizované a optimalizované pro kosinovou podobnost.

Troubleshooting

Issue Solution
server selection error Ověřte, že jsou proměnné prostředí správně nastavené. Ujistěte se, že je vaše IP adresa v pravidlech brány firewall DocumentDB.
authentication failed Ověřte platnost tokenu Microsoft Entra. Spusťte az login pro obnovení svých přihlašovacích údajů.
go: module not found Spusťte go mod tidy pro vyřešení závislostí.
Chyby sestavení Ujistěte se, že je nainstalovaný Go 1.24 nebo novější. Spusťte go version ke kontrole.
Prázdné výsledky hledání Sestavení vektorového indexu může trvat několik minut. Počkejte 2 až 3 minuty po vytvoření indexu a pak znovu spusťte skript.

Vyčistěte zdroje

Odeberte databázi pomocí rozšíření DocumentDB pro Visual Studio Code:

  1. Nainstalujte rozšíření DocumentDB .
  2. Připojte se ke clusteru Azure DocumentDB.
  3. Rozbalte cluster, otevřete místní nabídku databáze Hotels a vyberte Odstranit databázi.