Deploy embedded seekdb with Go
Start a local seekdb instance from Go, run a hybrid search that combines full-text and vector retrieval, and print the document IDs in ranked order.
Prerequisites
Use Go 1.22 or later with cgo enabled and a C compiler for your platform.
- Linux (amd64 or arm64) requires glibc 2.28 or later and GCC or Clang.
- macOS (arm64) requires macOS 15.6.1 or later and the Xcode Command Line Tools.
- Windows (amd64) requires MinGW-w64 GCC on
PATHand the Microsoft Visual C++ v14 x64 runtime.
The runtime defaults to a 1 GB memory budget and a 2 GB log disk size. See the Go SDK platform requirements and known limitations.
Create the Go module
Create an empty project and install the Go SDK and MySQL driver at the versions used in this example.
mkdir seekdb-go-quickstart
cd seekdb-go-quickstart
go mod init example.com/seekdb-go-quickstart
go get github.com/oceanbase/seekdb-go@v1.4.1 github.com/go-sql-driver/mysql@v1.9.3
Create the search program
Copy the SQL from Embedded hybrid search SQL into hybrid-search.sql next to go.mod. Keep the full SQL block, including the final SELECT.
Save the following program as main.go in the same directory. seekdb.Open starts a local instance. ConnectionOptions supplies the Unix socket on Linux and macOS or the loopback TCP address and port on Windows.
The program creates the table and indexes, inserts the sample data, and reads the search results from the test database. The code splits statements on semicolons for this SQL file.
package main
import (
"database/sql"
_ "embed"
"encoding/json"
"flag"
"fmt"
"log"
"net"
"strconv"
"strings"
"github.com/go-sql-driver/mysql"
seekdb "github.com/oceanbase/seekdb-go"
)
//go:embed hybrid-search.sql
var fixture string
func main() {
dir := flag.String("db-dir", "./seekdb-go-data", "seekdb data directory")
flag.Parse()
if err := run(*dir); err != nil {
log.Fatal(err)
}
}
func run(dir string) error {
engine, err := seekdb.Open(dir)
if err != nil {
return fmt.Errorf("open seekdb: %w", err)
}
defer engine.Close()
endpoint, err := engine.ConnectionOptions()
if err != nil {
return fmt.Errorf("get connection options: %w", err)
}
config := mysql.NewConfig()
config.User = endpoint.User
config.DBName = "test"
switch endpoint.Transport {
case "unix_socket":
config.Net = "unix"
config.Addr = endpoint.UnixSocket
case "tcp":
config.Net = "tcp"
config.Addr = net.JoinHostPort(endpoint.Host, strconv.FormatUint(uint64(endpoint.Port), 10))
default:
return fmt.Errorf("unsupported transport %q", endpoint.Transport)
}
db, err := sql.Open("mysql", config.FormatDSN())
if err != nil {
return err
}
defer db.Close()
var version string
if err := db.QueryRow("SELECT VERSION()").Scan(&version); err != nil {
return fmt.Errorf("query engine version: %w", err)
}
statements := strings.Split(strings.TrimSuffix(strings.TrimSpace(fixture), ";"), ";")
last := len(statements) - 1
for i, statement := range statements[:last] {
if _, err := db.Exec(statement); err != nil {
return fmt.Errorf("execute setup statement %d: %w", i+1, err)
}
}
var result string
if err := db.QueryRow(statements[last]).Scan(&result); err != nil {
return fmt.Errorf("run hybrid search: %w", err)
}
var hits []struct {
ID int `json:"id"`
}
if err := json.Unmarshal([]byte(result), &hits); err != nil {
return fmt.Errorf("decode search results: %w", err)
}
ids := make([]int, 0, len(hits))
for _, hit := range hits {
ids = append(ids, hit.ID)
}
fmt.Println(version)
fmt.Printf("Ranked IDs: %v\n", ids)
return nil
}
Run the search
Use a fresh data directory. The program uses ./seekdb-go-data by default and creates it if needed.
On Linux or macOS, run the program with cgo enabled.
CGO_ENABLED=1 go run .
On Windows, run the following in PowerShell.
$env:CGO_ENABLED = "1"
$env:CC = "gcc"
go run .
With the engine bundled in Go SDK v1.4.1, expect the following output on Linux amd64.
5.7.25-OceanBase seekdb-v1.4.1.0
Ranked IDs: [1 3 2]
The program closes the SQL client before it closes the seekdb handle. Data stays in the directory, so you can reopen it with seekdb.Open. To rerun the full example, pass -db-dir ./seekdb-go-data-2 after go run . to use another empty directory.