-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.go
116 lines (99 loc) · 2.37 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (C) 2024-2025 by Ubaldo Porcheddu <ubaldo@eja.it>
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func Search(query string, limit int) ([]SearchResult, error) {
var results []SearchResult
log.Println("FTS title searching", query)
titles, err := db.SearchTitle(query, limit)
if err != nil {
return nil, err
}
for _, title := range titles {
results = append(results, title)
}
log.Println("FTS content searching", query)
contents, err := db.SearchContent(query, limit)
if err != nil {
return nil, err
}
for _, content := range contents {
results = append(results, content)
}
if ai {
log.Println("Vectors searching", query)
vectors, err := db.SearchVectors(query, limit)
if err != nil {
return nil, err
}
for _, vector := range vectors {
results = append(results, vector)
}
}
return searchOptimize(results), nil
}
func SearchCli() error {
reader := bufio.NewReader(os.Stdin)
articles := make(map[int]int)
for {
fmt.Print("> ")
query, _ := reader.ReadString('\n')
query = strings.TrimSpace(query)
if query == "" {
return nil
}
queryIdx, err := strconv.Atoi(query)
if err == nil {
if articleID, exists := articles[queryIdx]; exists {
article, err := db.ArticleGet(articleID)
if err != nil {
log.Fatal("CLI error: ", err)
}
for _, entry := range article {
fmt.Printf("%s\n\n", entry.Title)
for _, section := range entry.Sections {
fmt.Printf("%s\n\n", section.Title)
for _, text := range section.Texts {
fmt.Println(text)
}
fmt.Println()
}
}
continue
}
}
results, err := Search(query, options.limit)
if err != nil {
log.Fatal("CLI error: ", err)
}
articles = make(map[int]int)
for i, result := range results {
articles[i+1] = result.ArticleID
fmt.Printf("% 3d [%s] %s\n", i+1, result.Type, result.Title)
}
}
}
func searchOptimize(results []SearchResult) []SearchResult {
seen := make(map[int]bool)
accumulatedResults := []SearchResult{}
for _, result := range results {
if !seen[result.ArticleID] {
seen[result.ArticleID] = true
accumulatedResults = append(accumulatedResults, result)
} else {
for i := range accumulatedResults {
if accumulatedResults[i].ArticleID == result.ArticleID {
accumulatedResults[i].Power += result.Power
break
}
}
}
}
return accumulatedResults
}