aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/queryeval/intermediate_blueprints.h
blob: b8ca46f75493c518dd8b91e0c768650b1e5d3ea7 (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
// 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; }
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    void optimize_self() override;
    bool isAndNot() const override { return true; }
    Blueprint::UP get_replacement() override;
    void sort(Children &children) const override;
    bool inheritStrict(size_t i) const override;
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             bool strict, fef::MatchData &md) const override;
    SearchIterator::UP
    createFilterSearch(bool strict, FilterConstraint constraint) const override;
private:
    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; }
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    void optimize_self() override;
    bool isAnd() const override { return true; }
    Blueprint::UP get_replacement() override;
    void sort(Children &children) const override;
    bool inheritStrict(size_t i) const override;
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             bool strict, fef::MatchData &md) const override;
    SearchIterator::UP
    createFilterSearch(bool strict, FilterConstraint constraint) const override;
private:
    double computeNextHitRate(const Blueprint & child, double hitRate) const override;
};

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

/** normal OR operator */
class OrBlueprint : public IntermediateBlueprint
{
public:
    ~OrBlueprint() override;
    bool supports_termwise_children() const override { return true; }
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    void optimize_self() override;
    bool isOr() const override { return true; }
    Blueprint::UP get_replacement() override;
    void sort(Children &children) const override;
    bool inheritStrict(size_t i) const override;
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             bool strict, fef::MatchData &md) const override;
    SearchIterator::UP
    createFilterSearch(bool strict, FilterConstraint constraint) const override;
};

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

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

public:
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    void sort(Children &children) const override;
    bool inheritStrict(size_t i) const override;
    bool always_needs_unpack() const override;
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             bool strict, fef::MatchData &md) const override;
    SearchIterator::UP createFilterSearch(bool strict, FilterConstraint constraint) const override;

    WeakAndBlueprint(uint32_t n) : _n(n) {}
    ~WeakAndBlueprint();
    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;

public:
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    bool should_optimize_children() const override { return false; }
    void sort(Children &children) const override;
    bool inheritStrict(size_t i) const override;
    SearchIteratorUP createSearch(fef::MatchData &md, bool strict) const override;
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             bool strict, fef::MatchData &md) const override;
    SearchIterator::UP createFilterSearch(bool strict, FilterConstraint constraint) const override;

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

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

class ONearBlueprint  : public IntermediateBlueprint
{
private:
    uint32_t _window;

public:
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    bool should_optimize_children() const override { return false; }
    void sort(Children &children) const override;
    bool inheritStrict(size_t i) const override;
    SearchIteratorUP createSearch(fef::MatchData &md, bool strict) const override;
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             bool strict, fef::MatchData &md) const override;
    SearchIterator::UP createFilterSearch(bool strict, FilterConstraint constraint) const override;

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

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

class RankBlueprint final : public IntermediateBlueprint
{
public:
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    void optimize_self() override;
    Blueprint::UP get_replacement() override;
    void sort(Children &children) const override;
    bool inheritStrict(size_t i) const override;
    bool isRank() const override { return true; }
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             bool strict, fef::MatchData &md) const override;
    SearchIterator::UP
    createFilterSearch(bool strict, FilterConstraint constraint) const override;
};

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

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

public:
    SourceBlenderBlueprint(const ISourceSelector &selector);
    ~SourceBlenderBlueprint() override;
    HitEstimate combine(const std::vector<HitEstimate> &data) const override;
    FieldSpecBaseList exposeFields() const override;
    void sort(Children &children) const override;
    bool inheritStrict(size_t i) const override;
    /**
     * Will return the index matching the given sourceId.
     * @param sourceId The sourceid to find.
     * @return The index to the child representing the sourceId. -1 if not found.
     */
    ssize_t findSource(uint32_t sourceId) const;
    SearchIterator::UP
    createIntermediateSearch(MultiSearch::Children subSearches,
                             bool strict, fef::MatchData &md) const override;
    SearchIterator::UP
    createFilterSearch(bool strict, FilterConstraint constraint) const override;

    /** check if this blueprint has the same source selector as the other */
    bool isCompatibleWith(const SourceBlenderBlueprint &other) const;
    bool isSourceBlender() const override { return true; }
};

}