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

#pragma once

#include "handle.h"
#include "itermdata.h"
#include "simpletermfielddata.h"
#include <vespa/searchlib/query/weight.h>
#include <vector>

namespace search::fef {

/**
 * Static match data for a single unit (term/phrase/etc).
 **/
class SimpleTermData final : public ITermData
{
private:
    query::Weight   _weight;
    uint32_t        _numTerms;
    uint32_t        _uniqueId;
    std::optional<vespalib::string>  _query_tensor_name;
    std::vector<SimpleTermFieldData> _fields;

public:
    SimpleTermData() noexcept;
    SimpleTermData(const ITermData &rhs);
    SimpleTermData(const SimpleTermData &);
    SimpleTermData(SimpleTermData &&) noexcept;
    SimpleTermData & operator=(SimpleTermData &&) noexcept;
    ~SimpleTermData() override;

    //----------- ITermData implementation ------------------------------------

    [[nodiscard]] query::Weight getWeight() const override { return _weight; }

    [[nodiscard]] uint32_t getPhraseLength() const override { return _numTerms; }

    [[nodiscard]] uint32_t getUniqueId() const override { return _uniqueId; }

    [[nodiscard]] std::optional<vespalib::string> query_tensor_name() const override { return _query_tensor_name; }

    [[nodiscard]] size_t numFields() const override { return _fields.size(); }

    [[nodiscard]] const ITermFieldData &field(size_t i) const override {
        return _fields[i];
    }

    [[nodiscard]] const ITermFieldData *lookupField(uint32_t fieldId) const override {
        for (size_t fieldIdx(0), m(numFields()); fieldIdx < m; ++fieldIdx) {
            const ITermFieldData &tfd = field(fieldIdx);
            if (tfd.getFieldId() == fieldId) {
                return &tfd;
            }
        }
        return nullptr;
    }

    //----------- Utility functions -------------------------------------------

    /**
     * Sets the term weight.
     **/
    SimpleTermData &setWeight(query::Weight weight) {
        _weight = weight;
        return *this;
    }

    /**
     * Sets the number of terms represented by this term data object.
     **/
    SimpleTermData &setPhraseLength(uint32_t numTerms) {
        _numTerms = numTerms;
        return *this;
    }

    /**
     * Set the unique id of this term. 0 means not set.
     *
     * @param id unique id or 0
     * @return this to allow chaining.
     **/
    SimpleTermData &setUniqueId(uint32_t id) {
        _uniqueId = id;
        return *this;
    }

    SimpleTermData &set_query_tensor_name(const vespalib::string &name) {
        _query_tensor_name = name;
        return *this;
    }

    /**
     * Add a new field to the set that is searched by this term.
     *
     * @return the newly added field
     * @param fieldId field id of the added field
     **/
    SimpleTermFieldData &addField(uint32_t fieldId) {
        _fields.emplace_back(fieldId);
        return _fields.back();
    }

    /**
     * Direct access to data for individual fields
     * @param i local index, must have: 0 <= i < numFields()
     */
    SimpleTermFieldData &field(size_t i) {
        return _fields[i];
    }

    /**
     * Obtain information about a specific field that may be searched
     * by this term. If the requested field is not searched by this
     * term, NULL will be returned.
     *
     * @return term field data, or NULL if not found
     **/
    SimpleTermFieldData *lookupField(uint32_t fieldId) {
        for (size_t fieldIdx(0), m(numFields()); fieldIdx < m; ++fieldIdx) {
            SimpleTermFieldData& tfd = field(fieldIdx);
            if (tfd.getFieldId() == fieldId) {
                return &tfd;
            }
        }
        return nullptr;
    }
};

}