aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/fef/test/featuretest.cpp
blob: 5313fcc3eb49b815ff03f5288a5ec0cd496954e5 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "featuretest.h"
#include <vespa/searchlib/fef/utils.h>
#include <vespa/searchlib/fef/blueprint.h>
#include <vespa/vespalib/testkit/test_kit.h>

#include <vespa/log/log.h>
LOG_SETUP(".fef.featuretest");

namespace search::fef::test {

FeatureTest::FeatureTest(BlueprintFactory &factory,
                         const IndexEnvironment &indexEnv,
                         QueryEnvironment &queryEnv,
                         MatchDataLayout &layout,
                         const std::vector<vespalib::string> &features,
                         const Properties &overrides) :
    _factory(factory),
    _indexEnv(indexEnv),
    _queryEnv(queryEnv),
    _features(features),
    _layout(layout),
    _overrides(overrides),
    _resolver(new BlueprintResolver(factory, indexEnv)),
    _match_data(_layout.createMatchData()),
    _rankProgram(new RankProgram(_resolver)),
    _doneSetup(false)
{
}

FeatureTest::~FeatureTest() = default;

FeatureTest::FeatureTest(BlueprintFactory &factory,
                         const IndexEnvironment &indexEnv,
                         QueryEnvironment &queryEnv,
                         MatchDataLayout &layout,
                         const vespalib::string &feature,
                         const Properties &overrides) :
    _factory(factory),
    _indexEnv(indexEnv),
    _queryEnv(queryEnv),
    _features(),
    _layout(layout),
    _overrides(overrides),
    _resolver(new BlueprintResolver(factory, indexEnv)),
    _match_data(_layout.createMatchData()),
    _rankProgram(new RankProgram(_resolver)),
    _doneSetup(false)
{
    _features.push_back(feature);
}

bool
FeatureTest::setup()
{
    if (_doneSetup) {
        LOG(error, "Setup already done.");
        return false;
    }

    // clear state so that setup can be called multiple times.
    clear();

    for (uint32_t i = 0; i < _features.size(); ++i) {
        _resolver->addSeed(_features[i]);
    }

    if (!_resolver->compile()) {
        LOG(error, "Failed to compile blueprint resolver.");
        return false;
    }
    for (const auto &spec: _resolver->getExecutorSpecs()) {
        spec.blueprint->prepareSharedState(_queryEnv, _queryEnv.getObjectStore());
    }
    _rankProgram->setup(*_match_data, _queryEnv, _overrides);
    _doneSetup = true;
    return true;
}

MatchDataBuilder::UP
FeatureTest::createMatchDataBuilder()
{
    if (_doneSetup) {
        return MatchDataBuilder::UP(new MatchDataBuilder(_queryEnv, *_match_data));
    }
    LOG(warning, "Match data not initialized.");
    return MatchDataBuilder::UP();
}

bool
FeatureTest::execute(const RankResult &expected, uint32_t docId)
{
    RankResult result;
    if (!executeOnly(result, docId)) {
        return false;
    }

    if (!result.includes(expected)) {
        std::stringstream exp, act;
        exp << "Expected: " << expected;
        act << "Actual  : " << result;

        LOG(error, "Expected result not present in actual result after execution:");
        LOG(error, "%s", exp.str().c_str());
        LOG(error, "%s", act.str().c_str());

        return false;
    }
    return true;
}

bool
FeatureTest::execute(feature_t expected, double epsilon, uint32_t docId)
{
    return execute(RankResult().setEpsilon(epsilon).addScore(_features.front(), expected), docId);
}

bool
FeatureTest::executeOnly(RankResult & result, uint32_t docId)
{
    if (!_doneSetup) {
        LOG(error, "Setup not done.");
        return false;
    }
    std::map<vespalib::string, feature_t> all = Utils::getAllFeatures(*_rankProgram, docId);
    for (auto itr = all.begin(); itr != all.end(); ++itr) {
        result.addScore(itr->first, itr->second);
    }
    return true;
}

vespalib::eval::Value::CREF
FeatureTest::resolveObjectFeature(uint32_t docid)
{
    return Utils::getObjectFeature(*_rankProgram, docid);
}

void
FeatureTest::clear()
{
    _resolver = BlueprintResolver::SP(new BlueprintResolver(_factory, _indexEnv));
    _match_data = _layout.createMatchData();
    _rankProgram.reset(new RankProgram(_resolver));
    _doneSetup = false;
}

}