summaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/features/nns_distance/nns_distance_test.cpp
blob: fff4c9f1c0e24ced8cb867fbb8e482bf34e349aa (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
108
109
110
111
112
113
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/eval/eval/tensor_spec.h>
#include <vespa/searchlib/features/distancefeature.h>
#include <vespa/searchlib/fef/test/dummy_dependency_handler.h>
#include <vespa/searchlib/fef/test/labels.h>
#include <vespa/searchlib/test/features/distance_closeness_fixture.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/util/stringfmt.h>

using search::feature_t;
using namespace search::features::test;
using namespace search::features;
using namespace search::fef::test;
using namespace search::fef;

using vespalib::eval::TensorSpec;

const vespalib::string labelFeatureName("distance(label,nns)");
const vespalib::string fieldFeatureName("distance(bar)");

using RankFixture = DistanceClosenessFixture;

TEST_F("require that blueprint can be created from factory", BlueprintFactoryFixture) {
    Blueprint::SP bp = f.factory.createBlueprint("distance");
    EXPECT_TRUE(bp.get() != 0);
    EXPECT_TRUE(dynamic_cast<DistanceBlueprint*>(bp.get()) != 0);
}

TEST_FFF("require that no features are dumped", DistanceBlueprint, IndexEnvironmentFixture, FeatureDumpFixture) {
    f1.visitDumpFeatures(f2.indexEnv, f3);
}

TEST_FF("require that setup can be done on random label", DistanceBlueprint, IndexEnvironmentFixture) {
    DummyDependencyHandler deps(f1);
    f1.setName(vespalib::make_string("%s(label,random_label)", f1.getBaseName().c_str()));
    EXPECT_TRUE(static_cast<Blueprint&>(f1).setup(f2.indexEnv, std::vector<vespalib::string>{"label", "random_label"}));
}

TEST_FF("require that setup with unknown field fails", DistanceBlueprint, IndexEnvironmentFixture) {
    DummyDependencyHandler deps(f1);
    f1.setName(vespalib::make_string("%s(field,random_fieldname)", f1.getBaseName().c_str()));
    EXPECT_FALSE(static_cast<Blueprint&>(f1).setup(f2.indexEnv, std::vector<vespalib::string>{"field", "random_fieldname"}));
}

TEST_FF("require that no label gives max-double distance", NoLabel(), RankFixture(2, 2, f1, labelFeatureName)) {
    EXPECT_EQUAL(std::numeric_limits<feature_t>::max(), f2.getScore(10));
}

TEST_FF("require that unrelated label gives max-double distance", SingleLabel("unrelated", 1), RankFixture(2, 2, f1, labelFeatureName)) {
    EXPECT_EQUAL(std::numeric_limits<feature_t>::max(), f2.getScore(10));
}

TEST_FF("require that labeled item raw score can be obtained", SingleLabel("nns", 1), RankFixture(2, 2, f1, labelFeatureName)) {
    f2.setFooScore(0, 10, 5.0);
    EXPECT_EQUAL(5.0, f2.getScore(10));
}

TEST_FF("require that field raw score can be obtained", NoLabel(), RankFixture(2, 2, f1, fieldFeatureName)) {
    f2.setBarScore(0, 10, 5.0);
    EXPECT_EQUAL(5.0, f2.getScore(10));
}

TEST_FF("require that other raw scores are ignored", SingleLabel("nns", 2), RankFixture(2, 2, f1, labelFeatureName)) {
    f2.setFooScore(0, 10, 1.0);
    f2.setFooScore(1, 10, 2.0);
    f2.setBarScore(0, 10, 5.0);
    f2.setBarScore(1, 10, 6.0);
    EXPECT_EQUAL(2.0, f2.getScore(10));
}

TEST_FF("require that the correct raw score is used", NoLabel(), RankFixture(2, 2, f1, fieldFeatureName)) {
    f2.setFooScore(0, 10, 3.0);
    f2.setFooScore(1, 10, 4.0);
    f2.setBarScore(0, 10, 8.0);
    f2.setBarScore(1, 10, 7.0);
    EXPECT_EQUAL(7.0, f2.getScore(10));
}

TEST_FF("require that stale data is ignored", SingleLabel("nns", 2), RankFixture(2, 2, f1, labelFeatureName)) {
    f2.setFooScore(0, 10, 1.0);
    f2.setFooScore(1, 5, 2.0);
    EXPECT_EQUAL(std::numeric_limits<feature_t>::max(), f2.getScore(10));
}

void
expect_raw_score_calculated_on_the_fly(RankFixture& f)
{
    f.setBarScore(0, 8, 13.0);
    f.set_attribute_tensor(9, TensorSpec::from_expr("tensor(x[2]):[5,11]"));
    f.set_attribute_tensor(10, TensorSpec::from_expr("tensor(x[2]):[7,11]"));

    // For docids 9 and 10 the raw score is calculated on the fly
    // using a distance calculator over the attribute and query tensors.
    EXPECT_EQUAL(13.0, f.getScore(8));
    EXPECT_EQUAL((5-3), f.getScore(9));
    EXPECT_EQUAL((7-3), f.getScore(10));
}

TEST_FF("raw score is calculated on the fly (using field setup)",
        NoLabel(), RankFixture(0, 1, f1, fieldFeatureName, "tensor(x[2]):[3,11]"))
{
    expect_raw_score_calculated_on_the_fly(f2);
}

TEST_FF("raw score is calculated on the fly (using label setup)",
        SingleLabel("nns", 1), RankFixture(0, 1, f1, labelFeatureName, "tensor(x[2]):[3,11]"))
{
    expect_raw_score_calculated_on_the_fly(f2);
}

TEST_MAIN() { TEST_RUN_ALL(); }