aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/tensor/hnsw_best_neighbors/hnsw_best_neighbors_test.cpp
blob: 13430a8f2a90b30651c63c8987d78c44891ffd1b (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/searchlib/tensor/hnsw_multi_best_neighbors.h>
#include <vespa/searchlib/tensor/hnsw_single_best_neighbors.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <ostream>

using vespalib::datastore::EntryRef;
using search::tensor::HnswMultiBestNeighbors;
using search::tensor::HnswSingleBestNeighbors;
using search::tensor::NearestNeighborIndex;

using Neighbor = NearestNeighborIndex::Neighbor;

namespace search::tensor
{

std::ostream& operator<<(std::ostream& os, const Neighbor& neighbor) {
    os << "{ docid=" << neighbor.docid << ", distance=" << neighbor.distance << "}";
    return os;
}

}

struct DocidThenDistanceOrder
{
    bool operator()(const Neighbor& lhs, const Neighbor& rhs) const {
        if (lhs.docid != rhs.docid) {
            return lhs.docid < rhs.docid;
        }
        return lhs.distance < rhs.distance;
    }
};

template <typename BestNeighbors>
class HnswBestNeighborsTest : public testing::Test {
protected:
    BestNeighbors _neighbors;
    HnswBestNeighborsTest()
        : testing::Test(),
          _neighbors()
    {
        populate();
    }
    ~HnswBestNeighborsTest() override;

    void add(uint32_t nodeid, uint32_t docid, double distance) {
        _neighbors.emplace(nodeid, docid, EntryRef(), distance);
    }

    size_t size() const { return _neighbors.size(); }

    void assert_neighbors(std::vector<NearestNeighborIndex::Neighbor> exp, uint32_t k, double distance_limit) {
        auto neighbors_copy = _neighbors;
        auto act = neighbors_copy.get_neighbors(k, distance_limit);
        std::sort(act.begin(), act.end(), DocidThenDistanceOrder());
        EXPECT_EQ(exp, act);
    }

    void populate() {
        add(3, 3, 7.0);
        add(2, 2, 10.0);
        add(1, 1, 1.0);
        add(4, 2, 5.0);
    }
};

template <typename BestNeighbors>
HnswBestNeighborsTest<BestNeighbors>::~HnswBestNeighborsTest() = default;

using HnswBestNeighborsTypedTestTypes = testing::Types<HnswSingleBestNeighbors, HnswMultiBestNeighbors>;

TYPED_TEST_SUITE(HnswBestNeighborsTest, HnswBestNeighborsTypedTestTypes);

TYPED_TEST(HnswBestNeighborsTest, k_limit_is_enforced)
{
    this->assert_neighbors({}, 0, 40.0);
    this->assert_neighbors({{1, 1.0}}, 1, 40.0);
    this->assert_neighbors({{1, 1.0}, {2, 5.0}}, 2, 40.0);
    this->assert_neighbors({{1, 1.0}, {2, 5.0}, {3, 7.0}}, 3, 40.0);
}

TYPED_TEST(HnswBestNeighborsTest, distance_limit_is_enforced)
{
    this->assert_neighbors({}, 40, 0.5);
    this->assert_neighbors({{1, 1.0}}, 40, 1.0);
    this->assert_neighbors({{1, 1.0}, {2, 5.0}}, 40, 5.0);
    this->assert_neighbors({{1, 1.0}, {2, 5.0}, {3, 7.0}}, 40, 7.0);
}

using HnswSingleBestNeighborsTest = HnswBestNeighborsTest<HnswSingleBestNeighbors>;

TEST_F(HnswSingleBestNeighborsTest, duplicate_docids_are_not_elimiated)
{
    EXPECT_EQ(4, size());
    assert_neighbors({{1, 1.0}, {2, 5.0}, {2, 10.0}, {3, 7.0}}, 40, 40.0);
}

using HnswMultiBestNeighborsTest = HnswBestNeighborsTest<HnswMultiBestNeighbors>;

TEST_F(HnswMultiBestNeighborsTest, duplicate_docids_are_eliminated)
{
    EXPECT_EQ(3, size());
    assert_neighbors({{1, 1.0}, {2, 5.0}, {3, 7.0}}, 40, 40.0);
}

GTEST_MAIN_RUN_ALL_TESTS()