-
Notifications
You must be signed in to change notification settings - Fork 104
feat: add runnable examples for search and chat streaming #703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d95b87b
feat: add runnable examples for search and chat streaming
r-sahni ffc404a
feat: Add comprehensive examples with CodeRabbit AI improvements
r-sahni 79b5b90
fix: Apply final CodeRabbit suggestions
r-sahni 2616275
docs: Align all README files with actual Go implementations
r-sahni 81a4241
docs: Align README files with comprehensive structure
r-sahni c10d1bf
Aligned the readmes
r-sahni 1fe04d8
updated docs
r-sahni 85712c9
Merge branch 'main' into add-examples-usage
RISHABH4SAHNI 425515a
Corrected linter error
r-sahni 83fb23c
Merge branch 'add-examples-usage' of github.com:RISHABH4SAHNI/meilise…
r-sahni 42723d6
Corrected linter error
r-sahni b0651c9
Corrected linter error
r-sahni 9c0ea23
Corrected linter error
r-sahni dad3465
Corrected linter error
r-sahni 0f77661
Corrected linter error
r-sahni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Document Management Example | ||
|
||
This example shows how to manage documents in Meilisearch. You'll learn to add, update, retrieve, and delete documents. | ||
|
||
## What it does | ||
|
||
```go | ||
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"` | ||
} | ||
``` | ||
|
||
1. Create a "users" index | ||
2. Add multiple documents at once | ||
3. Add single documents | ||
4. Update existing documents | ||
5. Get documents by ID | ||
6. Get multiple documents with pagination | ||
7. Delete single document | ||
8. Delete multiple documents | ||
|
||
## Configuration | ||
|
||
```bash | ||
# Set Meilisearch server URL (defaults to http://localhost:7700) | ||
export MEILI_HOST="http://localhost:7700" | ||
|
||
# Set API key (recommended for production) | ||
export MEILI_API_KEY="your-api-key" | ||
``` | ||
|
||
## Run it | ||
|
||
```bash | ||
go run ./examples/add_documents | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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: "[email protected]", Role: "admin", Active: true, JoinDate: "2023-01-15"}, | ||
{ID: 2, Name: "Bob Smith", Email: "[email protected]", Role: "user", Active: true, JoinDate: "2023-02-20"}, | ||
{ID: 3, Name: "Charlie Brown", Email: "[email protected]", Role: "moderator", Active: false, JoinDate: "2023-03-10"}, | ||
} | ||
|
||
task, err := index.AddDocuments(users, nil) | ||
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: "[email protected]", | ||
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: "[email protected]", Role: "admin", Active: true, JoinDate: "2023-02-20"}, | ||
{ID: 3, Name: "Charlie Brown", Email: "[email protected]", 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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Chat Streaming Example | ||
|
||
This example shows how to use Meilisearch's chat features with streaming responses. | ||
|
||
## What it does | ||
|
||
- Check server health | ||
- List available chat workspaces | ||
- Start an interactive chat session | ||
- Stream responses in real-time | ||
- Type 'quit' to exit | ||
|
||
## Prerequisites | ||
|
||
**Meilisearch Enterprise** with chat features enabled | ||
|
||
## Configuration | ||
|
||
```bash | ||
export MEILI_HOST="http://localhost:7700" | ||
export MEILI_API_KEY="your-enterprise-api-key" | ||
``` | ||
|
||
## Run it | ||
|
||
```bash | ||
go run ./examples/chat_stream | ||
``` | ||
``` | ||
|
||
The example will start an interactive chat session where you can ask questions and receive streaming responses. Type 'quit' or 'exit' to end the session. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.