summaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/matching/indexenvironment.cpp
blob: 6fceb0db87f3712911f3b3ed5cfbf7d89340629e (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "indexenvironment.h"

#include <vespa/searchlib/fef/functiontablefactory.h>
#include <vespa/searchlib/fef/indexproperties.h>
#include <vespa/searchcore/proton/documentmetastore/documentmetastore.h>
#include <set>

using namespace search::fef;

namespace {

using StringSet = std::set<vespalib::string>;

void
consider_field_for_extraction(const vespalib::string& field_name, StringSet& virtual_fields)
{
    size_t pos = field_name.find_last_of('.');
    if (pos != vespalib::string::npos) {
        vespalib::string virtual_field = field_name.substr(0, pos);
        virtual_fields.insert(virtual_field);
        consider_field_for_extraction(virtual_field, virtual_fields);
    }
}

StringSet
extract_virtual_fields(const std::vector<search::fef::FieldInfo>& fields)
{
    // Fields that are represented by a set of attributes (normal and imported) in the backend are considered virtual fields.
    // Currently, this is map or array of struct fields (from the SD file) with struct-field attributes.
    // These attributes have '.' in their names, example: my_map.key and my_map.value represent a map<int, string>.
    StringSet result;
    for (const auto& field : fields) {
        if (field.hasAttribute()) {
            consider_field_for_extraction(field.name(), result);
        }
    }
    return result;
}

}

namespace proton::matching {

void
IndexEnvironment::extractFields(const search::index::Schema &schema)
{
    using SchemaField = search::index::Schema::Field;
    for (uint32_t i = 0; i < schema.getNumAttributeFields(); ++i) {
        const SchemaField &field = schema.getAttributeField(i);
        FieldInfo fieldInfo(FieldType::ATTRIBUTE, field.getCollectionType(), field.getName(), _fields.size());
        fieldInfo.set_data_type(field.getDataType());
        insertField(fieldInfo);
    }
    for (uint32_t i = 0; i < schema.getNumIndexFields(); ++i) {
        const SchemaField &field = schema.getIndexField(i);
        FieldInfo fieldInfo(FieldType::INDEX, field.getCollectionType(), field.getName(), _fields.size());
        fieldInfo.set_data_type(field.getDataType());
        if (indexproperties::IsFilterField::check(_properties, field.getName())) {
            fieldInfo.setFilter(true);
        }
        auto itr = _fieldNames.find(field.getName());
        if (itr != _fieldNames.end()) { // override the attribute field
            FieldInfo shadow_field(fieldInfo.type(), fieldInfo.collection(), fieldInfo.name(), itr->second);
            shadow_field.set_data_type(fieldInfo.get_data_type());
            shadow_field.addAttribute(); // tell ranking about the shadowed attribute
            _fields[itr->second] = shadow_field;
        } else {
            insertField(fieldInfo);
        }
    }
    for (const auto &attr : schema.getImportedAttributeFields()) {
        FieldInfo field(FieldType::ATTRIBUTE, attr.getCollectionType(), attr.getName(), _fields.size());
        field.set_data_type(attr.getDataType());
        insertField(field);
    }

    //TODO: This is a kludge to get [documentmetastore] searchable
    {
        FieldInfo fieldInfo(FieldType::HIDDEN_ATTRIBUTE, FieldInfo::CollectionType::SINGLE,
                            DocumentMetaStore::getFixedName(), _fields.size());
        fieldInfo.set_data_type(FieldInfo::DataType::RAW);
        fieldInfo.setFilter(true);
        insertField(fieldInfo);
    }
    for (const auto& field : extract_virtual_fields(_fields)) {
        FieldInfo info(FieldType::VIRTUAL, FieldInfo::CollectionType::ARRAY, field, _fields.size());
        info.set_data_type(FieldInfo::DataType::COMBINED);
        insertField(info);
    }
}

void
IndexEnvironment::insertField(const search::fef::FieldInfo &field)
{
    assert(field.id() == _fields.size());
    _fieldNames[field.name()] = _fields.size();
    _fields.push_back(field);
}

IndexEnvironment::IndexEnvironment(uint32_t distributionKey,
                                   const search::index::Schema &schema,
                                   search::fef::Properties props,
                                   const IRankingAssetsRepo &rankingAssetsRepo)
  : _tableManager(),
    _properties(std::move(props)),
    _fieldNames(),
    _fields(),
    _motivation(UNKNOWN),
    _rankingAssetsRepo(rankingAssetsRepo),
    _distributionKey(distributionKey)
{
    _tableManager.addFactory(std::make_shared<search::fef::FunctionTableFactory>(256));
    extractFields(schema);
}

const search::fef::Properties &
IndexEnvironment::getProperties() const
{
    return _properties;
}

uint32_t
IndexEnvironment::getNumFields() const
{
    return _fields.size();
}

const search::fef::FieldInfo *
IndexEnvironment::getField(uint32_t id) const
{
    if (id < _fields.size()) {
        return &_fields[id];
    }
    return nullptr;
}

const search::fef::FieldInfo *
IndexEnvironment::getFieldByName(const string &name) const
{
    auto pos = _fieldNames.find(name);
    if (pos == _fieldNames.end()) {
        return nullptr;
    }
    return getField(pos->second);
}

const search::fef::ITableManager &
IndexEnvironment::getTableManager() const {
    return _tableManager;
}

IIndexEnvironment::FeatureMotivation
IndexEnvironment::getFeatureMotivation() const {
    return _motivation;
}

void
IndexEnvironment::hintFeatureMotivation(FeatureMotivation motivation) const {
    _motivation = motivation;
}

void
IndexEnvironment::hintFieldAccess(uint32_t ) const { }

void
IndexEnvironment::hintAttributeAccess(const string &) const { }

IndexEnvironment::~IndexEnvironment() = default;

}