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 ", 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"` Title string `json:"title"` DocType string `json:"doc_type"` Tags []string `json:"tags"` ChunkMetadata map[string]interface{} `json:"chunk_metadata"` 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.Title) location := "" if page, ok := r.ChunkMetadata["page"]; ok && page != nil { location = fmt.Sprintf("Page %v", page) } if section, ok := r.ChunkMetadata["section_header"]; ok && section != nil { if s, ok := section.(string); ok && s != "" { if location != "" { location += " / " } location += s } } if location != "" { fmt.Printf(" Location: %s\n", location) } if r.DocType != "" { fmt.Printf(" Type: %s\n", r.DocType) } if len(r.Tags) > 0 { fmt.Printf(" Tags: %s\n", joinStrings(r.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 }