Skip to content

datastax/astra-db-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

astra-db-go

License Apache2 Go Reference Documentation

astra-db-go is a Go client for interacting with DataStax Astra DB and Hyper-Converged Database.

Warning

The Go client is currently in Public Preview and is under active development.

APIs are subject to breaking changes prior to the official stable release, and is not recommended for production workloads.

For production use, see the Python, TypeScript, Java, or .NET clients.

Requirements

Documentation

For Astra DB Serverless:

For Hyper-Converged Database (HCD):

Package-level documentation is available on pkg.go.dev.

At a glance

import (
    "context"
    "fmt"
    "log"

    "github.com/datastax/astra-db-go/v2/astra"
    "github.com/datastax/astra-db-go/v2/astra/filter"
    "github.com/datastax/astra-db-go/v2/astra/options"
    "github.com/datastax/astra-db-go/v2/astra/sort"
)

type MyDocument struct {
    Name        string `json:"name"`
    Price       int    `json:"price"`
    Description string `json:"$vectorize"`
}

func main() {
    ctx := context.Background()

    // Initialize the client
    client := astra.NewClient()
    db := client.Database("<endpoint>", options.API().SetToken("<token>"))

    // Create a collection
    coll, err := db.CreateCollection(ctx, "ExampleCollection")
    if err != nil {
        log.Fatal(err)
    }

    // Insert documents
    docs := []MyDocument{
        {Name: "Apple", Price: 10, Description: "..."},
        {Name: "Peach", Price: 5, Description: "..."},
        {Name: "Walnut", Price: 8, Description: "..."},
    }
    ids, err := coll.InsertMany(ctx, docs)
    if err == nil {
        fmt.Printf("Inserted documents with IDs: %v\n", ids)
    }

    // Find a document with vector search (vectorize)
    var result struct {
        MyDocument
        astra.WithSimilarity
    }

    err = coll.FindOne(ctx, filter.F{},
        options.CollectionFindOne().
            SetSort(sort.Vectorize("<search query>")).
            SetIncludeSimilarity(true),
    ).Decode(&result)

    if err == nil {
        fmt.Printf("Match: '%s' (similarity: %f)\n", result.Name, result.Similarity)
    }
}

Further usage examples are available in the examples directory of this repository.

Other Data API clients

About

Golang Data API client

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages