Files
steve 9aab79d49b v2 restructure: Go client, Docker engine, release tooling
- Remove v1 Python CLI (src/kb_search/, tests/, root pyproject.toml, uv.lock, .venv)
- Add Go client with cross-platform build (client/)
- Add FastAPI engine with NVIDIA and multi-stage ROCm Dockerfiles (engine/)
- Add VERSION files for client and engine, wired into builds
- Add release.sh for automated build, tag, release, and Docker push
- Update README with build/release docs and ROCm migration note
- Clean up .gitignore for v2 project structure

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 21:52:25 +00:00

143 lines
3.2 KiB
Go

package cmd
import (
"fmt"
"os"
"github.com/kb-search/kb/internal/api"
"github.com/kb-search/kb/internal/output"
"github.com/spf13/cobra"
)
var searchCmd = &cobra.Command{
Use: "search <query>",
Short: "Search the knowledge base",
Args: cobra.ExactArgs(1),
RunE: runSearch,
}
func init() {
searchCmd.Flags().IntP("top", "n", 10, "number of results to return")
searchCmd.Flags().String("tags", "", "filter by tags (comma-separated)")
searchCmd.Flags().String("type", "", "filter by document type")
searchCmd.Flags().Bool("fts-only", false, "use full-text search only")
searchCmd.Flags().Bool("vec-only", false, "use vector search only")
searchCmd.Flags().Float64("threshold", 0, "minimum score threshold")
rootCmd.AddCommand(searchCmd)
}
func runSearch(cmd *cobra.Command, args []string) error {
top, _ := cmd.Flags().GetInt("top")
tags, _ := cmd.Flags().GetString("tags")
docType, _ := cmd.Flags().GetString("type")
ftsOnly, _ := cmd.Flags().GetBool("fts-only")
vecOnly, _ := cmd.Flags().GetBool("vec-only")
threshold, _ := cmd.Flags().GetFloat64("threshold")
body := map[string]interface{}{
"query": args[0],
"top": top,
}
if tags != "" {
body["tags"] = tags
}
if docType != "" {
body["type"] = docType
}
if ftsOnly {
body["fts_only"] = true
}
if vecOnly {
body["vec_only"] = true
}
if threshold > 0 {
body["threshold"] = threshold
}
client := api.NewClient()
resp, err := client.Post("/api/v1/search", body)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := api.CheckError(resp); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
var result struct {
Results []struct {
Score float64 `json:"score"`
Document struct {
Title string `json:"title"`
Type string `json:"doc_type"`
Tags []string `json:"tags"`
} `json:"document"`
Page interface{} `json:"page"`
Section string `json:"section"`
Text string `json:"text"`
} `json:"results"`
}
if output.IsJSON() {
var raw interface{}
if err := api.DecodeJSON(resp, &raw); err != nil {
return fmt.Errorf("failed to decode response: %w", err)
}
output.PrintJSON(raw)
return nil
}
if err := api.DecodeJSON(resp, &result); err != nil {
return fmt.Errorf("failed to decode response: %w", err)
}
if len(result.Results) == 0 {
fmt.Println("No results found.")
return nil
}
for i, r := range result.Results {
snippet := r.Text
if len(snippet) > 200 {
snippet = snippet[:200] + "..."
}
fmt.Printf("\n%d. [%.4f] %s\n", i+1, r.Score, r.Document.Title)
location := ""
if r.Page != nil {
location = fmt.Sprintf("Page %v", r.Page)
}
if r.Section != "" {
if location != "" {
location += " / "
}
location += r.Section
}
if location != "" {
fmt.Printf(" Location: %s\n", location)
}
if r.Document.Type != "" {
fmt.Printf(" Type: %s\n", r.Document.Type)
}
if len(r.Document.Tags) > 0 {
fmt.Printf(" Tags: %s\n", joinStrings(r.Document.Tags))
}
fmt.Printf(" %s\n", snippet)
}
fmt.Println()
return nil
}
func joinStrings(ss []string) string {
result := ""
for i, s := range ss {
if i > 0 {
result += ", "
}
result += s
}
return result
}