summaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/matching/index_environment/index_environment_test.cpp
blob: bd7c3a4e8fd99181e32127f710bec928896aae8b (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/test_kit.h>

#include <vespa/eval/eval/value_cache/constant_value.h>
#include <vespa/searchcore/proton/matching/indexenvironment.h>
#include <vespa/searchcore/proton/matching/ranking_expressions.h>
#include <vespa/searchcore/proton/matching/onnx_models.h>

using namespace proton::matching;
using search::fef::FieldInfo;
using search::fef::FieldType;
using search::fef::Properties;
using search::fef::OnnxModel;
using search::index::Schema;
using search::index::schema::CollectionType;
using search::index::schema::DataType;
using vespalib::eval::ConstantValue;
using SIAF = Schema::ImportedAttributeField;

const vespalib::string my_expr_ref(
    "this is my reference ranking expression.\n"
    "this is my reference ranking expression.\n"
    "it will not compile into a function.\n"
    "it will not compile into a function.\n"
    "it is just some text, that can also be compressed...\n"
    "it is just some text, that can also be compressed...\n");

RankingExpressions make_expressions() {
    RankingExpressions expr_list;
    expr_list.add("expr1", TEST_PATH("my_expr"));
    expr_list.add("expr2", TEST_PATH("my_expr.lz4"));
    return expr_list;
}

OnnxModels make_models() {
    OnnxModels::Vector list;
    list.emplace_back(std::move(OnnxModel("model1", "path1").input_feature("input1","feature1").output_name("output1", "out1")));
    list.emplace_back(OnnxModel("model2", "path2"));
    return {std::move(list)};
}

struct MyRankingAssetsRepo : public IRankingAssetsRepo {
    RankingExpressions _expressions;
    OnnxModels _onnxModels;
    MyRankingAssetsRepo(RankingExpressions expressions, OnnxModels onnxModels)
        : _expressions(std::move(expressions)),
          _onnxModels(std::move(onnxModels))
    {}
    ~MyRankingAssetsRepo() override;
    ConstantValue::UP getConstant(const vespalib::string &) const override {
        return {};
    }

    vespalib::string getExpression(const vespalib::string & name) const override {
        return _expressions.loadExpression(name);
    }

    const OnnxModel *getOnnxModel(const vespalib::string & name) const override {
        return _onnxModels.getModel(name);
    }
};

MyRankingAssetsRepo::~MyRankingAssetsRepo() = default;

Schema::UP
buildSchema()
{
    Schema::UP result = std::make_unique<Schema>();
    result->addImportedAttributeField(SIAF("imported_a", DataType::INT32, CollectionType::SINGLE));
    result->addImportedAttributeField(SIAF("imported_b", DataType::STRING, CollectionType::ARRAY));
    return result;
}

Schema::UP
buildEmptySchema()
{
    return std::make_unique<Schema>();
}

struct Fixture {
    MyRankingAssetsRepo repo;
    Schema::UP schema;
    IndexEnvironment env;
    explicit Fixture(Schema::UP schema_)
        : repo(make_expressions(), make_models()),
          schema(std::move(schema_)),
          env(7, *schema, Properties(), repo)
    {
    }
    const FieldInfo *assertField(size_t idx,
                                 const vespalib::string &name,
                                 DataType dataType,
                                 CollectionType collectionType) const {
        const FieldInfo *field = env.getField(idx);
        ASSERT_TRUE(field != nullptr);
        EXPECT_EQUAL(field, env.getFieldByName(name));
        EXPECT_EQUAL(name, field->name());
        EXPECT_EQUAL(dataType, field->get_data_type());
        EXPECT_TRUE(collectionType == field->collection());
        EXPECT_EQUAL(idx, field->id());
        return field;
    }
    void assertHiddenAttributeField(size_t idx,
                                    const vespalib::string &name,
                                    DataType dataType,
                                    CollectionType collectionType) const {
        const FieldInfo *field = assertField(idx, name, dataType, collectionType);
        EXPECT_FALSE(field->hasAttribute());
        EXPECT_TRUE(field->type() == FieldType::HIDDEN_ATTRIBUTE);
        EXPECT_TRUE(field->isFilter());
    }
    void assertAttributeField(size_t idx,
                              const vespalib::string &name,
                              DataType dataType,
                              CollectionType collectionType) const {
        const FieldInfo *field = assertField(idx, name, dataType, collectionType);
        EXPECT_TRUE(field->hasAttribute());
        EXPECT_TRUE(field->type() == FieldType::ATTRIBUTE);
        EXPECT_FALSE(field->isFilter());
    }
};

TEST_F("require that document meta store is always extracted in index environment", Fixture(buildEmptySchema()))
{
    ASSERT_EQUAL(1u, f.env.getNumFields());
    TEST_DO(f.assertHiddenAttributeField(0, "[documentmetastore]", DataType::RAW, CollectionType::SINGLE));
}

TEST_F("require that distribution key is visible in index environment", Fixture(buildEmptySchema()))
{
    ASSERT_EQUAL(7u, f.env.getDistributionKey());
}

TEST_F("require that imported attribute fields are extracted in index environment", Fixture(buildSchema()))
{
    ASSERT_EQUAL(3u, f.env.getNumFields());
    TEST_DO(f.assertAttributeField(0, "imported_a", DataType::INT32, CollectionType::SINGLE));
    TEST_DO(f.assertAttributeField(1, "imported_b", DataType::STRING, CollectionType::ARRAY));
    EXPECT_EQUAL("[documentmetastore]", f.env.getField(2)->name());
}

TEST_F("require that onnx model config can be obtained", Fixture(buildEmptySchema())) {
    {
        auto model = f1.env.getOnnxModel("model1");
        ASSERT_TRUE(model != nullptr);
        EXPECT_EQUAL(model->file_path(), vespalib::string("path1"));
        EXPECT_EQUAL(model->input_feature("input1").value(), vespalib::string("feature1"));
        EXPECT_EQUAL(model->output_name("output1").value(), vespalib::string("out1"));
    }
    {
        auto model = f1.env.getOnnxModel("model2");
        ASSERT_TRUE(model != nullptr);
        EXPECT_EQUAL(model->file_path(), vespalib::string("path2"));
        EXPECT_FALSE(model->input_feature("input1").has_value());
        EXPECT_FALSE(model->output_name("output1").has_value());
    }
    EXPECT_TRUE(f1.env.getOnnxModel("model3") == nullptr);
}

TEST_F("require that external ranking expressions can be obtained", Fixture(buildEmptySchema())) {
    auto expr1 = f1.env.getRankingExpression("expr1");
    auto expr2 = f1.env.getRankingExpression("expr2");
    auto expr3 = f1.env.getRankingExpression("expr3");
    EXPECT_EQUAL(expr1, my_expr_ref);
    EXPECT_EQUAL(expr2, my_expr_ref);
    EXPECT_TRUE(expr3.empty());
}

TEST_MAIN() { TEST_RUN_ALL(); }