| | |
| | | import ( |
| | | "fmt" |
| | | "os" |
| | | "strings" |
| | | |
| | | "github.com/aisim/kb-cli/internal/index" |
| | | "github.com/aisim/kb-cli/internal/search" |
| | |
| | | var searchCmd = &cobra.Command{ |
| | | Use: "search [keywords...]", |
| | | Short: "搜索知识库", |
| | | Long: `# search - 搜索 |
| | | kb search <关键词> [--top N] [--expanded <词>] [--symptom <词>] [--with-content] [--with-links] [--json] # 搜索知识库,支持关键词、扩展词、症状词`, |
| | | Args: cobra.MinimumNArgs(1), |
| | | RunE: runSearch, |
| | | Long: `# search - 搜索 |
| | | kb-cli search <关键词> [--top N] [--expanded <词>] [--symptom <词>] [--with-content] [--with-links] [--json] # 搜索知识库,支持关键词、扩展词、症状词`, |
| | | Args: cobra.MinimumNArgs(1), |
| | | RunE: runSearch, |
| | | } |
| | | |
| | | func init() { |
| | |
| | | } |
| | | } |
| | | |
| | | // 合并所有关键词(支持空格分隔和引号分隔) |
| | | var allKeywords []string |
| | | for _, arg := range args { |
| | | // 如果参数包含空格,按空格分割 |
| | | parts := strings.Fields(arg) |
| | | allKeywords = append(allKeywords, parts...) |
| | | } |
| | | |
| | | if len(allKeywords) == 0 { |
| | | return fmt.Errorf("请提供至少一个关键词") |
| | | } |
| | | |
| | | // 执行搜索 |
| | | opts := search.SearchOptions{ |
| | | Expanded: expanded, |
| | |
| | | WithLinks: withLinks, |
| | | } |
| | | |
| | | results, err := search.Search(store, args, opts) |
| | | results, err := search.Search(store, allKeywords, opts) |
| | | if err != nil { |
| | | return fmt.Errorf("搜索失败: %w", err) |
| | | } |