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
114
115
116
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/searchlib/attribute/attributefactory.h>
#include <vespa/searchlib/attribute/attributevector.h>
#include <vespa/searchlib/attribute/integerbase.h>
#include <vespa/searchlib/attribute/floatbase.h>
#include <vespa/searchlib/features/setup.h>
#include <vespa/searchlib/fef/test/indexenvironment.h>
#include <vespa/searchlib/fef/test/indexenvironmentbuilder.h>
#include <vespa/searchlib/fef/test/queryenvironment.h>
#include <vespa/searchlib/fef/test/ftlib.h>
#include <vespa/searchlib/features/euclidean_distance_feature.h>
#include <vespa/searchlib/fef/fef.h>
using search::feature_t;
using namespace search::fef;
using namespace search::fef::test;
using namespace search::features;
using search::AttributeFactory;
using search::IntegerAttribute;
using search::FloatingPointAttribute;
typedef search::attribute::Config AVC;
typedef search::attribute::BasicType AVBT;
typedef search::attribute::CollectionType AVCT;
typedef search::AttributeVector::SP AttributePtr;
typedef FtTestApp FTA;
using CollectionType = FieldInfo::CollectionType;
struct SetupFixture
{
EuclideanDistanceBlueprint blueprint;
IndexEnvironment indexEnv;
SetupFixture()
: blueprint(),
indexEnv()
{
FieldInfo myField(FieldType::ATTRIBUTE, CollectionType::ARRAY, "myAttribute", 1);
indexEnv.getFields().push_back(myField);
}
};
TEST_F("require that blueprint can be created from factory", SetupFixture)
{
EXPECT_TRUE(FTA::assertCreateInstance(f.blueprint, "euclideanDistance"));
}
TEST_F("require that setup succeeds with attribute source", SetupFixture)
{
FTA::FT_SETUP_OK(f.blueprint, f.indexEnv, StringList().add("myAttribute").add("myVector"),
StringList(), StringList().add("distance"));
}
struct ExecFixture
{
BlueprintFactory factory;
FtFeatureTest test;
ExecFixture(const vespalib::string &feature)
: factory(),
test(factory, feature)
{
setup_search_features(factory);
setupAttributeVectors();
setupQueryEnvironment();
ASSERT_TRUE(test.setup());
}
void setupAttributeVectors() {
std::vector<AttributePtr> attrs;
attrs.push_back(AttributeFactory::createAttribute("aint", AVC(AVBT::INT32, AVCT::ARRAY)));
attrs.push_back(AttributeFactory::createAttribute("afloat", AVC(AVBT::FLOAT, AVCT::ARRAY)));
test.getIndexEnv().getFields().push_back(FieldInfo(FieldType::ATTRIBUTE, CollectionType::ARRAY, "aint", 0));
test.getIndexEnv().getFields().push_back(FieldInfo(FieldType::ATTRIBUTE, CollectionType::ARRAY, "afloat", 1));
for (const auto &attr : attrs) {
attr->addReservedDoc();
attr->addDocs(1);
test.getIndexEnv().getAttributeMap().add(attr);
}
IntegerAttribute *aint = static_cast<IntegerAttribute *>(attrs[0].get());
aint->append(1, 1, 0);
aint->append(1, -2, 0);
aint->append(1, 3, 0);
FloatingPointAttribute *afloat = static_cast<FloatingPointAttribute *>(attrs[1].get());
afloat->append(1, 1.3, 0);
afloat->append(1, 1.5, 0);
afloat->append(1, -1.7, 0);
for (const auto &attr : attrs) {
attr->commit();
}
}
void setupQueryEnvironment() {
test.getQueryEnv().getProperties().add("euclideanDistance.intquery", "[4 5 -6]");
test.getQueryEnv().getProperties().add("euclideanDistance.floatquery", "[4.1 15 0.001]");
}
};
TEST_F("require that distance is calculated for integer vectors",
ExecFixture("euclideanDistance(aint,intquery)"))
{
EXPECT_TRUE(f.test.execute(11.789826, 0.000001));
}
TEST_F("require that distance is calculated for floating point vectors",
ExecFixture("euclideanDistance(afloat,floatquery)"))
{
EXPECT_TRUE(f.test.execute(13.891846, 0.000001));
}
TEST_MAIN() { TEST_RUN_ALL(); }
|