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

#pragma once

#include "queryterm.h"
#include <optional>

namespace search::streaming {

/*
 * Nearest neighbor query node for streaming search.
 */
class NearestNeighborQueryNode: public QueryTerm {
public:
    class RawScoreCalculator {
    public:
        virtual ~RawScoreCalculator() = default;
        /**
         * Convert the given distance to a raw score.
         *
         * This is used during unpacking, and also signals that the entire document was a match.
         */
        virtual double to_raw_score(double distance) = 0;
    };

private:
    uint32_t _target_hits;
    double _distance_threshold;
    // When this value is set it also indicates a match for this query node.
    std::optional<double> _distance;
    RawScoreCalculator* _calc;


public:
    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);
    NearestNeighborQueryNode(const NearestNeighborQueryNode &) = delete;
    NearestNeighborQueryNode & operator = (const NearestNeighborQueryNode &) = delete;
    NearestNeighborQueryNode(NearestNeighborQueryNode &&) = delete;
    NearestNeighborQueryNode & operator = (NearestNeighborQueryNode &&) = delete;
    ~NearestNeighborQueryNode() override;
    bool evaluate() override;
    void reset() override;
    NearestNeighborQueryNode* as_nearest_neighbor_query_node() noexcept override;
    const vespalib::string& get_query_tensor_name() const { return getTermString(); }
    uint32_t get_target_hits() const { return _target_hits; }
    double get_distance_threshold() const { return _distance_threshold; }
    void set_raw_score_calc(RawScoreCalculator* calc_in) { _calc = calc_in; }
    void set_distance(double value) { _distance = value; }
    const std::optional<double>& get_distance() const { return _distance; }
    // This is used during unpacking, and also signals to the RawScoreCalculator that the entire document was a match.
    std::optional<double> get_raw_score() const;
};

}