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

#include "distance_calculator_bundle.h"
#include "utils.h"
#include <vespa/searchlib/fef/iqueryenvironment.h>
#include <vespa/searchlib/fef/query_value.h>
#include <vespa/searchlib/tensor/distance_calculator.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/issue.h>

using search::fef::ITermData;
using search::fef::IllegalHandle;
using search::fef::InvalidValueTypeException;
using search::fef::QueryValue;
using search::fef::TermFieldHandle;
using search::tensor::DistanceCalculator;
using vespalib::Issue;

namespace search::features {

namespace {

void
prepare_query_tensor(const fef::IQueryEnvironment& env,
                     fef::IObjectStore& store,
                     const vespalib::string& query_tensor_name,
                     const vespalib::string& feature_name)
{
    try {
        auto qvalue = QueryValue::from_config(query_tensor_name, env.getIndexEnvironment());
        qvalue.prepare_shared_state(env, store);
    } catch (const InvalidValueTypeException& ex) {
        Issue::report("%s feature: Query tensor '%s' has invalid type '%s'.",
                      feature_name.c_str(), query_tensor_name.c_str(), ex.type_str().c_str());
    }
}

std::unique_ptr<DistanceCalculator>
make_distance_calculator(const fef::IQueryEnvironment& env,
                         const search::attribute::IAttributeVector& attr,
                         const vespalib::string& query_tensor_name,
                         const vespalib::string& feature_name)
{
    try {
        auto qvalue = QueryValue::from_config(query_tensor_name, env.getIndexEnvironment());
        const auto* query_tensor = qvalue.lookup_value(env.getObjectStore());
        if (query_tensor == nullptr) {
            Issue::report("%s feature: Query tensor '%s' is not found in the object store.",
                          feature_name.c_str(), query_tensor_name.c_str());
            return {};
        }
        return DistanceCalculator::make_with_validation(attr, *query_tensor);
    } catch (const InvalidValueTypeException& ex) {
        Issue::report("%s feature: Query tensor '%s' has invalid type '%s'.",
                      feature_name.c_str(), query_tensor_name.c_str(), ex.type_str().c_str());
    } catch (const vespalib::IllegalArgumentException& ex) {
        Issue::report("%s feature: Could not create distance calculator for attribute '%s' and query tensor '%s': %s.",
                      feature_name.c_str(), attr.getName().c_str(), query_tensor_name.c_str(), ex.getMessage().c_str());
    }
    return {};
}

const search::attribute::IAttributeVector*
resolve_attribute_for_field(const fef::IQueryEnvironment& env,
                            uint32_t field_id,
                            const vespalib::string& feature_name)
{
    const auto* field = env.getIndexEnvironment().getField(field_id);
    if (field != nullptr) {
        const auto* attr = env.getAttributeContext().getAttribute(field->name());
        if (attr == nullptr) {
            Issue::report("%s feature: The attribute vector '%s' for field id '%u' doesn't exist.",
                          feature_name.c_str(), field->name().c_str(), field_id);
        }
        return attr;
    }
    return nullptr;
}

}

DistanceCalculatorBundle::Element::Element(fef::TermFieldHandle handle_in) noexcept
    : handle(handle_in),
      calc()
{
}

DistanceCalculatorBundle::Element::Element(fef::TermFieldHandle handle_in, std::unique_ptr<search::tensor::DistanceCalculator> calc_in) noexcept
    : handle(handle_in),
      calc(std::move(calc_in))
{
}

DistanceCalculatorBundle::Element::~Element() = default;

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

    : _elems(),
      _min_rawscore(0.0)
{
    _elems.reserve(env.getNumTerms());
    const auto* attr = resolve_attribute_for_field(env, field_id, feature_name);
    for (uint32_t i = 0; i < env.getNumTerms(); ++i) {
        search::fef::TermFieldHandle handle = util::getTermFieldHandle(env, i, field_id);
        if (handle != search::fef::IllegalHandle) {
            const auto* term = env.getTerm(i);
            if (term->query_tensor_name().has_value() && (attr != nullptr)) {
                _elems.emplace_back(handle, make_distance_calculator(env, *attr, term->query_tensor_name().value(), feature_name));
                if (_elems.back().calc) {
                    _min_rawscore = _elems.back().calc->function().min_rawscore();
                }
            } else {
                _elems.emplace_back(handle);
            }
        }
    }
}

DistanceCalculatorBundle::DistanceCalculatorBundle(const fef::IQueryEnvironment& env,
                                                   std::optional<uint32_t> field_id,
                                                   const vespalib::string& label,
                                                   const vespalib::string& feature_name)
    : _elems(),
      _min_rawscore(0.0)
{
    const ITermData* term = util::getTermByLabel(env, label);
    if (term != nullptr) {
        // expect numFields() == 1
        for (uint32_t i = 0; i < term->numFields(); ++i) {
            const auto& term_field = term->field(i);
            if (field_id.has_value() && field_id.value() != term_field.getFieldId()) {
                continue;
            }
            TermFieldHandle handle = term_field.getHandle();
            if (handle != IllegalHandle) {
                std::unique_ptr<DistanceCalculator> calc;
                if (term->query_tensor_name().has_value()) {
                    const auto* attr = resolve_attribute_for_field(env, term_field.getFieldId(), feature_name);
                    if (attr != nullptr) {
                        calc = make_distance_calculator(env, *attr, term->query_tensor_name().value(), feature_name);
                        if (calc) {
                            _min_rawscore = calc->function().min_rawscore();
                        }
                    }
                }
                _elems.emplace_back(handle, std::move(calc));
            }
        }
    }
}

void
DistanceCalculatorBundle::prepare_shared_state(const fef::IQueryEnvironment& env,
                                               fef::IObjectStore& store,
                                               uint32_t field_id,
                                               const vespalib::string& feature_name)
{
    for (uint32_t i = 0; i < env.getNumTerms(); ++i) {
        search::fef::TermFieldHandle handle = util::getTermFieldHandle(env, i, field_id);
        if (handle != search::fef::IllegalHandle) {
            const auto* term = env.getTerm(i);
            if (term->query_tensor_name().has_value()) {
                prepare_query_tensor(env, store, term->query_tensor_name().value(), feature_name);
            }
        }
    }
}

void
DistanceCalculatorBundle::prepare_shared_state(const fef::IQueryEnvironment& env,
                                               fef::IObjectStore& store,
                                               const vespalib::string& label,
                                               const vespalib::string& feature_name)
{
    const auto* term = util::getTermByLabel(env, label);
    if ((term != nullptr) && term->query_tensor_name().has_value()) {
        prepare_query_tensor(env, store, term->query_tensor_name().value(), feature_name);
    }
}

}