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

#include "nativeattributematchfeature.h"
#include "valuefeature.h"
#include "utils.h"
#include <vespa/searchlib/fef/fieldinfo.h>
#include <vespa/searchlib/fef/indexproperties.h>
#include <vespa/searchlib/fef/itablemanager.h>
#include <vespa/searchlib/fef/properties.h>
#include <vespa/vespalib/util/stash.h>

using namespace search::fef;

namespace search::features {

feature_t
NativeAttributeMatchExecutor::calculateScore(const CachedTermData &td, const TermFieldMatchData &tfmd)
{
    return (td.weightBoostTable->get(tfmd.getWeight()) * td.scale);
}

NativeAttributeMatchExecutor::Precomputed
NativeAttributeMatchExecutor::preComputeSetup(const IQueryEnvironment & env,
                                              const NativeAttributeMatchParams & params)
{
    NativeAttributeMatchExecutor::Precomputed precomputed;
    for (uint32_t i = 0; i < env.getNumTerms(); ++i) {
        const ITermData *termData = env.getTerm(i);
        if (termData->getWeight().percent() != 0) // only consider query terms with contribution
        {
            using FRA = search::fef::ITermFieldRangeAdapter;
            for (FRA iter(*termData); iter.valid(); iter.next()) {
                const ITermFieldData& tfd = iter.get();
                uint32_t fieldId = tfd.getFieldId();
                if (params.considerField(fieldId)) { // only consider fields with contribution
                    const NativeAttributeMatchParams::Param & param = params.vector[fieldId];
                    precomputed.first.push_back(CachedTermData(params, tfd,
                                                        param.fieldWeight * termData->getWeight().percent() / param.maxTableSum));
                    precomputed.second += (param.fieldWeight * termData->getWeight().percent());
                }
            }
        }
    }
    return precomputed;
}

FeatureExecutor &
NativeAttributeMatchExecutor::createExecutor(const IQueryEnvironment & env,
                                             const NativeAttributeMatchParams & params,
                                             vespalib::Stash &stash)
{
    Precomputed setup = preComputeSetup(env, params);
    if (setup.first.size() == 0) {
        return stash.create<SingleZeroValueExecutor>();
    } else if (setup.first.size() == 1) {
        return stash.create<NativeAttributeMatchExecutorSingle>(setup);
    } else {
        return stash.create<NativeAttributeMatchExecutorMulti>(setup);
    }
}

void
NativeAttributeMatchExecutorMulti::execute(uint32_t docId)
{
    feature_t score = 0;
    for (size_t i = 0; i < _queryTermData.size(); ++i) {
        const TermFieldMatchData *tfmd = _md->resolveTermField(_queryTermData[i].tfh);
        if (tfmd->getDocId() == docId) {
            score += calculateScore(_queryTermData[i], *tfmd);
        }
    }
    outputs().set_number(0, score / _divisor);
}

void
NativeAttributeMatchExecutorMulti::handle_bind_match_data(const MatchData &md)
{
    _md = &md;
}

void
NativeAttributeMatchExecutorSingle::execute(uint32_t docId)
{
    const TermFieldMatchData &tfmd = *_md->resolveTermField(_queryTermData.tfh);
    outputs().set_number(0, (tfmd.getDocId() == docId)
                         ? calculateScore(_queryTermData, tfmd)
                         : 0);
}

void
NativeAttributeMatchExecutorSingle::handle_bind_match_data(const MatchData &md)
{
    _md = &md;
}

NativeAttributeMatchBlueprint::NativeAttributeMatchBlueprint() :
    Blueprint("nativeAttributeMatch"),
    _params()
{
}

namespace {
const vespalib::string DefaultWeightTable = "linear(1,0)";
const vespalib::string WeightTableName = "weightTable";
}

void
NativeAttributeMatchBlueprint::visitDumpFeatures(const IIndexEnvironment & env,
                                                 IDumpFeatureVisitor & visitor) const
{
    (void) env;
    visitor.visitDumpFeature(getBaseName());
}

Blueprint::UP
NativeAttributeMatchBlueprint::createInstance() const
{
    return std::make_unique<NativeAttributeMatchBlueprint>();
}

fef::ParameterDescriptions
NativeAttributeMatchBlueprint::getDescriptions() const
{
    return fef::ParameterDescriptions().desc().attribute(fef::ParameterDataTypeSet::normalTypeSet(), fef::ParameterCollection::ANY).repeat();
}

bool
NativeAttributeMatchBlueprint::setup(const IIndexEnvironment & env,
                                     const ParameterList & params)
{
    _params.resize(env.getNumFields());
    FieldWrapper fields(env, params, FieldType::ATTRIBUTE);
    for (uint32_t i = 0; i < fields.getNumFields(); ++i) {
        const FieldInfo * info = fields.getField(i);

        uint32_t fieldId = info->id();
        NativeAttributeMatchParams::Param & param = _params.vector[fieldId];
        param.field = true;
        const Table * weightBoostTable = util::lookupTable(env, getBaseName(), WeightTableName, info->name(), DefaultWeightTable);
        if (weightBoostTable == NULL) {
            return false;
        }
        param.weightBoostTable = SymmetricTable(*weightBoostTable);
        param.fieldWeight = indexproperties::FieldWeight::lookup(env.getProperties(), info->name());
        if (param.fieldWeight == 0) {
            param.field = false;
        }
        if (NativeRankBlueprint::useTableNormalization(env)) {
            _params.setMaxTableSums(fieldId, param.weightBoostTable.max());
        }
    }

    describeOutput("score", "The native attribute match score");
    return true;
}

FeatureExecutor &
NativeAttributeMatchBlueprint::createExecutor(const IQueryEnvironment &env, vespalib::Stash &stash) const
{
    return NativeAttributeMatchExecutor::createExecutor(env, _params, stash);
}

}