summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/queryeval/leaf_blueprints.cpp
blob: a72fbc8d57a5700cb7a09c030860cc8074151bf1 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "leaf_blueprints.h"
#include "emptysearch.h"
#include "simplesearch.h"
#include "fake_search.h"

namespace search::queryeval {

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

SearchIterator::UP
EmptyBlueprint::createLeafSearch(const search::fef::TermFieldMatchDataArray &,
                                 bool) const
{
    return std::make_unique<EmptySearch>();
}

SearchIterator::UP
EmptyBlueprint::createFilterSearch(bool /*strict*/, FilterConstraint /* constraint */) const
{
    return std::make_unique<EmptySearch>();
}

EmptyBlueprint::EmptyBlueprint(const FieldSpecBase &field)
    : SimpleLeafBlueprint(field)
{
}

EmptyBlueprint::EmptyBlueprint(const FieldSpecBaseList &fields)
    : SimpleLeafBlueprint(fields)
{
}

EmptyBlueprint::EmptyBlueprint()
    : SimpleLeafBlueprint(FieldSpecBaseList())
{
}

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

SearchIterator::UP
SimpleBlueprint::createLeafSearch(const search::fef::TermFieldMatchDataArray &, bool) const
{
    SimpleSearch *ss = new SimpleSearch(_result);
    SearchIterator::UP search(ss);
    ss->tag(_tag);
    return search;
}

SearchIterator::UP
SimpleBlueprint::createFilterSearch(bool strict, FilterConstraint constraint) const
{
    SimpleSearch *ss = new SimpleSearch(_result);
    SearchIterator::UP search(ss);
    ss->tag(_tag +
            (strict ? "<strict," : "<nostrict,") +
            (constraint == FilterConstraint::UPPER_BOUND ? "upper>" : "lower>"));
    return search;
}

SimpleBlueprint::SimpleBlueprint(const SimpleResult &result)
    : SimpleLeafBlueprint(FieldSpecBaseList()),
      _tag(),
      _result(result)
{
    setEstimate(HitEstimate(result.getHitCount(), (result.getHitCount() == 0)));
}

SimpleBlueprint::~SimpleBlueprint() = default;

SimpleBlueprint &
SimpleBlueprint::tag(const vespalib::string &t)
{
    _tag = t;
    return *this;
}

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

namespace {

struct FakeContext : attribute::ISearchContext {
    const vespalib::string &name;
    const FakeResult &result;
    FakeContext(const vespalib::string &name_in, const FakeResult &result_in)
        : name(name_in), result(result_in) {}
    int32_t onFind(DocId docid, int32_t elemid, int32_t &weight) const override {
        for (const auto &doc: result.inspect()) {
            if (doc.docId == docid) {
                for (const auto &elem: doc.elements) {
                    if (elem.id >= uint32_t(elemid)) {
                        weight = elem.weight;
                        return elem.id;
                    }
                }
            }
        }
        return -1;
    }
    int32_t onFind(DocId docid, int32_t elem) const override {
        int32_t ignore_weight;
        return onFind(docid, elem, ignore_weight);
    }
    unsigned int approximateHits() const override { return 0; }
    std::unique_ptr<SearchIterator> createIterator(fef::TermFieldMatchData *, bool) override { abort(); }
    void fetchPostings(const ExecuteInfo &) override { }
    bool valid() const override { return true; }
    search::Int64Range getAsIntegerTerm() const override { abort(); }
    const search::QueryTermUCS4 * queryTerm() const override { abort(); }
    const vespalib::string &attributeName() const override { return name; }
};

}

SearchIterator::UP
FakeBlueprint::createLeafSearch(const fef::TermFieldMatchDataArray &tfmda, bool) const
{
    auto result = std::make_unique<FakeSearch>(_tag, _field.getName(), _term, _result, tfmda);
    result->attr_ctx(_ctx.get());
    return result;
}

FakeBlueprint::FakeBlueprint(const FieldSpec &field, const FakeResult &result)
    : SimpleLeafBlueprint(field),
      _tag("<tag>"),
      _term("<term>"),
      _field(field),
      _result(result),
      _ctx()
{
    setEstimate(HitEstimate(result.inspect().size(), result.inspect().empty()));
}

FakeBlueprint::~FakeBlueprint() = default;

FakeBlueprint &
FakeBlueprint::is_attr(bool value) {
    if (value) {
        _ctx = std::make_unique<FakeContext>(_field.getName(), _result);
    } else {
        _ctx.reset();
    }
    return *this;
}

}