aboutsummaryrefslogtreecommitdiffstats
path: root/streamingvisitors/src/vespa/searchvisitor/rankmanager.cpp
blob: 81a2a48fb4da8f5ac4bb9c98743dc5353f9050f6 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "rankmanager.h"
#include <vespa/searchlib/features/setup.h>
#include <vespa/searchlib/fef/fieldinfo.h>
#include <vespa/searchlib/fef/functiontablefactory.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/util/exception.h>
#include <vespa/vsm/common/document.h>
#include <vespa/vsm/vsm/vsm-adapter.hpp>

#include <vespa/log/log.h>
LOG_SETUP(".searchvisitor.rankmanager");

using vespa::config::search::RankProfilesConfig;
using vespa::config::search::vsm::VsmfieldsConfig;
using search::fef::Blueprint;
using search::fef::BlueprintFactory;
using search::fef::FieldInfo;
using search::fef::Properties;
using search::fef::RankSetup;
using vsm::VsmfieldsHandle;
using vsm::VSMAdapter;
using vsm::FieldIdTList;
using vespalib::make_string_short::fmt;

namespace streaming {

void
RankManager::Snapshot::addProperties(const vespa::config::search::RankProfilesConfig & cfg)
{
    for (uint32_t i = 0; i < cfg.rankprofile.size(); ++i) {
        const RankProfilesConfig::Rankprofile & curr = cfg.rankprofile[i];
        _properties.push_back(NamedPropertySet());
        _properties.back().first = curr.name;
        Properties & p = _properties.back().second;
        for (uint32_t j = 0; j < curr.fef.property.size(); ++j) {
            p.add(vespalib::string(curr.fef.property[j].name.c_str()),
                  vespalib::string(curr.fef.property[j].value.c_str()));
        }
    }
}

FieldInfo::DataType
to_data_type(VsmfieldsConfig::Fieldspec::Searchmethod search_method)
{
    if (search_method == VsmfieldsConfig::Fieldspec::Searchmethod::NEAREST_NEIGHBOR) {
        return FieldInfo::DataType::TENSOR;
    }
    // This is the default FieldInfo data type if not specified.
    return FieldInfo::DataType::DOUBLE;
}

void
RankManager::Snapshot::detectFields(const VsmfieldsHandle & fields)
{
    for (uint32_t i = 0; i < fields->fieldspec.size(); ++i) {
        const VsmfieldsConfig::Fieldspec & fs = fields->fieldspec[i];
        bool isAttribute = (fs.fieldtype == VsmfieldsConfig::Fieldspec::Fieldtype::ATTRIBUTE);
        LOG(debug, "Adding field of type '%s' and name '%s' with id '%u' the index environment.",
                   isAttribute ? "ATTRIBUTE" : "INDEX", fs.name.c_str(), i);
        // This id must match the vsm specific field id
        _protoEnv.addField(fs.name, isAttribute, to_data_type(fs.searchmethod));
    }
}

namespace {

FieldIdTList
buildFieldSet(const VsmfieldsConfig::Documenttype::Index & ci, const search::fef::IIndexEnvironment & indexEnv,
              const VsmfieldsConfig::Documenttype::IndexVector & indexes)
{
    LOG(spam, "Index %s with %zd fields", ci.name.c_str(), ci.field.size());
    FieldIdTList ifm;
    for (const VsmfieldsConfig::Documenttype::Index::Field & cf : ci.field) {
        LOG(spam, "Parsing field %s", cf.name.c_str());
        auto foundIndex = std::find_if(indexes.begin(), indexes.end(),
                                       [&cf](const auto & v) { return v.name == cf.name;});
        if ((foundIndex != indexes.end()) && (cf.name != ci.name)) {
            FieldIdTList sub = buildFieldSet(*foundIndex, indexEnv, indexes);
            ifm.insert(ifm.end(), sub.begin(), sub.end());
        } else {
            const FieldInfo * info = indexEnv.getFieldByName(cf.name);
            if (info != nullptr) {
                LOG(debug, "Adding field '%s' to view in index '%s' (field id '%u')",
                    cf.name.c_str(), ci.name.c_str(), info->id());
                ifm.push_back(info->id());
            } else {
                LOG(warning, "Field '%s' is not registred in the index environment. "
                        "Cannot add to index view.", cf.name.c_str());
            }
        }
    }
    return ifm;
}

}
    
void
RankManager::Snapshot::buildFieldMappings(const VsmfieldsHandle & fields)
{
    for(const VsmfieldsConfig::Documenttype & di : fields->documenttype) {
        LOG(debug, "Looking through indexes for documenttype '%s'", di.name.c_str());
        for(const VsmfieldsConfig::Documenttype::Index & ci : di.index) {
            FieldIdTList view = buildFieldSet(ci, _protoEnv, di.index);
            if (_views.find(ci.name) == _views.end()) {
                std::sort(view.begin(), view.end()); // lowest field id first
                _views[ci.name] = view;
            } else {
                LOG(warning, "We already have a view for index '%s'. Drop the new view.", ci.name.c_str());
            }
        }
    }
}

bool
RankManager::Snapshot::initRankSetup(const BlueprintFactory & factory)
{
    // set up individual index environments per rank profile
    for (uint32_t i = 0; i < _properties.size(); ++i) {
        _indexEnv.push_back(_protoEnv);
        IndexEnvironment & ie = _indexEnv.back();
        ie.getProperties().import(_properties[i].second);
    }

    // set up individual rank setups per rank profile
    for (uint32_t i = 0; i < _indexEnv.size(); ++i) {
        IndexEnvironment & ie = _indexEnv[i];

        auto rs = std::make_shared<RankSetup>(factory, ie);
        rs->configure(); // reads config values from the property map
        if (!rs->compile()) {
            LOG(warning, "Could not compile rank setup for rank profile '%u'. Errors = %s", i, rs->getJoinedWarnings().c_str());
            return false;
        }
        _rankSetup.push_back(rs);
    }
    LOG_ASSERT(_indexEnv.size() == _rankSetup.size());
    LOG(debug, "Number of index environments and rank setups: %u", (uint32_t)_indexEnv.size());
    LOG_ASSERT(_properties.size() == _rankSetup.size());
    for (uint32_t i = 0; i < _properties.size(); ++i) {
        vespalib::string number = fmt("%u", i);
        _rpmap[number] = i;
    }
    for (uint32_t i = 0; i < _properties.size(); ++i) {
        const vespalib::string &name = _properties[i].first;
        _rpmap[name] = i;
    }
    return true;
}

RankManager::Snapshot::Snapshot() :
    _tableManager(),
    _protoEnv(_tableManager),
    _properties(),
    _indexEnv(),
    _rankSetup(),
    _rpmap(),
    _views()
{
    _tableManager.addFactory(search::fef::ITableFactory::SP(new search::fef::FunctionTableFactory(256)));
}

RankManager::Snapshot::~Snapshot() = default;
    
bool
RankManager::Snapshot::setup(const RankManager & rm, const std::vector<NamedPropertySet> & properties)
{
    _properties = properties;
    return setup(rm);
}

bool
RankManager::Snapshot::setup(const RankManager & rm)
{
    VsmfieldsHandle fields = rm._vsmAdapter->getFieldsConfig();
    detectFields(fields);
    buildFieldMappings(fields);
    if (!initRankSetup(rm._blueprintFactory)) {
        return false;
    }
    return true;
}

bool
RankManager::Snapshot::setup(const RankManager & rm, const RankProfilesConfig & cfg)
{
    addProperties(cfg);
    return setup(rm);
}

void
RankManager::notify(const vsm::VSMConfigSnapshot & snap)
{
    configureRankProfiles(*snap.getConfig<RankProfilesConfig>());
}


void
RankManager::configureRankProfiles(const RankProfilesConfig & cfg)
{
    LOG(debug, "configureRankProfiles(): Size of cfg rankprofiles: %zd", cfg.rankprofile.size());

    auto snapshot = std::make_unique<Snapshot>();
    if (snapshot->setup(*this, cfg)) {
        _snapshot.set(snapshot.release());
        _snapshot.latch(); // switch to the new config object
    } else {
        vespalib::string msg = "(re-)configuration of rank manager failed";
        LOG(error, "%s", msg.c_str());
        throw vespalib::Exception(msg, VESPA_STRLOC);
    }
}

RankManager::RankManager(VSMAdapter * const vsmAdapter) :
    _blueprintFactory(),
    _snapshot(),
    _vsmAdapter(vsmAdapter)
{
    // init blueprint factory
    search::features::setup_search_features(_blueprintFactory);
}

RankManager::~RankManager() = default;

void
RankManager::configure(const vsm::VSMConfigSnapshot & snap)
{
    notify(snap);
}
    
}