Files
2026-09-08 00:59:28 -07:00

4.1 KiB

search-go

Hanzo Search Go SDK

Go Reference License: MIT

Go client for Hanzo Search — documents in, ranked results out, over full-text, vector and hybrid retrieval. Built on the Meilisearch Go client, so the declared Go package is meilisearch; alias the import as below.

The search engine itself lives in hanzoai/index. hanzoai/search is an old path that redirects there — one product, not two.

Installation

Requires Go >= 1.20.

go get github.com/hanzoai/search-go

Quick Start

package main

import (
    "fmt"
    "os"

    search "github.com/hanzoai/search-go"
)

func main() {
    client := search.New("http://localhost:7700", search.WithAPIKey("your-api-key"))

    // Create an index
    task, err := client.CreateIndex(&search.IndexConfig{
        Uid:        "movies",
        PrimaryKey: "id",
    })
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println("CreateIndex task:", task.TaskUID)

    // Add documents
    index := client.Index("movies")

    documents := []map[string]interface{}{
        {"id": 1, "title": "Carol", "genres": []string{"Romance", "Drama"}},
        {"id": 2, "title": "Wonder Woman", "genres": []string{"Action", "Adventure"}},
        {"id": 3, "title": "Life of Pi", "genres": []string{"Adventure", "Drama"}},
        {"id": 4, "title": "Mad Max: Fury Road", "genres": []string{"Adventure", "Science Fiction"}},
        {"id": 5, "title": "Moana", "genres": []string{"Fantasy", "Action"}},
        {"id": 6, "title": "Philadelphia", "genres": []string{"Drama"}},
    }

    task, err = index.AddDocuments(documents, nil)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println("AddDocuments task:", task.TaskUID)

    // Search
    res, err := index.Search("wonder", &search.SearchRequest{
        Limit: 10,
    })
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println(res.Hits)
}

Common Operations

CreateIndex

task, err := client.CreateIndex(&search.IndexConfig{
    Uid:        "products",
    PrimaryKey: "id",
})

AddDocuments

index := client.Index("products")

docs := []map[string]interface{}{
    {"id": 1, "name": "Keyboard", "price": 49.99},
    {"id": 2, "name": "Mouse", "price": 29.99},
}

task, err := index.AddDocuments(docs, nil)
// Basic search
res, err := index.Search("keyboard", &search.SearchRequest{
    Limit: 20,
})

// Search with highlighting
res, err := index.Search("keyboard", &search.SearchRequest{
    AttributesToHighlight: []string{"*"},
})

// Search with filters (requires filterable attributes to be set first)
res, err := index.Search("keyboard", &search.SearchRequest{
    Filter: "price < 50",
})

UpdateSettings

task, err := index.UpdateSettings(&search.Settings{
    FilterableAttributes: []string{"price", "category"},
    SortableAttributes:   []string{"price"},
    SearchableAttributes: []string{"name", "description"},
})

Update Filterable Attributes

task, err := index.UpdateFilterableAttributes(&[]string{"price", "category"})

Client Options

client := search.New("http://localhost:7700",
    search.WithAPIKey("your-api-key"),
    search.WithCustomClient(http.DefaultClient),
    search.WithContentEncoding(search.GzipEncoding, search.BestCompression),
    search.WithCustomRetries([]int{502, 503, 504}, 3),
)
Option Description
WithAPIKey Set API key for authentication
WithCustomClient Use a custom http.Client
WithCustomClientWithTLS Enable TLS for the HTTP client
WithContentEncoding Configure request/response encoding (gzip, deflate, brotli)
WithCustomRetries Customize retry behavior by status code and max retries
DisableRetries Disable automatic retries

Attribution

License

MIT License. Copyright (c) Hanzo AI Inc.