From c9fed28b9bc2252d6293d655ac97cc5d0c30dde7 Mon Sep 17 00:00:00 2001 From: oriHab Date: Thu, 1 Jul 2021 08:55:33 +0300 Subject: [PATCH] _example/flat_index_map.go: example test added to demonstrate usage --- _example/flat_index_map/flat_index_map.go | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 _example/flat_index_map/flat_index_map.go diff --git a/_example/flat_index_map/flat_index_map.go b/_example/flat_index_map/flat_index_map.go new file mode 100644 index 0000000..3ba6f8b --- /dev/null +++ b/_example/flat_index_map/flat_index_map.go @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" + "github.com/DataIntelligenceCrew/go-faiss" +) + +func main() { + dimension := 1 + dbSize := 5 + + index, err := faiss.NewIndexFlat(dimension, faiss.MetricL2) + if err != nil { + fmt.Println(err.Error()) + } + indexMap, err := faiss.NewIndexIDMap(index) + if err != nil { + fmt.Println(err.Error()) + } + xb := []float32{1,2,3,4,5} + ids := make([]int64, dbSize) + for i := 0; i < dbSize; i++ { + ids[i] = int64(i) + } + + err = indexMap.AddWithIDs(xb, ids) + if err != nil { + fmt.Println(err.Error()) + } + toFind := xb[dimension:2*dimension] + distances1, resultIds, err := indexMap.Search(toFind, 5) + fmt.Println(distances1, resultIds, err) + fmt.Println(resultIds[0] == ids[1]) + fmt.Println(distances1[0] == 0) + +}