-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_test.go
110 lines (99 loc) · 3.06 KB
/
query_test.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
package vector
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestQuery(t *testing.T) {
for _, ns := range namespaces {
t.Run("namespace_"+ns, func(t *testing.T) {
client, err := newTestClient()
require.NoError(t, err)
namespace := client.Namespace(ns)
id0 := randomString()
id1 := randomString()
id2 := randomString()
err = namespace.UpsertMany([]Upsert{
{
Id: id0,
Vector: []float32{0, 1},
Metadata: map[string]any{"foo": "bar"},
Data: "vector0 data",
},
{
Id: id1,
Vector: []float32{5, 10},
Data: "vector1 data",
},
{
Id: id2,
Vector: []float32{0.01, 1.01},
Metadata: map[string]any{"foo": "nay"},
},
})
require.NoError(t, err)
require.Eventually(t, func() bool {
info, err := client.Info()
require.NoError(t, err)
return info.PendingVectorCount == 0
}, 10*time.Second, 1*time.Second)
t.Run("score", func(t *testing.T) {
scores, err := namespace.Query(Query{
Vector: []float32{0, 1},
TopK: 2,
})
require.NoError(t, err)
require.Equal(t, 2, len(scores))
require.Equal(t, id0, scores[0].Id)
require.Equal(t, float32(1.0), scores[0].Score)
require.Equal(t, id2, scores[1].Id)
})
t.Run("with metadata and vectors", func(t *testing.T) {
scores, err := namespace.Query(Query{
Vector: []float32{0, 1},
TopK: 2,
IncludeMetadata: true,
IncludeVectors: true,
IncludeData: true,
})
require.NoError(t, err)
require.Equal(t, 2, len(scores))
require.Equal(t, id0, scores[0].Id)
require.Equal(t, float32(1.0), scores[0].Score)
require.Equal(t, map[string]any{"foo": "bar"}, scores[0].Metadata)
require.Equal(t, "vector0 data", scores[0].Data)
require.Equal(t, []float32{0, 1}, scores[0].Vector)
require.Equal(t, id2, scores[1].Id)
require.Equal(t, []float32{0.01, 1.01}, scores[1].Vector)
require.Equal(t, map[string]any{"foo": "nay"}, scores[1].Metadata)
require.Empty(t, scores[1].Data)
})
t.Run("with metadata filtering", func(t *testing.T) {
query := Query{
Vector: []float32{0, 1},
TopK: 10,
IncludeMetadata: true,
IncludeVectors: true,
IncludeData: true,
Filter: `foo = 'bar'`,
}
scores, err := namespace.Query(query)
require.NoError(t, err)
require.Equal(t, 1, len(scores))
require.Equal(t, id0, scores[0].Id)
require.Equal(t, float32(1.0), scores[0].Score)
require.Equal(t, map[string]any{"foo": "bar"}, scores[0].Metadata)
require.Equal(t, "vector0 data", scores[0].Data)
require.Equal(t, []float32{0, 1}, scores[0].Vector)
query.Filter = `foo = 'nay'`
scores, err = namespace.Query(query)
require.NoError(t, err)
require.Equal(t, 1, len(scores))
require.Equal(t, id2, scores[0].Id)
require.Equal(t, map[string]any{"foo": "nay"}, scores[0].Metadata)
require.Equal(t, []float32{0.01, 1.01}, scores[0].Vector)
require.Empty(t, scores[0].Data)
})
})
}
}