aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/diskindex/disktermblueprint.cpp
blob: 08a690ce9fe9afbb49ff328faebbec630c8f4fef (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "disktermblueprint.h"
#include <vespa/searchlib/common/bitvectoriterator.h>
#include <vespa/searchlib/queryeval/booleanmatchiteratorwrapper.h>
#include <vespa/searchlib/queryeval/filter_wrapper.h>
#include <vespa/searchlib/queryeval/intermediate_blueprints.h>
#include <vespa/vespalib/objects/visit.h>
#include <vespa/vespalib/util/stringfmt.h>

#include <vespa/log/log.h>
LOG_SETUP(".diskindex.disktermblueprint");

using search::BitVectorIterator;
using search::fef::TermFieldMatchDataArray;
using search::index::Schema;
using search::queryeval::BooleanMatchIteratorWrapper;
using search::queryeval::FieldSpec;
using search::queryeval::FieldSpecBaseList;
using search::queryeval::SearchIterator;
using search::queryeval::LeafBlueprint;
using search::queryeval::Blueprint;

namespace search::diskindex {

namespace {

vespalib::string
getName(uint32_t indexId)
{
    return vespalib::make_string("fieldId(%u)", indexId);
}

}

DiskTermBlueprint::DiskTermBlueprint(const FieldSpec & field,
                                     const DiskIndex & diskIndex,
                                     const vespalib::string& query_term,
                                     DiskIndex::LookupResult::UP lookupRes,
                                     bool useBitVector) :
    SimpleLeafBlueprint(field),
    _field(field),
    _diskIndex(diskIndex),
    _query_term(query_term),
    _lookupRes(std::move(lookupRes)),
    _useBitVector(useBitVector),
    _fetchPostingsDone(false),
    _postingHandle(),
    _bitVector()
{
    setEstimate(HitEstimate(_lookupRes->counts._numDocs,
                            _lookupRes->counts._numDocs == 0));
}

void
DiskTermBlueprint::fetchPostings(const queryeval::ExecuteInfo &execInfo)
{
    (void) execInfo;
    if (!_fetchPostingsDone) {
        _bitVector = _diskIndex.readBitVector(*_lookupRes);
        if (!_useBitVector || !_bitVector) {
            _postingHandle = _diskIndex.readPostingList(*_lookupRes);
        }
    }
    _fetchPostingsDone = true;
}

SearchIterator::UP
DiskTermBlueprint::createLeafSearch(const TermFieldMatchDataArray & tfmda, bool strict) const
{
    if (_bitVector && (_useBitVector || tfmda[0]->isNotNeeded())) {
        LOG(debug, "Return BitVectorIterator: %s, wordNum(%" PRIu64 "), docCount(%" PRIu64 ")",
            getName(_lookupRes->indexId).c_str(), _lookupRes->wordNum, _lookupRes->counts._numDocs);
        return BitVectorIterator::create(_bitVector.get(), *tfmda[0], strict);
    }
    SearchIterator::UP search(_postingHandle->createIterator(_lookupRes->counts, tfmda, _useBitVector));
    if (_useBitVector) {
        LOG(debug, "Return BooleanMatchIteratorWrapper: %s, wordNum(%" PRIu64 "), docCount(%" PRIu64 ")",
            getName(_lookupRes->indexId).c_str(), _lookupRes->wordNum, _lookupRes->counts._numDocs);
        return std::make_unique<BooleanMatchIteratorWrapper>(std::move(search), tfmda);
    }
    LOG(debug, "Return posting list iterator: %s, wordNum(%" PRIu64 "), docCount(%" PRIu64 ")",
        getName(_lookupRes->indexId).c_str(), _lookupRes->wordNum, _lookupRes->counts._numDocs);
    return search;
}

SearchIterator::UP
DiskTermBlueprint::createFilterSearch(bool strict, FilterConstraint) const
{
    auto wrapper = std::make_unique<queryeval::FilterWrapper>(getState().numFields());
    auto & tfmda = wrapper->tfmda();
    if (_bitVector) {
        wrapper->wrap(BitVectorIterator::create(_bitVector.get(), *tfmda[0], strict));
    } else {
        wrapper->wrap(_postingHandle->createIterator(_lookupRes->counts, tfmda, _useBitVector));
    }
    return wrapper;
}

void
DiskTermBlueprint::visitMembers(vespalib::ObjectVisitor& visitor) const
{
    SimpleLeafBlueprint::visitMembers(visitor);
    visit(visitor, "field_name", _field.getName());
    visit(visitor, "query_term", _query_term);
}

} // namespace