-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb_search.go
223 lines (195 loc) · 5.09 KB
/
db_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (C) 2024-2025 by Ubaldo Porcheddu <ubaldo@eja.it>
package main
import (
"database/sql"
)
func (h *DBHandler) SearchTitle(searchQuery string, limit int) ([]SearchResult, error) {
sqlQuery := `
SELECT rowid, title, bm25(article_search) AS power
FROM article_search
WHERE article_search MATCH ?
ORDER BY power ASC
LIMIT ?
`
rows, err := h.db.Query(sqlQuery, searchQuery, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var results []SearchResult
for rows.Next() {
var result SearchResult
if err := rows.Scan(
&result.ArticleID,
&result.Title,
&result.Power,
); err != nil {
return nil, err
}
contentQuery := `
SELECT text
FROM hashes
WHERE id = (
SELECT hash_id
FROM content
WHERE section_id = (
SELECT id
FROM sections
WHERE article_id = ?
LIMIT 1
)
)
`
err = h.db.QueryRow(contentQuery, result.ArticleID, result.ArticleID).Scan(&result.Text)
if err != nil && err != sql.ErrNoRows {
return nil, err
}
result.Type = "T"
results = append(results, result)
}
return results, nil
}
func (h *DBHandler) SearchContent(searchQuery string, limit int) ([]SearchResult, error) {
sqlQuery := `
SELECT rowid, text, bm25(hash_search) as power
FROM hash_search
WHERE hash_search MATCH ?
ORDER BY power
LIMIT ?;
`
rows, err := h.db.Query(sqlQuery, searchQuery, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var results []SearchResult
for rows.Next() {
var result SearchResult
var contentID int
if err := rows.Scan(&contentID, &result.Text, &result.Power); err != nil {
return nil, err
}
articleQuery := `
SELECT id, title
FROM articles
WHERE articles.id = (
SELECT article_id
FROM sections
WHERE sections.id =
(SELECT section_id FROM content WHERE content.hash_id=?)
)
`
err = h.db.QueryRow(articleQuery, contentID).Scan(&result.ArticleID, &result.Title)
if err != nil && err != sql.ErrNoRows {
return nil, err
}
result.Type = "C"
results = append(results, result)
}
return results, nil
}
func (h *DBHandler) SearchVectors(query string, limit int) ([]SearchResult, error) {
annLimit := limit * 8
queryEmbedding, err := aiEmbeddings(query)
if err != nil {
return nil, err
}
quantizedQuery := aiQuantizeBinary(queryEmbedding)
rows, err := h.db.Query("SELECT id, chunk FROM vectors_ann_chunks")
if err != nil {
return nil, err
}
defer rows.Close()
type VectorDistance struct {
ID int64
ChunkRowID int64
ChunkPosition int
Distance float32
}
topANNResults := make([]VectorDistance, 0, annLimit)
for rows.Next() {
var chunkRowID int64
var chunkBlob []byte
chunkSize := len(quantizedQuery)
if err := rows.Scan(&chunkRowID, &chunkBlob); err != nil {
return nil, err
}
for position := 0; position < len(chunkBlob); position += chunkSize {
var result VectorDistance
embeddingBlob := chunkBlob[position:(position + chunkSize)]
distance, err := aiHammingDistance(quantizedQuery, embeddingBlob)
if err != nil {
return nil, err
}
result.ChunkRowID = chunkRowID
result.ChunkPosition = position / chunkSize
result.Distance = distance
if len(topANNResults) < annLimit {
topANNResults = append(topANNResults, result)
} else {
for i := range topANNResults {
if topANNResults[i].Distance > distance {
topANNResults[i] = result
break
}
}
}
}
}
for k, v := range topANNResults {
var vectors_id int64
if err := h.db.QueryRow("SELECT vectors_id FROM vectors_ann_index WHERE chunk_id = ? AND chunk_position = ? LIMIT 1", v.ChunkRowID, v.ChunkPosition).Scan(&vectors_id); err != nil {
return nil, err
}
topANNResults[k].ID = vectors_id
}
topResults := make([]VectorDistance, 0, limit)
for _, annResult := range topANNResults {
var embeddingBlob []byte
err := h.db.QueryRow("SELECT embedding FROM vectors WHERE id = ?", annResult.ID).Scan(&embeddingBlob)
if err != nil && err != sql.ErrNoRows {
return nil, err
}
embedding := aiBytesToFloat32(embeddingBlob)
distance, err := aiL2Distance(queryEmbedding, embedding)
if err != nil {
return nil, err
}
if len(topResults) < limit {
topResults = append(topResults, VectorDistance{ID: annResult.ID, Distance: float32(distance)})
} else {
for i := range topResults {
if topResults[i].Distance > distance {
topResults[i] = VectorDistance{ID: annResult.ID, Distance: float32(distance)}
break
}
}
}
}
var results []SearchResult
for _, vd := range topResults {
sqlQuery := `
SELECT id, title, (SELECT text FROM hashes WHERE id=?) AS text
FROM articles
WHERE articles.id = (
SELECT article_id
FROM sections
WHERE sections.id =
(SELECT section_id FROM content WHERE content.hash_id=? LIMIT 1)
)
`
var result SearchResult
err := h.db.QueryRow(sqlQuery, vd.ID, vd.ID).Scan(
&result.ArticleID,
&result.Title,
&result.Text,
)
if err != nil && err != sql.ErrNoRows {
return nil, err
}
result.Type = "V"
result.Power = float64(vd.Distance)
results = append(results, result)
}
return results, nil
}