Go Client Examples
Complete examples demonstrating the hlquery Go client usage.
Basic Usage
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/hlquery/go-api"
)
func main() {
// Initialize client
client := hlquery.NewClient("http://localhost:9200")
// Health check
health, err := client.Health()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Health Status: %d\n", health.StatusCode)
// List collections
collections, err := client.ListCollections(0, 10)
if err != nil {
log.Fatal(err)
}
if collections.IsSuccess() {
body := collections.Body
if cols, ok := body["collections"].([]interface{}); ok {
fmt.Printf("Found %d collections\n", len(cols))
}
}
}
Collections Management
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/hlquery/go-api"
)
func main() {
client := hlquery.NewClient("http://localhost:9200")
// List collections
collections, err := client.Collections().List(0, 10)
if err != nil {
log.Fatal(err)
}
collectionsJSON, _ := json.MarshalIndent(collections.Body, "", " ")
fmt.Println(string(collectionsJSON))
// Get collection details
collection, err := client.Collections().Get("my_collection")
if err != nil {
log.Fatal(err)
}
collectionJSON, _ := json.MarshalIndent(collection.Body, "", " ")
fmt.Println(string(collectionJSON))
// Create collection
schema := map[string]interface{}{
"fields": []map[string]interface{}{
{"name": "title", "type": "string"},
{"name": "content", "type": "string"},
},
}
result, err := client.Collections().Create("new_collection", schema)
if err != nil {
log.Fatal(err)
}
resultJSON, _ := json.MarshalIndent(result.Body, "", " ")
fmt.Println(string(resultJSON))
// Delete collection
deleteResult, err := client.Collections().Delete("collection_name")
if err != nil {
log.Fatal(err)
}
deleteJSON, _ := json.MarshalIndent(deleteResult.Body, "", " ")
fmt.Println(string(deleteJSON))
}
Document Operations
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/hlquery/go-api"
)
func main() {
client := hlquery.NewClient("http://localhost:9200")
collection := "my_collection"
// List documents
params := map[string]interface{}{
"offset": 0,
"limit": 10,
}
docs, err := client.Documents().List(collection, params)
if err != nil {
log.Fatal(err)
}
docsJSON, _ := json.MarshalIndent(docs.Body, "", " ")
fmt.Println(string(docsJSON))
// Add document
doc := map[string]interface{}{
"id": "doc1",
"title": "Example Document",
"content": "This is an example",
}
addResult, err := client.Documents().Add(collection, doc)
if err != nil {
log.Fatal(err)
}
addJSON, _ := json.MarshalIndent(addResult.Body, "", " ")
fmt.Println(string(addJSON))
// Get document
getDoc, err := client.Documents().Get(collection, "doc1")
if err != nil {
log.Fatal(err)
}
getJSON, _ := json.MarshalIndent(getDoc.Body, "", " ")
fmt.Println(string(getJSON))
// Update document
updatedDoc := map[string]interface{}{
"title": "Updated Title",
}
updateResult, err := client.Documents().Update(collection, "doc1", updatedDoc)
if err != nil {
log.Fatal(err)
}
updateJSON, _ := json.MarshalIndent(updateResult.Body, "", " ")
fmt.Println(string(updateJSON))
// Bulk import
bulkDocs := []map[string]interface{}{
{"id": "doc2", "title": "Doc 2"},
{"id": "doc3", "title": "Doc 3"},
}
importResult, err := client.Documents().Import(collection, bulkDocs)
if err != nil {
log.Fatal(err)
}
importJSON, _ := json.MarshalIndent(importResult.Body, "", " ")
fmt.Println(string(importJSON))
// Delete document
deleteResult, err := client.Documents().Delete(collection, "doc1")
if err != nil {
log.Fatal(err)
}
deleteJSON, _ := json.MarshalIndent(deleteResult.Body, "", " ")
fmt.Println(string(deleteJSON))
}
Search Operations
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/hlquery/go-api"
)
func main() {
client := hlquery.NewClient("http://localhost:9200")
collection := "my_collection"
// Simple search
params := map[string]interface{}{
"q": "laptop",
"query_by": "title,description",
"limit": 10,
}
results, err := client.Search().Perform(collection, params)
if err != nil {
log.Fatal(err)
}
resultsJSON, _ := json.MarshalIndent(results.Body, "", " ")
fmt.Println(string(resultsJSON))
// Search with filters
filterParams := map[string]interface{}{
"q": "laptop",
"query_by": "title",
"filter_by": "price:>1000",
"sort_by": "price:asc",
"limit": 10,
}
filterResults, err := client.Search().Perform(collection, filterParams)
if err != nil {
log.Fatal(err)
}
filterJSON, _ := json.MarshalIndent(filterResults.Body, "", " ")
fmt.Println(string(filterJSON))
// Multi-search
searches := []map[string]interface{}{
{"collection": "products", "q": "laptop"},
{"collection": "articles", "q": "laptop"},
}
multiResults, err := client.Search().Multi(searches)
if err != nil {
log.Fatal(err)
}
multiJSON, _ := json.MarshalIndent(multiResults.Body, "", " ")
fmt.Println(string(multiJSON))
// Vector search
vectorParams := map[string]interface{}{
"vector": []float64{0.1, 0.2, 0.3, 0.4, 0.5},
"limit": 10,
}
vectorResults, err := client.Search().Vector(collection, vectorParams)
if err != nil {
log.Fatal(err)
}
vectorJSON, _ := json.MarshalIndent(vectorResults.Body, "", " ")
fmt.Println(string(vectorJSON))
}
Authentication
package main
import (
"fmt"
"log"
"github.com/hlquery/go-api"
)
func main() {
// Method 1: Set token in constructor
client := hlquery.NewClient("http://localhost:9200", hlquery.ClientOptions{
Token: "your_token_here",
AuthMethod: "bearer",
})
// Method 2: Set token dynamically
client2 := hlquery.NewClient("http://localhost:9200")
client2.SetAuthToken("your_token_here", "bearer")
// Method 3: Use X-API-Key
client3 := hlquery.NewClient("http://localhost:9200")
client3.SetAuthToken("your_api_key", "api-key")
// Test authentication
health, err := client.Health()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Health Status: %d\n", health.StatusCode)
}
Error Handling
package main
import (
"fmt"
"log"
"github.com/hlquery/go-api"
)
func main() {
client := hlquery.NewClient("http://localhost:9200")
result, err := client.CreateCollection("collection", map[string]interface{}{
"fields": []map[string]interface{}{},
})
if err != nil {
// Handle network/request errors
log.Fatalf("Request failed: %v", err)
}
if result.IsError() {
// Handle HTTP errors (4xx, 5xx)
errorMsg := result.GetError()
fmt.Printf("API Error: %s\n", errorMsg)
fmt.Printf("Status Code: %d\n", result.StatusCode)
} else {
// Handle success
fmt.Println("Collection created successfully")
}
}
Running Examples
All examples are located in the examples/ directory:
cd api/go/examples
go run basic_usage.go
go run collections.go
go run documents.go
go run search.go
Or use the quick access script:
./scripts/go
go run examples/basic_usage.go