summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/query/streaming/nearest_neighbor_query_node.cpp
blob: 07d16c7bddc21e84f76914dc50013b35e8010b6d (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "nearest_neighbor_query_node.h"
#include <vespa/searchlib/fef/itermdata.h>
#include <vespa/searchlib/fef/matchdata.h>
#include <cassert>

namespace search::streaming {

NearestNeighborQueryNode::NearestNeighborQueryNode(std::unique_ptr<QueryNodeResultBase> resultBase,
                                                   const string& query_tensor_name, const string& field_name,
                                                   uint32_t target_hits, double distance_threshold,
                                                   int32_t unique_id, search::query::Weight weight)
    : QueryTerm(std::move(resultBase), query_tensor_name, field_name, Type::NEAREST_NEIGHBOR, Normalizing::NONE),
      _target_hits(target_hits),
      _distance_threshold(distance_threshold),
      _distance(),
      _calc()
{
    setUniqueId(unique_id);
    setWeight(weight);
}

NearestNeighborQueryNode::~NearestNeighborQueryNode() = default;

bool
NearestNeighborQueryNode::evaluate() const
{
    return _distance.has_value();
}

void
NearestNeighborQueryNode::reset()
{
    _distance.reset();
}

NearestNeighborQueryNode*
NearestNeighborQueryNode::as_nearest_neighbor_query_node() noexcept
{
    return this;
}

std::optional<double>
NearestNeighborQueryNode::get_raw_score() const
{
    if (_distance.has_value()) {
        assert(_calc != nullptr);
        return _calc->to_raw_score(_distance.value());
    }
    return std::nullopt;
}

void
NearestNeighborQueryNode::unpack_match_data(uint32_t docid, const fef::ITermData& td, fef::MatchData& match_data)
{
    auto raw_score = get_raw_score();
    if (raw_score.has_value()) {
        if (td.numFields() == 1u) {
            auto& tfd = td.field(0u);
            auto tmd = match_data.resolveTermField(tfd.getHandle());
            assert(tmd != nullptr);
            tmd->setRawScore(docid, raw_score.value());
        }
    }
}

}