cli + tui to publish to leaflet (wip) & manage tasks, notes & watch/read lists 馃崈
charm
leaflet
readability
golang
1package main
2
3import (
4 "strings"
5
6 "github.com/spf13/cobra"
7 "github.com/stormlightlabs/noteleaf/internal/handlers"
8)
9
10// SearchCommand implements [CommandGroup] for document search commands
11type SearchCommand struct {
12 handler *handlers.DocumentHandler
13}
14
15// NewSearchCommand creates a new search command group
16func NewSearchCommand(handler *handlers.DocumentHandler) *SearchCommand {
17 return &SearchCommand{handler: handler}
18}
19
20func (c *SearchCommand) Create() *cobra.Command {
21 root := &cobra.Command{
22 Use: "search",
23 Short: "Search notes using TF-IDF",
24 Long: `Full-text search for notes using Term Frequency-Inverse Document Frequency (TF-IDF) ranking.
25
26The search engine tokenizes text, builds an inverted index, and ranks results by relevance.
27Results are sorted by TF-IDF score, with higher scores indicating better matches.`,
28 }
29
30 queryCmd := &cobra.Command{
31 Use: "query [search terms...]",
32 Short: "Search for documents matching query terms",
33 Long: `Search for documents using TF-IDF ranking.
34
35Examples:
36 noteleaf search query go programming
37 noteleaf search query "machine learning" --limit 5`,
38 Args: cobra.MinimumNArgs(1),
39 RunE: func(cmd *cobra.Command, args []string) error {
40 query := strings.Join(args, " ")
41 limit, _ := cmd.Flags().GetInt("limit")
42
43 return c.handler.Search(cmd.Context(), query, limit)
44 },
45 }
46 queryCmd.Flags().IntP("limit", "l", 10, "Maximum number of results to return")
47 root.AddCommand(queryCmd)
48
49 rebuildCmd := &cobra.Command{
50 Use: "rebuild",
51 Short: "Rebuild search index from notes",
52 Long: `Rebuild the search index from all notes in the database.
53
54This command:
551. Clears the existing document index
562. Copies all notes to the documents table
573. Builds a new TF-IDF search index
58
59Run this after adding, updating, or deleting notes to refresh the search index.`,
60 RunE: func(cmd *cobra.Command, args []string) error {
61 return c.handler.RebuildIndex(cmd.Context())
62 },
63 }
64 root.AddCommand(rebuildCmd)
65
66 return root
67}