Esempio n. 1
0
        public void AddWithIdsTest()
        {
            int            dimension    = 128;
            int            vectorsCount = 5;
            int            idStart      = 5;
            int            k            = 5;
            List <float[]> vectors      = new List <float[]>();

            for (int i = 0; i < vectorsCount; i++)
            {
                vectors.Add(new float[dimension]);
                for (int j = 0; j < dimension; j++)
                {
                    vectors[i][j] = DRand();
                }
            }
            using (var index = new IndexFlatL2(dimension))
            {
                using (var idIndex = new IndexIDMap(index))
                {
                    idIndex.Add(vectors, Enumerable.Range(idStart, idStart + vectorsCount).Select(r => (long)r));
                    var result = idIndex.Search(vectors.Select(v => v), k);
                    foreach (var m in result)
                    {
                        Assert.InRange(m.Label, idStart, idStart + vectorsCount - 1);
                    }
                }
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            int            dimension    = 128;
            int            vectorsCount = 25;
            int            k            = 5;
            int            idStart      = 50;
            List <float[]> vectors      = new List <float[]>();

            System.Console.WriteLine($"Generating {vectorsCount} vectors with users generated ids from {idStart} to {vectorsCount - 1}...");

            for (int i = 0; i < vectorsCount; i++)
            {
                vectors.Add(new float[dimension]);
                for (int j = 0; j < dimension; j++)
                {
                    vectors[i][j] = DRand();
                }
            }
            System.Console.WriteLine("Building index");
            using (var index = new IndexFlatL2(dimension))
            {
                using (var idIndex = new IndexIDMap(index))
                {
                    System.Console.WriteLine($"IsTrained {index.IsTrained}");

                    idIndex.Add(vectors, Enumerable.Range(idStart, idStart + vectorsCount).Select(r => (long)r));
                    System.Console.WriteLine($"Elements in index: {index.Count}");

                    System.Console.WriteLine("Searching...");
                    var result = idIndex.Search(vectors.Take(5).Select(v => v), k).ToArray();
                    for (int i = 0; i < 5; i++)
                    {
                        var partialResult = result[(i * k)..(i * k + k)];