-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
297 lines (252 loc) · 7.6 KB
/
query.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package turbopg
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"strings"
)
// QueryOptions represents options for querying documents
type QueryOptions struct {
// Namespace to query
Namespace string
// Optional: Vector to search by similarity
// Note: When combining vector search with filters, documents with zero
// similarity to the query vector may be excluded from results, even if
// they match the filter conditions. This is an optimization by the
// underlying pgvector implementation.
Vector []float32
// Optional: Filter to apply
Filter Filter
// Number of results to return
TopK int
// Optional: Distance metric to use for vector search
// Default: "cosine"
Metric string
}
// QueryResult represents a single search result with its score
type QueryResult struct {
// Document that matched the query
Document Document
// Score represents the similarity/distance score
// Lower is better for distance metrics (L2)
// Higher is better for similarity metrics (cosine)
Score float64
}
// SearchVector finds the top-K most similar vectors in a namespace
func (s *Store) SearchVector(ctx context.Context, namespace string, vector []float32, topK int, metric string) ([]QueryResult, error) {
// Validate namespace
ns, err := s.GetNamespace(ctx, namespace)
if err != nil {
return nil, err
}
// Validate vector dimensions
if len(vector) != ns.Dimensions {
return nil, fmt.Errorf("vector dimensions mismatch: got %d, want %d", len(vector), ns.Dimensions)
}
// Validate topK
if topK <= 0 {
return nil, fmt.Errorf("topK must be positive, got %d", topK)
}
// Choose operator based on metric
var operator string
switch metric {
case "cosine":
operator = "<=>"
case "euclidean":
operator = "<->"
case "euclidean_squared":
operator = "<#>"
default:
return nil, fmt.Errorf("unsupported distance metric: %s", metric)
}
// Build table name
tableName := GetNamespaceTableName(s.prefix, namespace)
// Build query
query := fmt.Sprintf(`
SELECT id, vector, attributes, (vector %s $1) as distance
FROM %s
ORDER BY vector %s $1
LIMIT $2`,
operator, tableName, operator)
// Convert vector to string format that pgvector expects: [1,2,3]
vectorStr := VectorToString(vector)
// Execute query and parse results
results, err := s.executeQueryAndParse(ctx, query, []interface{}{vectorStr, topK}, func(rows *sql.Rows) (*QueryResult, error) {
var (
doc Document
vectorStr string
attrsJSON []byte
distance float64
)
if err := rows.Scan(&doc.ID, &vectorStr, &attrsJSON, &distance); err != nil {
return nil, fmt.Errorf("scan result: %w", err)
}
// Parse vector
doc.Vector, err = StringToVector(vectorStr)
if err != nil {
return nil, err
}
// Parse attributes
if err := json.Unmarshal(attrsJSON, &doc.Attributes); err != nil {
return nil, fmt.Errorf("unmarshal attributes: %w", err)
}
return &QueryResult{
Document: doc,
Score: distance,
}, nil
})
if err != nil {
return nil, err
}
s.logger.Info("vector search completed",
Field{Key: "namespace", Value: namespace},
Field{Key: "top_k", Value: topK},
Field{Key: "metric", Value: metric},
Field{Key: "results", Value: len(results)},
)
return results, nil
}
// SearchFiltered finds the top-K most similar vectors that match the given filter
func (s *Store) SearchFiltered(ctx context.Context, namespace string, vector []float32, filter FilterCondition, topK int, metric string) ([]QueryResult, error) {
return s.Query(ctx, QueryOptions{
Namespace: namespace,
Vector: vector,
Filter: filter,
TopK: topK,
Metric: metric,
})
}
// Query searches for documents based on the provided options
func (s *Store) Query(ctx context.Context, opts QueryOptions) ([]QueryResult, error) {
// Validate namespace
ns, err := s.GetNamespace(ctx, opts.Namespace)
if err != nil {
return nil, err
}
// Validate vector dimensions if provided
if opts.Vector != nil && len(opts.Vector) != ns.Dimensions {
return nil, fmt.Errorf("vector dimensions mismatch: got %d, want %d", len(opts.Vector), ns.Dimensions)
}
// Validate topK
if opts.TopK <= 0 {
return nil, fmt.Errorf("topK must be positive, got %d", opts.TopK)
}
// Use default metric if not specified
metric := opts.Metric
if metric == "" {
metric = "cosine"
}
// Choose operator based on metric
var operator string
switch metric {
case "cosine":
operator = "<=>"
case "euclidean":
operator = "<->"
case "euclidean_squared":
operator = "<#>"
default:
return nil, fmt.Errorf("unsupported distance metric: %s", metric)
}
// Build table name
tableName := GetNamespaceTableName(s.prefix, opts.Namespace)
// Build query
var queryBuilder strings.Builder
var distanceExpr string
if opts.Vector != nil {
distanceExpr = fmt.Sprintf("(vector %s $1) as distance", operator)
} else {
distanceExpr = "0 as distance"
}
queryBuilder.WriteString(fmt.Sprintf(`
SELECT id, vector, attributes, %s
FROM %s`,
distanceExpr,
tableName))
// Add WHERE clause if filter provided
var args []interface{}
var argOffset int
if opts.Vector != nil {
vectorStr := VectorToString(opts.Vector)
args = append(args, vectorStr)
argOffset = 1
s.logger.Info("added vector argument",
Field{Key: "vector", Value: vectorStr},
Field{Key: "argOffset", Value: argOffset},
)
}
if opts.Filter != nil {
whereClause, filterArgs, err := buildFilterSQL(opts.Filter)
if err != nil {
return nil, fmt.Errorf("build filter: %w", err)
}
if whereClause != "" {
s.logger.Info("built filter condition",
Field{Key: "whereClause", Value: whereClause},
Field{Key: "filterArgs", Value: fmt.Sprintf("%v", filterArgs)},
Field{Key: "argOffset", Value: argOffset},
)
// Adjust placeholders for accumulated args
for i := 1; i <= strings.Count(whereClause, "$"); i++ {
old := fmt.Sprintf("$%d", i)
new := fmt.Sprintf("$%d", i+argOffset)
whereClause = strings.Replace(whereClause, old, new, -1)
}
queryBuilder.WriteString("\nWHERE " + whereClause)
args = append(args, filterArgs...)
s.logger.Info("adjusted filter placeholders",
Field{Key: "whereClause", Value: whereClause},
Field{Key: "args", Value: fmt.Sprintf("%v", args)},
)
}
}
// Add ORDER BY if vector search
if opts.Vector != nil {
queryBuilder.WriteString(fmt.Sprintf("\nORDER BY vector %s $1", operator))
}
// Add LIMIT
queryBuilder.WriteString(fmt.Sprintf("\nLIMIT $%d", len(args)+1))
args = append(args, opts.TopK)
// Execute query
s.logger.Info("executing query",
Field{Key: "query", Value: queryBuilder.String()},
Field{Key: "args", Value: fmt.Sprintf("%v", args)},
)
// Execute query and parse results
results, err := s.executeQueryAndParse(ctx, queryBuilder.String(), args, func(rows *sql.Rows) (*QueryResult, error) {
var (
doc Document
vectorStr string
attrsJSON []byte
distance float64
)
if err := rows.Scan(&doc.ID, &vectorStr, &attrsJSON, &distance); err != nil {
return nil, fmt.Errorf("scan result: %w", err)
}
// Parse vector
doc.Vector, err = StringToVector(vectorStr)
if err != nil {
return nil, err
}
// Parse attributes
if err := json.Unmarshal(attrsJSON, &doc.Attributes); err != nil {
return nil, fmt.Errorf("unmarshal attributes: %w", err)
}
return &QueryResult{
Document: doc,
Score: distance,
}, nil
})
if err != nil {
return nil, err
}
s.logger.Info("query completed",
Field{Key: "namespace", Value: opts.Namespace},
Field{Key: "top_k", Value: opts.TopK},
Field{Key: "has_vector", Value: opts.Vector != nil},
Field{Key: "has_filter", Value: opts.Filter != nil},
Field{Key: "results", Value: len(results)},
)
return results, nil
}