Skip to content

Commit

Permalink
add: multiple vector test
Browse files Browse the repository at this point in the history
  • Loading branch information
iammytoo committed Sep 12, 2024
1 parent b3ca54f commit 045f024
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 6 deletions.
8 changes: 5 additions & 3 deletions internal/core/algorithm/usearch/usearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package usearch

import (
"strconv"
"sync"

core "github.com/unum-cloud/usearch/golang"
Expand Down Expand Up @@ -202,7 +203,6 @@ func (u *usearch) Search(q []float32, k int) ([]algorithm.SearchResult, error) {
if len(I) == 0 || len(D) == 0 {
return nil, errors.ErrEmptySearchResult
}

result := make([]algorithm.SearchResult, min(len(I), k))
for i := range result {
result[i] = algorithm.SearchResult{ID: uint32(I[i]), Distance: D[i], Error: nil}
Expand All @@ -218,9 +218,11 @@ func (u *usearch) GetObject(key core.Key, count int) ([]float32, error) {
if err != nil {
return nil, errors.NewUsearchError("failed to usearch_get")
}
// ASK: 何か適切なerrorがある?

if vectors == nil {
return nil, nil
return nil, errors.ErrObjectNotFound(
errors.NewUsearchError("failed to usearch_get"), strconv.Itoa(int(key)),
)
}

return vectors, nil
Expand Down
90 changes: 87 additions & 3 deletions internal/core/algorithm/usearch/usearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package usearch

import (
"math"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -136,8 +137,8 @@ func Test_usearch_Search(t *testing.T) {
return nil, err
}

for i := range poolSize {
if err := u.Add(uint64(i+1), vecs[i]); err != nil {
for i, v := range vecs {
if err := u.Add(uint64(i+1), v); err != nil {
t.Error(err)
return nil, err
}
Expand All @@ -146,7 +147,6 @@ func Test_usearch_Search(t *testing.T) {
return u, nil
}
tests := []test{
// object type uint8
{
name: "return vector id after the same vector inserted",
args: args{
Expand Down Expand Up @@ -203,6 +203,90 @@ func Test_usearch_Search(t *testing.T) {
},
},
},
{
name: "return limited result after insert 10 vectors with limited size 3",
args: args{
q: []float32{1, 2, 3, 4, 5, 6, 7, 8, 9},
k: 3,
},
fields: fields{
idxPath: idxTempDir(t),
quantizationType: "F32",
metricType: "cosine",
dimension: 9,
connectivity: 0,
expansionAdd: 0,
expansionSearch: 0,
multi: false,
},
createFunc: func(t *testing.T, fields fields) (Usearch, error) {
t.Helper()
ivs := [][]float32{ // insert 10 vec
{0, 1, 2, 3, 4, 5, 6, 7, 8},
{2, 3, 4, 5, 6, 7, 8, 9, 10},
{0, 1, 2, 3, 4, 5, 6, 7, 8},
{2, 3, 4, 5, 6, 7, 8, 9, 10},
{0, 1, 2, 3, 4, 5, 6, 7, 8},
{2, 3, 4, 5, 6, 7, 8, 9, 10},
{0, 1, 2, 3, 4, 5, 6, 7, 8},
{2, 3, 4, 5, 6, 7, 8, 9, 10},
{2, 3, 4, 5, 6, 7, 8, 9, 10},
{2, 3, 4, 5, 6, 7, 8, 9, math.MaxFloat32},
}

return insertCreateFunc(t, fields, ivs, 10)
},
want: want{
want: []algorithm.SearchResult{
{ID: uint32(10), Distance: 3},
{ID: uint32(9), Distance: 3},
{ID: uint32(8), Distance: 3},
},
},
},
{
name: "return most accurate result after insert 10 vectors with limited size 5",
args: args{
q: []float32{1, 2, 3, 4, 5, 6, 7, 8, 9},
k: 5,
},
fields: fields{
idxPath: idxTempDir(t),
quantizationType: "F32",
metricType: "cosine",
dimension: 9,
connectivity: 0,
expansionAdd: 0,
expansionSearch: 0,
multi: false,
},
createFunc: func(t *testing.T, fields fields) (Usearch, error) {
t.Helper()
ivs := [][]float32{
{0, 1, 2, 3, 4, 5, 6, 7, 8}, // vec id 1
{2, 3, 4, 5, 6, 7, 8, 9, 10}, // vec id 2
{0, 1, 2, 3, 4, 5, 6, 7, 8}, // vec id 3
{2, 3, 4, 5, 6, 7, 8, 9, 10}, // vec id 4
{0, 1, 2, 3, 4, 5, 6, 7, 8}, // vec id 5
{2, 3, 4, 5, 6, 7, 8, 9, 10}, // vec id 6
{2, 3, 4, 5, 6, 7, 8, 9, 9.04}, // vec id 7
{2, 3, 4, 5, 6, 7, 8, 9, 9.03}, // vec id 8
{1, 2, 3, 4, 5, 6, 7, 8, 9.01}, // vec id 9
{1, 2, 3, 4, 5, 6, 7, 8, 9.02}, // vec id 10
}

return insertCreateFunc(t, fields, ivs, 10)
},
want: want{
want: []algorithm.SearchResult{
{ID: uint32(9), Distance: 2.384185791015625e-07},
{ID: uint32(10), Distance: 5.364418029785156e-07},
{ID: uint32(6), Distance: 3},
{ID: uint32(4), Distance: 3},
{ID: uint32(2), Distance: 3},
},
},
},
{
name: "return nothing if the search dimension is less than the inserted vector",
args: args{
Expand Down

0 comments on commit 045f024

Please sign in to comment.