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

#include "matchdatabuilder.h"
#include <vespa/searchlib/attribute/attributevector.h>
#include <vespa/searchlib/attribute/stringbase.h>
#include <vespa/vespalib/util/stringfmt.h>

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

namespace search::fef::test {

MatchDataBuilder::MatchDataBuilder(QueryEnvironment &queryEnv, MatchData &data) :
    _queryEnv(queryEnv),
    _data(data),
    _index(),
    _match()
{
    // reset all match data objects.
    for (TermFieldHandle handle = 0; handle < _data.getNumTermFields(); ++handle) {
        _data.resolveTermField(handle)->reset(TermFieldMatchData::invalidId());
    }
}

MatchDataBuilder::~MatchDataBuilder() {}

TermFieldMatchData *
MatchDataBuilder::getTermFieldMatchData(uint32_t termId, uint32_t fieldId)
{
    const ITermData *term = _queryEnv.getTerm(termId);
    if (term == nullptr) {
        return nullptr;
    }
    const ITermFieldData *field = term->lookupField(fieldId);
    if (field == nullptr || field->getHandle() >= _data.getNumTermFields()) {
        return nullptr;
    }
    return _data.resolveTermField(field->getHandle());
}


bool
MatchDataBuilder::setFieldLength(const vespalib::string &fieldName, uint32_t length)
{
    const FieldInfo *info = _queryEnv.getIndexEnv()->getFieldByName(fieldName);
    if (info == nullptr) {
        LOG(error, "Field '%s' does not exist.", fieldName.c_str());
        return false;
    }
    _index[info->id()].fieldLength = length;
    return true;
}

bool
MatchDataBuilder::addElement(const vespalib::string &fieldName, int32_t weight, uint32_t length)
{
    const FieldInfo *info = _queryEnv.getIndexEnv()->getFieldByName(fieldName);
    if (info == nullptr) {
        LOG(error, "Field '%s' does not exist.", fieldName.c_str());
        return false;
    }
    _index[info->id()].elements.push_back(MyElement(weight, length));
    return true;
}

bool
MatchDataBuilder::addOccurence(const vespalib::string &fieldName, uint32_t termId, uint32_t pos, uint32_t element)
{
    const FieldInfo *info = _queryEnv.getIndexEnv()->getFieldByName(fieldName);
    if (info == nullptr) {
        LOG(error, "Field '%s' does not exist.", fieldName.c_str());
        return false;
    }
    if (termId >= _queryEnv.getNumTerms()) {
        LOG(error, "Term id '%u' is invalid.", termId);
        return false;
    }
    const ITermFieldData *tfd = _queryEnv.getTerm(termId)->lookupField(info->id());
    if (tfd == nullptr) {
        LOG(error, "Field '%s' is not searched by the given term.",
            fieldName.c_str());
        return false;
    }
    _match[termId][info->id()].insert(Position(pos, element));
    return true;
}

bool
MatchDataBuilder::setWeight(const vespalib::string &fieldName, uint32_t termId, int32_t weight)
{
    const FieldInfo *info = _queryEnv.getIndexEnv()->getFieldByName(fieldName);
    if (info == nullptr) {
        LOG(error, "Field '%s' does not exist.", fieldName.c_str());
        return false;
    }
    if (termId >= _queryEnv.getNumTerms()) {
        LOG(error, "Term id '%u' is invalid.", termId);
        return false;
    }
    const ITermFieldData *tfd = _queryEnv.getTerm(termId)->lookupField(info->id());
    if (tfd == nullptr) {
        LOG(error, "Field '%s' is not searched by the given term.",
            fieldName.c_str());
        return false;
    }
    uint32_t eid = _index[info->id()].elements.size();
    _match[termId][info->id()].clear();
    _match[termId][info->id()].insert(Position(0, eid));
    _index[info->id()].elements.push_back(MyElement(weight, 1));
    return true;
}

bool
MatchDataBuilder::apply(uint32_t docId)
{
    // For each term, do
    for (const auto& term_elem : _match) {
        uint32_t termId = term_elem.first;

        for (const auto& field_elem : term_elem.second) {
            uint32_t fieldId = field_elem.first;
            TermFieldMatchData *match = getTermFieldMatchData(termId, fieldId);

            // Make sure there is a corresponding term field match data object.
            if (match == nullptr) {
                LOG(error, "Term id '%u' is invalid.", termId);
                return false;
            }
            match->reset(docId);

            // find field data
            MyField field;
            auto idxItr = _index.find(fieldId);
            if (idxItr != _index.end()) {
                field = idxItr->second;
            }

            // For log, attempt to lookup field name.
            const FieldInfo *info = _queryEnv.getIndexEnv()->getField(fieldId);
            vespalib::string name = info != nullptr ? info->name() : vespalib::make_string("%d", fieldId).c_str();

            // For each occurence of that term, in that field, do
            for (const auto& occ : field_elem.second) {
                // Append a term match position to the term match data.
                match->appendPosition(TermFieldMatchDataPosition(
                                              occ.eid,
                                              occ.pos,
                                              field.getWeight(occ.eid),
                                              field.getLength(occ.eid)));
                LOG(debug,
                    "Added occurence of term '%u' in field '%s'"
                    " at position '%u'.",
                    termId, name.c_str(), occ.pos);
                if (occ.pos >= field.getLength(occ.eid)) {
                    LOG(warning,
                        "Added occurence of term '%u' in field '%s'"
                        " at position '%u' >= fieldLen '%u'.",
                        termId, name.c_str(), occ.pos, field.getLength(occ.eid));
                }
            }
        }
    }
    // Return ok.
    return true;
}

}