aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/matching/blueprintbuilder.cpp
blob: 0b2660824c0ae37208e96e0abcf37e60226e7a0d (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "blueprintbuilder.h"
#include "querynodes.h"
#include "same_element_builder.h"
#include <vespa/searchcorespi/index/indexsearchable.h>
#include <vespa/searchlib/query/tree/customtypevisitor.h>
#include <vespa/searchlib/queryeval/leaf_blueprints.h>
#include <vespa/searchlib/queryeval/intermediate_blueprints.h>
#include <vespa/searchlib/queryeval/equiv_blueprint.h>
#include <vespa/searchlib/queryeval/get_weight_from_node.h>
#include <vespa/vespalib/util/issue.h>

using namespace search::queryeval;
using search::query::Node;

namespace proton::matching {

namespace {

struct Mixer {
    std::unique_ptr<OrBlueprint> attributes;

    Mixer() : attributes() {}

    void addAttribute(Blueprint::UP attr) {
        if ( ! attributes) {
            attributes = std::make_unique<OrBlueprint>();
        }
        attributes->addChild(std::move(attr));
    }

    Blueprint::UP mix(Blueprint::UP indexes) {
        if ( ! attributes) {
            if ( ! indexes) {
                return std::make_unique<EmptyBlueprint>();
            }
            return indexes;
        }
        if ( ! indexes) {
            if (attributes->childCnt() == 1) {
                return attributes->removeChild(0);
            } else {
                return std::move(attributes);
            }
        }
        attributes->addChild(std::move(indexes));
        return std::move(attributes);
    }
};

/**
 * requires that match data space has been reserved
 */
class BlueprintBuilderVisitor :
        public search::query::CustomTypeVisitor<ProtonNodeTypes>
{
private:
    const IRequestContext & _requestContext;
    ISearchContext &_context;
    Blueprint::UP   _result;

    void buildChildren(IntermediateBlueprint &parent, const std::vector<Node *> &children);

    template <typename NodeType>
    void buildIntermediate(IntermediateBlueprint *b, NodeType &n) __attribute__((noinline));

    void buildWeakAnd(ProtonWeakAnd &n) {
        auto *wand = new WeakAndBlueprint(n.getTargetNumHits());
        Blueprint::UP result(wand);
        for (auto node : n.getChildren()) {
            uint32_t weight = getWeightFromNode(*node).percent();
            wand->addTerm(build(_requestContext, *node, _context), weight);
        }
        _result = std::move(result);
    }

    void buildEquiv(ProtonEquiv &n) {
        double eqw = n.getWeight().percent();
        FieldSpecBaseList specs;
        specs.reserve(n.numFields());
        for (size_t i = 0; i < n.numFields(); ++i) {
            specs.add(n.field(i).fieldSpec());
        }
        auto *eq = new EquivBlueprint(std::move(specs), n.children_mdl);
        _result.reset(eq);
        for (auto node : n.getChildren()) {
            double w = getWeightFromNode(*node).percent();
            eq->addTerm(build(_requestContext, *node, _context), w / eqw);
        }
        _result->setDocIdLimit(_context.getDocIdLimit());
        n.setDocumentFrequency(_result->getState().estimate().estHits, _context.getDocIdLimit());
    }

    void buildSameElement(ProtonSameElement &n) {
        if (n.numFields() == 1) {
            SameElementBuilder builder(_requestContext, _context, n.field(0).fieldSpec(), n.is_expensive());
            for (Node *node: n.getChildren()) {
                builder.add_child(*node);
            }
            _result = builder.build();
        } else {
            vespalib::Issue::report("SameElement operator searches in unexpected number of fields. Expected 1 but was %zu", n.numFields());
            _result = std::make_unique<EmptyBlueprint>();
        }
    }

    template <typename NodeType>
    void buildTerm(NodeType &n) {
        FieldSpecList indexFields;
        Mixer mixer;
        for (size_t i = 0; i < n.numFields(); ++i) {
            const ProtonTermData::FieldEntry &field = n.field(i);
            assert(field.getFieldId() != search::fef::IllegalFieldId);
            assert(field.getHandle() != search::fef::IllegalHandle);
            if (field.attribute_field) {
                mixer.addAttribute(_context.getAttributes().createBlueprint(_requestContext, field.fieldSpec(), n));
            } else {
                indexFields.add(field.fieldSpec());
            }
        }
        Blueprint::UP indexBlueprint;
        if (!indexFields.empty()) {
            indexBlueprint = _context.getIndexes().createBlueprint(_requestContext, indexFields, n);
        }
        _result = mixer.mix(std::move(indexBlueprint));
        _result->setDocIdLimit(_context.getDocIdLimit());
        n.setDocumentFrequency(_result->getState().estimate().estHits, _context.getDocIdLimit());
    }

protected:
    void visit(ProtonAnd &n)         override { buildIntermediate(new AndBlueprint(), n); }
    void visit(ProtonAndNot &n)      override { buildIntermediate(new AndNotBlueprint(), n); }
    void visit(ProtonOr &n)          override { buildIntermediate(new OrBlueprint(), n); }
    void visit(ProtonWeakAnd &n)     override { buildWeakAnd(n); }
    void visit(ProtonEquiv &n)       override { buildEquiv(n); }
    void visit(ProtonRank &n)        override { buildIntermediate(new RankBlueprint(), n); }
    void visit(ProtonNear &n)        override { buildIntermediate(new NearBlueprint(n.getDistance()), n); }
    void visit(ProtonONear &n)       override { buildIntermediate(new ONearBlueprint(n.getDistance()), n); }
    void visit(ProtonSameElement &n) override { buildSameElement(n); }

    void visit(ProtonWeightedSetTerm &n) override { buildTerm(n); }
    void visit(ProtonDotProduct &n)      override { buildTerm(n); }
    void visit(ProtonWandTerm &n)        override { buildTerm(n); }

    void visit(ProtonPhrase &n)          override { buildTerm(n); }
    void visit(ProtonNumberTerm &n)      override { buildTerm(n); }
    void visit(ProtonLocationTerm &n)    override { buildTerm(n); }
    void visit(ProtonPrefixTerm &n)      override { buildTerm(n); }
    void visit(ProtonRangeTerm &n)       override { buildTerm(n); }
    void visit(ProtonStringTerm &n)      override { buildTerm(n); }
    void visit(ProtonSubstringTerm &n)   override { buildTerm(n); }
    void visit(ProtonSuffixTerm &n)      override { buildTerm(n); }
    void visit(ProtonPredicateQuery &n)  override { buildTerm(n); }
    void visit(ProtonRegExpTerm &n)      override { buildTerm(n); }
    void visit(ProtonNearestNeighborTerm &n) override { buildTerm(n); }
    void visit(ProtonTrue &) override {
        _result = std::make_unique<AlwaysTrueBlueprint>();
    }
    void visit(ProtonFalse &) override {
        _result = std::make_unique<EmptyBlueprint>();
    }
    void visit(ProtonFuzzyTerm &n)      override { buildTerm(n); }
    void visit(ProtonInTerm& n)         override { buildTerm(n); }

public:
    BlueprintBuilderVisitor(const IRequestContext & requestContext, ISearchContext &context) :
        _requestContext(requestContext),
        _context(context),
        _result()
    { }
    Blueprint::UP build() {
        assert(_result);
        return std::move(_result);
    }
    static Blueprint::UP build(const IRequestContext & requestContext, Node &node, ISearchContext &context) {
        BlueprintBuilderVisitor visitor(requestContext, context);
        node.accept(visitor);
        Blueprint::UP result = visitor.build();
        return result;

    }
};

void
BlueprintBuilderVisitor::buildChildren(IntermediateBlueprint &parent, const std::vector<Node *> &children)
{
    parent.reserve(children.size());
    for (auto child : children) {
        parent.addChild(build(_requestContext, *child, _context));
    }
}

template <typename NodeType>
void
BlueprintBuilderVisitor::buildIntermediate(IntermediateBlueprint *b, NodeType &n) {
    std::unique_ptr<IntermediateBlueprint> blueprint(b);
    buildChildren(*blueprint, n.getChildren());
    _result = std::move(blueprint);
}

IntermediateBlueprint *
asRankOrAndNot(Blueprint * blueprint) {
    return ((blueprint->isAndNot() || blueprint->isRank()))
           ? blueprint->asIntermediate()
           : nullptr;
}

IntermediateBlueprint *
lastConsequtiveRankOrAndNot(Blueprint * blueprint) {
    IntermediateBlueprint * prev = nullptr;
    IntermediateBlueprint * curr = asRankOrAndNot(blueprint);
    while (curr != nullptr) {
        prev =  curr;
        curr = asRankOrAndNot(&curr->getChild(0));
    }
    return prev;
}

} // namespace proton::matching::<unnamed>

Blueprint::UP
BlueprintBuilder::build(const IRequestContext & requestContext,
                        Node &node, Blueprint::UP whiteList, ISearchContext &context)
{
    auto blueprint = BlueprintBuilderVisitor::build(requestContext, node, context);
    if (whiteList) {
        auto andBlueprint = std::make_unique<AndBlueprint>();
        IntermediateBlueprint * rankOrAndNot = lastConsequtiveRankOrAndNot(blueprint.get());
        if (rankOrAndNot != nullptr) {
            (*andBlueprint)
                    .addChild(rankOrAndNot->removeChild(0))
                    .addChild(std::move(whiteList));
            rankOrAndNot->insertChild(0, std::move(andBlueprint));
        } else {
            (*andBlueprint)
                    .addChild(std::move(blueprint))
                    .addChild(std::move(whiteList));
            blueprint = std::move(andBlueprint);
        }
    }
    blueprint->setDocIdLimit(context.getDocIdLimit());
    return blueprint;
}

}