aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/query/streaming/nearest_neighbor_query_node.cpp
blob: efbd6c935dcf12350eadf4778b1f156361075e58 (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
// 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 <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()
{
    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;
}

}