1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/meilisearch/meilisearch-go"
)
// User represents a user document
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Role string `json:"role"`
Active bool `json:"active"`
JoinDate string `json:"join_date"`
}
func main() {
// Initialize the Meilisearch client
host := getenv("MEILI_HOST", "http://localhost:7700")
apiKey := os.Getenv("MEILI_API_KEY")
client := meilisearch.New(host, meilisearch.WithAPIKey(apiKey))
defer client.Close()
// Test connection
fmt.Println("Testing connection to Meilisearch...")
if !client.IsHealthy() {
log.Fatal("Meilisearch is not available")
}
fmt.Println("ā
Connected to Meilisearch")
// Create index for document operations
indexUID := "users"
if err := createIndex(client, indexUID); err != nil {
log.Fatalf("Failed to create index: %v", err)
}
index := client.Index(indexUID)
fmt.Println("\nš Document Management Examples")
fmt.Println("===============================")
// 1. Add documents (batch)
fmt.Println("1. Adding documents in batch:")
users := []User{
{ID: 1, Name: "Alice Johnson", Email: "alice@example.com", Role: "admin", Active: true, JoinDate: "2023-01-15"},
{ID: 2, Name: "Bob Smith", Email: "bob@example.com", Role: "user", Active: true, JoinDate: "2023-02-20"},
{ID: 3, Name: "Charlie Brown", Email: "charlie@example.com", Role: "moderator", Active: false, JoinDate: "2023-03-10"},
}
task, err := index.AddDocuments(users, meilisearch.StringPtr("id"))
if err != nil {
log.Fatalf("Failed to add documents: %v", err)
}
if err := waitForTask(client, task.TaskUID); err != nil {
log.Fatalf("Failed to wait for add task: %v", err)
}
fmt.Printf("ā
Added %d documents (Task ID: %d)\n", len(users), task.TaskUID)
// 2. Add single document
fmt.Println("\n2. Adding single document:")
newUser := User{
ID: 4,
Name: "Diana Prince",
Email: "diana@example.com",
Role: "user",
Active: true,
JoinDate: "2023-04-05",
}
task, err = index.AddDocuments([]User{newUser}, nil)
if err != nil {
log.Fatalf("Failed to add single document: %v", err)
}
if err := waitForTask(client, task.TaskUID); err != nil {
log.Fatalf("Failed to wait for add task: %v", err)
}
fmt.Printf("ā
Added single document (Task ID: %d)\n", task.TaskUID)
// 3. Update documents
fmt.Println("\n3. Updating documents:")
updatedUsers := []User{
{ID: 2, Name: "Bob Smith Jr.", Email: "bob.jr@example.com", Role: "admin", Active: true, JoinDate: "2023-02-20"},
{ID: 3, Name: "Charlie Brown", Email: "charlie@example.com", Role: "moderator", Active: true, JoinDate: "2023-03-10"}, // Activating user
}
task, err = index.UpdateDocuments(updatedUsers, nil)
if err != nil {
log.Fatalf("Failed to update documents: %v", err)
}
if err := waitForTask(client, task.TaskUID); err != nil {
log.Fatalf("Failed to wait for update task: %v", err)
}
fmt.Printf("ā
Updated %d documents (Task ID: %d)\n", len(updatedUsers), task.TaskUID)
// 4. Get document by ID
fmt.Println("\n4. Getting document by ID:")
var doc User
err = index.GetDocument("2", nil, &doc)
if err != nil {
log.Fatalf("Failed to get document: %v", err)
}
fmt.Printf("Retrieved document: %v\n", doc)
// 5. Get multiple documents
fmt.Println("\n5. Getting multiple documents:")
var docs meilisearch.DocumentsResult
err = index.GetDocuments(&meilisearch.DocumentsQuery{
Limit: 10,
Offset: 0,
}, &docs)
if err != nil {
log.Fatalf("Failed to get documents: %v", err)
}
fmt.Printf("Retrieved %d documents:\n", len(docs.Results))
for i, doc := range docs.Results {
fmt.Printf(" %d. %v\n", i+1, doc)
}
// 6. Delete documents
fmt.Println("\n6. Deleting documents:")
task, err = index.DeleteDocument("4")
if err != nil {
log.Fatalf("Failed to delete document: %v", err)
}
if err := waitForTask(client, task.TaskUID); err != nil {
log.Fatalf("Failed to wait for delete task: %v", err)
}
fmt.Printf("ā
Deleted document with ID 4 (Task ID: %d)\n", task.TaskUID)
// 7. Delete multiple documents
fmt.Println("\n7. Deleting multiple documents:")
task, err = index.DeleteDocuments([]string{"1", "3"})
if err != nil {
log.Fatalf("Failed to delete documents: %v", err)
}
if err := waitForTask(client, task.TaskUID); err != nil {
log.Fatalf("Failed to wait for delete task: %v", err)
}
fmt.Printf("ā
Deleted multiple documents (Task ID: %d)\n", task.TaskUID)
// 8. Final document count
fmt.Println("\n8. Final document count:")
var finalDocs meilisearch.DocumentsResult
err = index.GetDocuments(&meilisearch.DocumentsQuery{Limit: 100}, &finalDocs)
if err != nil {
log.Fatalf("Failed to get final documents: %v", err)
}
fmt.Printf("Remaining documents: %d\n", len(finalDocs.Results))
fmt.Println("\nDocument management examples completed successfully! š")
}
func createIndex(client meilisearch.ServiceManager, indexUID string) error {
fmt.Printf("Creating index '%s'...\n", indexUID)
task, err := client.CreateIndex(&meilisearch.IndexConfig{
Uid: indexUID,
PrimaryKey: "id",
})
if err != nil {
log.Printf("Index might already exist: %v", err)
return nil // Continue if index exists
}
return waitForTask(client, task.TaskUID)
}
func waitForTask(client meilisearch.ServiceManager, taskUID int64) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err := client.WaitForTaskWithContext(ctx, taskUID, 100*time.Millisecond)
return err
}
// getenv returns the value of the environment variable named by the key,
// or def if the variable is not present or empty.
func getenv(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}
|