aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/fef/test/ftlib.h
blob: be52b4073697f5a1ba68a65539b3171edb6d4d66 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "featuretest.h"
#include "indexenvironment.h"
#include "indexenvironmentbuilder.h"
#include "matchdatabuilder.h"
#include "queryenvironment.h"
#include "queryenvironmentbuilder.h"
#include "rankresult.h"
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/searchlib/common/feature.h>
#include <vespa/searchlib/query/weight.h>
#include <vespa/searchlib/features/fieldmatch/params.h>
#include <vespa/searchlib/fef/fef.h>

using search::feature_t;

//---------------------------------------------------------------------------------------------------------------------
// StringList
//---------------------------------------------------------------------------------------------------------------------
class StringList : public std::vector<vespalib::string> {
public:
    StringList &add(vespalib::stringref str) { push_back(str); return *this; }
    StringList &clear()  { std::vector<vespalib::string>::clear(); return *this; }
};

//---------------------------------------------------------------------------------------------------------------------
// StringMap
//---------------------------------------------------------------------------------------------------------------------
class StringMap : public std::map<vespalib::string, vespalib::string> {
public:
    StringMap &add(const vespalib::string &key, const vespalib::string &val) {
        iterator it = insert(std::make_pair(key, val)).first;
        it->second = val;
        return *this;
    }
    StringMap &clear() {
        std::map<vespalib::string, vespalib::string>::clear();
        return *this;
    }
};

//---------------------------------------------------------------------------------------------------------------------
// StringSet
//---------------------------------------------------------------------------------------------------------------------
class StringSet : public std::set<vespalib::string> {
public:
    StringSet & add(const vespalib::string & str) { insert(str); return *this; }
    StringSet & clear() { std::set<vespalib::string>::clear(); return *this; }
};


//---------------------------------------------------------------------------------------------------------------------
// FtIndexEnvironment
//---------------------------------------------------------------------------------------------------------------------
class FtIndexEnvironment : public search::fef::test::IndexEnvironment {
public:
    FtIndexEnvironment();

    search::fef::test::IndexEnvironmentBuilder &getBuilder() { return _builder; }

private:
    search::fef::test::IndexEnvironmentBuilder _builder;
};

//---------------------------------------------------------------------------------------------------------------------
// FtQueryEnvironment
//---------------------------------------------------------------------------------------------------------------------
class FtQueryEnvironment : public search::fef::test::QueryEnvironment {
public:
    FtQueryEnvironment(search::fef::test::IndexEnvironment &indexEnv);
    ~FtQueryEnvironment();

    search::fef::test::QueryEnvironmentBuilder &getBuilder() { return _builder; }
    search::fef::MatchDataLayout               &getLayout()  { return _layout; }

private:
    search::fef::MatchDataLayout               _layout;
    search::fef::test::QueryEnvironmentBuilder _builder;
};

//---------------------------------------------------------------------------------------------------------------------
// FtDumpFeatureVisitor
//---------------------------------------------------------------------------------------------------------------------
class FtDumpFeatureVisitor : public search::fef::IDumpFeatureVisitor
{
private:
    std::vector<vespalib::string> _features;

public:
    FtDumpFeatureVisitor();
    void visitDumpFeature(const vespalib::string & name) override { _features.push_back(name); }
    const std::vector<vespalib::string> & features() const { return _features; }
};

//---------------------------------------------------------------------------------------------------------------------
// FtTestRunner
//---------------------------------------------------------------------------------------------------------------------
class FtFeatureTest {
public:
    FtFeatureTest(search::fef::BlueprintFactory &factory, const vespalib::string &feature);
    FtFeatureTest(search::fef::BlueprintFactory &factory, const std::vector<vespalib::string> &features);
    ~FtFeatureTest();

    bool setup()                                                                    { return _test.setup(); }
    bool execute(feature_t expected, double epsilon = 0, uint32_t docId = 1)        { return _test.execute(expected, epsilon, docId); }
    bool execute(const search::fef::test::RankResult &expected, uint32_t docId = 1) { return _test.execute(expected, docId); }
    bool executeOnly(search::fef::test::RankResult &result, uint32_t docId = 1)     { return _test.executeOnly(result, docId); }
    search::fef::test::MatchDataBuilder::UP createMatchDataBuilder()                { return _test.createMatchDataBuilder(); }
    vespalib::eval::Value::CREF resolveObjectFeature(uint32_t docid = 1) { return _test.resolveObjectFeature(docid); }

    FtIndexEnvironment &getIndexEnv() { return _indexEnv; }
    FtQueryEnvironment &getQueryEnv() { return _queryEnv; }
    search::fef::Properties &getOverrides() { return _overrides; }

private:
    FtIndexEnvironment                   _indexEnv;
    FtQueryEnvironment                   _queryEnv;
    search::fef::Properties              _overrides;
    search::fef::test::FeatureTest       _test;
};

//---------------------------------------------------------------------------------------------------------------------
// FtQueryTerm
//---------------------------------------------------------------------------------------------------------------------
struct FtQueryTerm {
    FtQueryTerm(const vespalib::string t, uint32_t tw = 100, feature_t co = 0.1f, feature_t si = 0.1f) :
        term(t), termWeight(tw), connexity(co), significance(si) {}
    FtQueryTerm() noexcept : term(), termWeight(100), connexity(0.1f), significance(0.1f) {}
    vespalib::string term;
    search::query::Weight termWeight;
    feature_t connexity;
    feature_t significance;
    bool operator<(const FtQueryTerm & rhs) const {
        return term < rhs.term;
    }
    bool operator==(const FtQueryTerm & rhs) const {
        return term == rhs.term;
    }
};

using FtQuery = std::vector<FtQueryTerm>;
using StringVectorMap = std::map<vespalib::string, std::vector<vespalib::string> >;

//---------------------------------------------------------------------------------------------------------------------
// FtUtil
//---------------------------------------------------------------------------------------------------------------------
class FtUtil {
public:
    static std::vector<vespalib::string> tokenize(const vespalib::string & str, const vespalib::string & separator = " ");
    static FtQuery toQuery(const vespalib::string & query, const vespalib::string & separator = " ");
    static search::fef::test::RankResult toRankResult(const vespalib::string & baseName,
                                                      const vespalib::string & result,
                                                      const vespalib::string & separator = " ");
};

//---------------------------------------------------------------------------------------------------------------------
// FtIndex
//---------------------------------------------------------------------------------------------------------------------
struct FtIndex {
    struct Element {
        using Tokens = std::vector<vespalib::string>;
        int32_t weight;
        Tokens  tokens;
        Element(int32_t w, const Tokens &t)
            : weight(w), tokens(t) {}
    };
    using Field = std::vector<Element>;
    using FieldMap = std::map<vespalib::string, Field>;
    FieldMap index; // raw content of all fields
    vespalib::string cursor; // last referenced field
    FtIndex() : index(), cursor() {}
    ~FtIndex();
    FtIndex &field(const vespalib::string &name) {
        cursor = name;
        index[name];
        return *this;
    }
    FtIndex &element(const vespalib::string &content, int32_t weight = 1) {
        assert(!cursor.empty());
        index[cursor].push_back(Element(weight, FtUtil::tokenize(content, " ")));
        return *this;
    }
};

//---------------------------------------------------------------------------------------------------------------------
// FtTestApp
//---------------------------------------------------------------------------------------------------------------------
struct FtTestApp : public vespalib::TestApp {
    using string = vespalib::string;
    static void FT_SETUP_FAIL(const search::fef::Blueprint &prototype, const StringList &params);
    static void FT_SETUP_FAIL(const search::fef::Blueprint &prototype, const search::fef::test::IndexEnvironment &env,
                              const StringList &params);
    static void FT_SETUP_OK(const search::fef::Blueprint &prototype, const StringList &params,
                            const StringList &expectedIn, const StringList &expectedOut);
    static void FT_SETUP_OK(const search::fef::Blueprint &prototype, const search::fef::test::IndexEnvironment &env,
                            const StringList &params, const StringList &expectedIn, const StringList &expectedOut);

    static void FT_DUMP_EMPTY(search::fef::BlueprintFactory &factory, const vespalib::string &baseName);
    static void FT_DUMP_EMPTY(search::fef::BlueprintFactory &factory, const vespalib::string &baseName,
                              search::fef::test::IndexEnvironment &env);
    static void FT_DUMP(search::fef::BlueprintFactory &factory, const vespalib::string &baseName,
                        const StringList &expected);
    static void FT_DUMP(search::fef::BlueprintFactory &factory, const vespalib::string &baseName,
                        search::fef::test::IndexEnvironment &env,
                        const StringList &expected);

    static void FT_EQUAL(const std::vector<string> &expected, const std::vector<string> &actual,
                         const vespalib::string & prefix = "");

    static void FT_LOG(const search::fef::Blueprint &prototype, const search::fef::test::IndexEnvironment &env, const StringList &params);
    static void FT_LOG(const vespalib::string &prefix, const std::vector<vespalib::string> &arr);


    static void FT_SETUP(FtFeatureTest & test, const vespalib::string & query, const StringMap & index, uint32_t docId);
    static void FT_SETUP(FtFeatureTest & test, const FtQuery & query, const StringVectorMap & index, uint32_t docId);

    static void FT_SETUP(FtFeatureTest &test, const FtQuery &query, const FtIndex &index, uint32_t docId);

    static void setupQueryEnv(FtQueryEnvironment & queryEnv, const FtQuery & query);
    static void setupFieldMatch(FtFeatureTest & test, const vespalib::string & indexName,
                                const vespalib::string & query, const vespalib::string & field,
                                const search::features::fieldmatch::Params * params,
                                uint32_t totalTermWeight, feature_t totalSignificance,
                                uint32_t docId);

    static search::fef::test::RankResult toRankResult(const vespalib::string & baseName,
                                                      const vespalib::string & result,
                                                      const vespalib::string & separator = " ");

    template <typename T>
    static bool assertCreateInstance(const T & prototype, const vespalib::string & baseName) {
        search::fef::Blueprint::UP bp = prototype.createInstance();
        if (!EXPECT_TRUE(dynamic_cast<T*>(bp.get()) != NULL)) return false;
        if (!EXPECT_EQUAL(bp->getBaseName(), baseName)) return false;
        return true;
    }
};