aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/features/distance_calculator_bundle.h
blob: a8a6eb93b0eb23fe66c26fd2090f99e06877afc5 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/searchlib/fef/handle.h>
#include <vespa/vespalib/stllike/string.h>
#include <memory>
#include <optional>
#include <vector>

namespace search::tensor { class DistanceCalculator; }
namespace search::fef {
class IObjectStore;
class IQueryEnvironment;
}

namespace search::features {

/**
 * A bundle of term-field tuples (TermFieldHandle, DistanceCalculator) used by the closeness and distance rank features.
 *
 * For most document ids the raw score is available in the TermFieldMatchData retrieved using the TermFieldHandle,
 * as it was calculated during matching. In the other cases the DistanceCalculator can be used to calculate the score on the fly.
 */
class DistanceCalculatorBundle {
public:
    struct Element {
        fef::TermFieldHandle handle;
        std::unique_ptr<search::tensor::DistanceCalculator> calc;
        Element(Element&& rhs) noexcept = default; // Needed as std::vector::reserve() is used.
        Element(fef::TermFieldHandle handle_in) noexcept;
        Element(fef::TermFieldHandle handle_in, std::unique_ptr<search::tensor::DistanceCalculator> calc_in) noexcept;
        ~Element();
    };
private:
    std::vector<Element> _elems;
    double _min_rawscore;

public:
    DistanceCalculatorBundle(const fef::IQueryEnvironment& env,
                             uint32_t field_id,
                             const vespalib::string& feature_name);

    DistanceCalculatorBundle(const fef::IQueryEnvironment& env,
                             std::optional<uint32_t> field_id,
                             const vespalib::string& label,
                             const vespalib::string& feature_name);

    const std::vector<Element>& elements() const { return _elems; }

    double min_rawscore() const { return _min_rawscore; }

    static void prepare_shared_state(const fef::IQueryEnvironment& env,
                                     fef::IObjectStore& store,
                                     uint32_t field_id,
                                     const vespalib::string& feature_name);

    static void prepare_shared_state(const fef::IQueryEnvironment& env,
                                     fef::IObjectStore& store,
                                     const vespalib::string& label,
                                     const vespalib::string& feature_name);
};

}