aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/queryeval/intermediate_blueprints.h
blob: f7eeace3e8be1a9a3d1e126f5e808c289c6c313b (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "blueprint.h"
#include "multisearch.h"

namespace search::queryeval {

class ISourceSelector;

//-----------------------------------------------------------------------------

class AndNotBlueprint : public IntermediateBlueprint
{
public:
    bool supports_termwise_children() const override { return true; }
    FlowStats calculate_flow_stats(uint32_t docid_limit) const final;
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    void optimize_self(OptimizePass pass) override;
    AndNotBlueprint * asAndNot() noexcept final { return this; }
    Blueprint::UP get_replacement() override;
    void sort(Children &children, InFlow in_flow) const override;
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             fef::MatchData &md) const override;
    SearchIterator::UP
    createFilterSearch(FilterConstraint constraint) const override;
private:
    AnyFlow my_flow(InFlow in_flow) const override;
    uint8_t calculate_cost_tier() const override {
        return (childCnt() > 0) ? get_children()[0]->getState().cost_tier() : State::COST_TIER_NORMAL;
    }
    bool isPositive(size_t index) const override { return index == 0; }
};

//-----------------------------------------------------------------------------

/** normal AND operator */
class AndBlueprint : public IntermediateBlueprint
{
public:
    bool supports_termwise_children() const override { return true; }
    FlowStats calculate_flow_stats(uint32_t docid_limit) const final;
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    void optimize_self(OptimizePass pass) override;
    AndBlueprint * asAnd() noexcept final { return this; }
    Blueprint::UP get_replacement() override;
    void sort(Children &children, InFlow in_flow) const override;
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             fef::MatchData &md) const override;
    SearchIterator::UP
    createFilterSearch(FilterConstraint constraint) const override;
private:
    AnyFlow my_flow(InFlow in_flow) const override;
};

//-----------------------------------------------------------------------------

/** normal OR operator */
class OrBlueprint : public IntermediateBlueprint
{
public:
    ~OrBlueprint() override;
    bool supports_termwise_children() const override { return true; }
    FlowStats calculate_flow_stats(uint32_t docid_limit) const final;
    double estimate_self_cost(InFlow in_flow) const noexcept override;
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    void optimize_self(OptimizePass pass) override;
    OrBlueprint * asOr() noexcept final { return this; }
    Blueprint::UP get_replacement() override;
    void sort(Children &children, InFlow in_flow) const override;
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             fef::MatchData &md) const override;
    SearchIterator::UP
    createFilterSearch(FilterConstraint constraint) const override;
private:
    AnyFlow my_flow(InFlow in_flow) const override;
    uint8_t calculate_cost_tier() const override;
};

//-----------------------------------------------------------------------------

class WeakAndBlueprint : public IntermediateBlueprint
{
private:
    uint32_t              _n;
    float                 _idf_range;
    std::vector<uint32_t> _weights;

    AnyFlow my_flow(InFlow in_flow) const override;
public:
    FlowStats calculate_flow_stats(uint32_t docid_limit) const final;
    double estimate_self_cost(InFlow in_flow) const noexcept override;
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    Blueprint::UP get_replacement() override;
    void sort(Children &children, InFlow in_flow) const override;
    bool always_needs_unpack() const override;
    WeakAndBlueprint * asWeakAnd() noexcept final { return this; }
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             fef::MatchData &md) const override;
    SearchIterator::UP createFilterSearch(FilterConstraint constraint) const override;

    explicit WeakAndBlueprint(uint32_t n) noexcept : WeakAndBlueprint(n, 0.0) {}
    WeakAndBlueprint(uint32_t n, float idf_range) noexcept : _n(n), _idf_range(idf_range), _weights() {}
    ~WeakAndBlueprint() override;
    void addTerm(Blueprint::UP bp, uint32_t weight) {
        addChild(std::move(bp));
        _weights.push_back(weight);
    }
    uint32_t getN() const { return _n; }
    const std::vector<uint32_t> &getWeights() const { return _weights; }
};

//-----------------------------------------------------------------------------

class NearBlueprint : public IntermediateBlueprint
{
private:
    uint32_t _window;

    AnyFlow my_flow(InFlow in_flow) const override;
public:
    FlowStats calculate_flow_stats(uint32_t docid_limit) const final;
    double estimate_self_cost(InFlow in_flow) const noexcept override;
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    void sort(Children &children, InFlow in_flow) const override;
    SearchIteratorUP createSearch(fef::MatchData &md) const override;
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             fef::MatchData &md) const override;
    SearchIterator::UP createFilterSearch(FilterConstraint constraint) const override;

    explicit NearBlueprint(uint32_t window) noexcept : _window(window) {}
};

//-----------------------------------------------------------------------------

class ONearBlueprint  : public IntermediateBlueprint
{
private:
    uint32_t _window;

    AnyFlow my_flow(InFlow in_flow) const override;
public:
    FlowStats calculate_flow_stats(uint32_t docid_limit) const final;
    double estimate_self_cost(InFlow in_flow) const noexcept override;
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    void sort(Children &children, InFlow in_flow) const override;
    SearchIteratorUP createSearch(fef::MatchData &md) const override;
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             fef::MatchData &md) const override;
    SearchIterator::UP createFilterSearch(FilterConstraint constraint) const override;

    explicit ONearBlueprint(uint32_t window) noexcept : _window(window) {}
};

//-----------------------------------------------------------------------------

class RankBlueprint final : public IntermediateBlueprint
{
public:
    FlowStats calculate_flow_stats(uint32_t docid_limit) const final;
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    void optimize_self(OptimizePass pass) override;
    Blueprint::UP get_replacement() override;
    void sort(Children &children, InFlow in_flow) const override;
    bool isRank() const noexcept final { return true; }
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             fef::MatchData &md) const override;
    SearchIterator::UP
    createFilterSearch(FilterConstraint constraint) const override;
    uint8_t calculate_cost_tier() const override {
        return (childCnt() > 0) ? get_children()[0]->getState().cost_tier() : State::COST_TIER_NORMAL;
    }
private:
    AnyFlow my_flow(InFlow in_flow) const override;
};

//-----------------------------------------------------------------------------

class SourceBlenderBlueprint final : public IntermediateBlueprint
{
private:
    const ISourceSelector &_selector;

    AnyFlow my_flow(InFlow in_flow) const override;
public:
    explicit SourceBlenderBlueprint(const ISourceSelector &selector) noexcept;
    ~SourceBlenderBlueprint() override;
    FlowStats calculate_flow_stats(uint32_t docid_limit) const final;
    double estimate_self_cost(InFlow in_flow) const noexcept override;
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    void sort(Children &children, InFlow in_flow) const override;
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             fef::MatchData &md) const override;
    SearchIterator::UP
    createFilterSearch(FilterConstraint constraint) const override;

    /** check if this blueprint has the same source selector as the other */
    bool isCompatibleWith(const SourceBlenderBlueprint &other) const;
    SourceBlenderBlueprint * asSourceBlender() noexcept final { return this; }
    uint8_t calculate_cost_tier() const override;
};

}