aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/query/streaming/queryterm.cpp
blob: 728b9a2ab76549bd08f640837b770a16aace8de6 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "queryterm.hpp"
#include <vespa/searchlib/fef/itermdata.h>
#include <vespa/searchlib/fef/matchdata.h>
#include <vespa/vespalib/objects/visit.h>
#include <algorithm>
#include <limits>

#include <vespa/log/log.h>
LOG_SETUP(".searchlib.query.streaming.queryterm");

using search::fef::ITermData;
using search::fef::ITermFieldData;
using search::fef::MatchData;
using search::fef::TermFieldMatchData;
using search::fef::TermFieldMatchDataPosition;

namespace {

class CharInfo {
public:
    CharInfo();
    uint8_t get(uint8_t c) const noexcept { return _charInfo[c]; }
private:
    uint8_t _charInfo[256];
};

CharInfo::CharInfo()
    : _charInfo()
{
    // XXX: Should refactor to reduce number of magic constants.
    memset(_charInfo, 0x01, 128); // All 7 bits are ascii7bit
    memset(_charInfo+128, 0x00, 128); // The rest are not.
    memset(_charInfo + static_cast<uint8_t>('0'), 0x07, 10);
    _charInfo[uint8_t('-')] = 0x07;
    _charInfo[uint8_t('<')] = 0x07;
    _charInfo[uint8_t('>')] = 0x07;
    _charInfo[uint8_t(';')] = 0x07;
    _charInfo[uint8_t('[')] = 0x07;
    _charInfo[uint8_t(']')] = 0x07;

    _charInfo[uint8_t('.')] = 0x05;
    _charInfo[uint8_t('+')] = 0x05;
    _charInfo[uint8_t('e')] = 0x05;
    _charInfo[uint8_t('E')] = 0x05;
}

CharInfo G_charTable;

}

namespace search::streaming {

QueryTerm::~QueryTerm() = default;

void
QueryTerm::visitMembers(vespalib::ObjectVisitor & visitor) const
{
    QueryTermUCS4::visitMembers(visitor);
    visit(visitor, "encoding.isBase10Integer", _encoding.isBase10Integer());
    visit(visitor, "encoding.isFloat", _encoding.isFloat());
    visit(visitor, "encoding.isAscii7Bit", _encoding.isAscii7Bit());
    visit(visitor, "index", _index);
    visit(visitor, "weight", _weight.percent());
    visit(visitor, "uniqueid", _uniqueId);
}

QueryTerm::QueryTerm(std::unique_ptr<QueryNodeResultBase> org, stringref termS, const string & indexS,
                     Type type, Normalizing normalizing)
    : QueryTermUCS4(QueryNormalization::optional_fold(termS, type, normalizing), type),
      _hitList(),
      _index(indexS),
      _result(org.release()),
      _encoding(0x01),
      _isRanked(true),
      _filter(false),
      _weight(100),
      _uniqueId(0),
      _fieldInfo()
{
    if (!empty()) {
        uint8_t enc(0xff);
        for (char c : getTermString()) {
            enc &= G_charTable.get(c);
        }
        _encoding = EncodingBitMap(enc);
    }
}

void QueryTerm::getLeaves(QueryTermList & tl)                { tl.push_back(this); }
void QueryTerm::getLeaves(ConstQueryTermList & tl)     const { tl.push_back(this); }
bool QueryTerm::evaluate()                             const { return !_hitList.empty(); }
void QueryTerm::reset()                                      { _hitList.clear(); }
const HitList & QueryTerm::evaluateHits(HitList &) const { return _hitList; }

void QueryTerm::resizeFieldId(size_t fieldNo)
{
    if (fieldNo >= _fieldInfo.size()) {
        _fieldInfo.resize(std::max(32ul, fieldNo + 1));
    }
}

uint32_t
QueryTerm::add(uint32_t field_id, uint32_t element_id, int32_t element_weight, uint32_t position)
{
    uint32_t idx = _hitList.size();
    _hitList.emplace_back(field_id, element_id, element_weight, position);
    return idx;
}

void
QueryTerm::set_element_length(uint32_t hitlist_idx, uint32_t element_length)
{
    _hitList[hitlist_idx].set_element_length(element_length);
}

void
QueryTerm::unpack_match_data(uint32_t docid, const fef::ITermData& td, fef::MatchData& match_data, const fef::IIndexEnvironment& index_env)
{
    HitList list;
    const HitList & hit_list = evaluateHits(list);
    unpack_match_data_helper(docid, td, match_data, hit_list, *this, is_filter(), index_env);
}

NearestNeighborQueryNode*
QueryTerm::as_nearest_neighbor_query_node() noexcept
{
    return nullptr;
}

MultiTerm*
QueryTerm::as_multi_term() noexcept
{
    return nullptr;
}

RegexpTerm*
QueryTerm::as_regexp_term() noexcept
{
    return nullptr;
}

FuzzyTerm*
QueryTerm::as_fuzzy_term() noexcept
{
    return nullptr;
}

EquivQueryNode*
QueryTerm::as_equiv_query_node() noexcept
{
    return nullptr;
}

const EquivQueryNode*
QueryTerm::as_equiv_query_node() const noexcept
{
    return nullptr;
}

}