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

#include "closenessfeature.h"
#include "distance_calculator_bundle.h"
#include "utils.h"
#include <vespa/searchcommon/common/schema.h>
#include <vespa/searchlib/fef/properties.h>
#include <vespa/searchlib/tensor/distance_calculator.h>
#include <vespa/vespalib/util/stash.h>

#include <vespa/log/log.h>
LOG_SETUP(".features.closenessfeature");

using namespace search::fef;

namespace search::features {

/** Implements the executor for converting NNS rawscore to a closeness feature. */
class ConvertRawScoreToCloseness : public fef::FeatureExecutor {
private:
    DistanceCalculatorBundle _bundle;
    const fef::MatchData    *_md;
    void handle_bind_match_data(const fef::MatchData &md) override {
        _md = &md;
    }
public:
    ConvertRawScoreToCloseness(const fef::IQueryEnvironment &env, uint32_t fieldId);
    ConvertRawScoreToCloseness(const fef::IQueryEnvironment &env, const vespalib::string &label);
    void execute(uint32_t docId) override;
};

ConvertRawScoreToCloseness::ConvertRawScoreToCloseness(const fef::IQueryEnvironment &env, uint32_t fieldId)
  : _bundle(env, fieldId, "closeness"),
    _md(nullptr)
{
}

ConvertRawScoreToCloseness::ConvertRawScoreToCloseness(const fef::IQueryEnvironment &env, const vespalib::string &label)
  : _bundle(env, std::nullopt, label, "closeness"),
    _md(nullptr)
{
}

void
ConvertRawScoreToCloseness::execute(uint32_t docId)
{
    feature_t max_closeness = _bundle.min_rawscore();
    assert(_md);
    for (const auto& elem : _bundle.elements()) {
        const TermFieldMatchData *tfmd = _md->resolveTermField(elem.handle);
        if (tfmd->getDocId() == docId) {
            feature_t converted = tfmd->getRawScore();
            max_closeness = std::max(max_closeness, converted);
        } else if (elem.calc) {
            feature_t converted = elem.calc->calc_raw_score(docId);
            max_closeness = std::max(max_closeness, converted);
        }
    }
    outputs().set_number(0, max_closeness);
}


ClosenessExecutor::ClosenessExecutor(feature_t maxDistance, feature_t scaleDistance) :
    FeatureExecutor(),
    _maxDistance(maxDistance),
    _logCalc(maxDistance, scaleDistance)
{
}

void
ClosenessExecutor::execute(uint32_t)
{
    feature_t distance = inputs().get_number(0);
    feature_t closeness = std::max(1 - (distance / _maxDistance), (feature_t)0);
    outputs().set_number(0, closeness);
    outputs().set_number(1, _logCalc.get(distance));
}


// Polar Earth radius r = 6356.8 km
// Polar Earth diameter = 2 * pi * r = 39940.952 km
// 1 diameter = 39940.952 km = 360 degrees = 360 * 1000000 microdegrees
// -> 1 km = 9013.30536007 microdegrees

ClosenessBlueprint::ClosenessBlueprint() :
    Blueprint("closeness"),
    _maxDistance(9013305.0),     // default value (about 250 km)
    _scaleDistance(5.0*9013.305), // default value (about 5 km)
    _halfResponse(1),
    _arg_string(),
    _attr_id(search::index::Schema::UNKNOWN_FIELD_ID),
    _use_geo_pos(false),
    _use_nns_tensor(false),
    _use_item_label(false)
{
}

ClosenessBlueprint::~ClosenessBlueprint() = default;

void
ClosenessBlueprint::visitDumpFeatures(const IIndexEnvironment &,
                                      IDumpFeatureVisitor &) const
{
}

bool
ClosenessBlueprint::setup(const IIndexEnvironment & env,
                          const search::fef::ParameterList & params)
{
    // params[0] = attribute name
    vespalib::string arg = params[0].getValue();
    if (params.size() == 2) {
        // params[0] = field / label
        // params[0] = attribute name / label value
        if (arg == "label") {
            _arg_string = params[1].getValue();
            _use_item_label = true;
            describeOutput("out", "The closeness from the labeled query item.");
            return true;
        } else if (arg == "field") {
            arg = params[1].getValue();
            // sanity checking happens in distance feature
        } else {
            LOG(error, "first argument must be 'field' or 'label', but was '%s'",
                arg.c_str());
            return false;
        }
    }
    const FieldInfo *fi = env.getFieldByName(arg);
    if (fi != nullptr && fi->hasAttribute()) {
        auto dt = fi->get_data_type();
        auto ct = fi->collection();
        if (dt == search::index::schema::DataType::TENSOR &&
            ct == search::index::schema::CollectionType::SINGLE)
        {
            _arg_string = arg;
            _use_nns_tensor = true;
            _attr_id = fi->id();
            describeOutput("out", "The closeness for the given tensor field.");
            return true;
        }
    }
    Property p = env.getProperties().lookup(getName(), "maxDistance");
    if (p.found()) {
        _maxDistance = util::strToNum<feature_t>(p.get());
    }
    p = env.getProperties().lookup(getName(), "halfResponse");
    bool useHalfResponse = false;
    if (p.found()) {
        _halfResponse = util::strToNum<feature_t>(p.get());
        useHalfResponse = true;
    }
    // sanity checks:
    if (_maxDistance < 1) {
        LOG(warning, "Invalid %s.maxDistance = %g, using 1.0",
            getName().c_str(), (double)_maxDistance);
        _maxDistance = 1.0;
    }
    if (_halfResponse < 1) {
        LOG(warning, "Invalid %s.halfResponse = %g, using 1.0",
            getName().c_str(), (double)_halfResponse);
        _halfResponse = 1.0;
    }
    if (_halfResponse >= _maxDistance / 2) {
        feature_t newResponse = (_maxDistance / 2) - 1;
        LOG(warning, "Invalid %s.halfResponse = %g, using %g ((%s.maxDistance / 2) - 1)",
            getName().c_str(), (double)_halfResponse, (double)newResponse, getName().c_str());
        _halfResponse = newResponse;
    }

    if (useHalfResponse) {
        _scaleDistance = LogarithmCalculator::getScale(_halfResponse, _maxDistance);
    }

    _use_geo_pos = true;
    if (params.size() == 2) {
        defineInput("distance(field," + arg + ")");
    } else {
        defineInput("distance(" + arg + ")");
    }
    describeOutput("out", "The closeness of the document (linear)");
    describeOutput("logscale", "The closeness of the document (logarithmic shape)");
    return true;
}

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

void
ClosenessBlueprint::prepareSharedState(const fef::IQueryEnvironment& env, fef::IObjectStore& store) const
{
    if (_use_nns_tensor) {
        DistanceCalculatorBundle::prepare_shared_state(env, store, _attr_id, "closeness");
    }
    if (_use_item_label) {
        DistanceCalculatorBundle::prepare_shared_state(env, store, _arg_string, "closeness");
    }
}

FeatureExecutor &
ClosenessBlueprint::createExecutor(const IQueryEnvironment &env, vespalib::Stash &stash) const
{
    if (_use_nns_tensor) {
        return stash.create<ConvertRawScoreToCloseness>(env, _attr_id);
    }
    if (_use_item_label) {
        return stash.create<ConvertRawScoreToCloseness>(env, _arg_string);
    }
    assert(_use_geo_pos);
    return stash.create<ClosenessExecutor>(_maxDistance, _scaleDistance);
}

}