aboutsummaryrefslogtreecommitdiffstats
path: root/searchcommon/src/vespa/searchcommon/common/schemaconfigurer.cpp
blob: 401003cb74b290eb72accc040b364ea7efe77eaf (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
247
248
249
250
251
252
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "schemaconfigurer.h"
#include <vespa/config-attributes.h>
#include <vespa/config-imported-fields.h>
#include <vespa/config-indexschema.h>
#include <vespa/config-summary.h>
#include <vespa/searchcommon/common/schema.h>
#include <vespa/searchcommon/attribute/collectiontype.h>
#include <vespa/searchcommon/attribute/basictype.h>

#include <vespa/searchcommon/config/subscriptionproxyng.h>

#include <vespa/log/log.h>
LOG_SETUP(".index.schemaconfigurer");

using namespace config;
using namespace vespa::config::search;

namespace search::index {

using schema::DataType;
using schema::CollectionType;

namespace {

Schema::DataType
convertIndexDataType(const IndexschemaConfig::Indexfield::Datatype &type)
{
    switch (type) {
    case IndexschemaConfig::Indexfield::STRING:
        return DataType::STRING;
    case IndexschemaConfig::Indexfield::INT64:
        return DataType::INT64;
    case IndexschemaConfig::Indexfield::BOOLEANTREE:
        return DataType::BOOLEANTREE;
    }
    return DataType::STRING;
}


Schema::CollectionType
convertIndexCollectionType(const IndexschemaConfig::Indexfield::Collectiontype &type)
{
    switch (type) {
    case IndexschemaConfig::Indexfield::SINGLE:
        return CollectionType::SINGLE;
    case IndexschemaConfig::Indexfield::ARRAY:
        return CollectionType::ARRAY;
    case IndexschemaConfig::Indexfield::WEIGHTEDSET:
        return CollectionType::WEIGHTEDSET;
    }
    return CollectionType::SINGLE;
}

template <typename ConfigType>
Schema::DataType
convertDataType(const ConfigType &type)
{
    switch (type) {
    case ConfigType::STRING:
        return DataType::STRING;
    case ConfigType::BOOL:
        return DataType::BOOL;
    case ConfigType::UINT2:
        return DataType::UINT2;
    case ConfigType::UINT4:
        return DataType::UINT4;
    case ConfigType::INT8:
        return DataType::INT8;
    case ConfigType::INT16:
        return DataType::INT16;
    case ConfigType::INT32:
        return DataType::INT32;
    case ConfigType::INT64:
        return DataType::INT64;
    case ConfigType::FLOAT:
        return DataType::FLOAT;
    case ConfigType::DOUBLE:
        return DataType::DOUBLE;
    case ConfigType::PREDICATE:
        return DataType::BOOLEANTREE;
    case ConfigType::TENSOR:
        return DataType::TENSOR;
    case ConfigType::REFERENCE:
        return DataType::REFERENCE;
    default:
        break;
    }
    // TODO: exception?
    return DataType::STRING;
}

template <typename ConfigType>
Schema::CollectionType
convertCollectionType(const ConfigType &type)
{
    switch (type) {
    case ConfigType::SINGLE:
        return CollectionType::SINGLE;
    case ConfigType::ARRAY:
        return CollectionType::ARRAY;
    case ConfigType::WEIGHTEDSET:
        return CollectionType::WEIGHTEDSET;
    }
    return CollectionType::SINGLE;
}


Schema::DataType
convertSummaryType(const vespalib::string &type)
{
    if (type == "byte") {
        return DataType::INT8;
    } else if (type == "short") {
        return DataType::INT16;
    } else if (type == "integer") {
        return DataType::INT32;
    } else if (type == "int64") {
        return DataType::INT64;
    } else if (type == "float") {
        return DataType::FLOAT;
    } else if (type == "double") {
        return DataType::DOUBLE;
    } else if (type == "string" ||
               type == "longstring" ||
               type == "xmlstring" ||
               type == "featuredata" ||
               type == "jsonstring")
    {
        return DataType::STRING;
    } else if (type == "data" ||
               type == "longdata")
    {
        return DataType::RAW;
    }
    return DataType::RAW;
}

}

void
SchemaBuilder::build(const IndexschemaConfig &cfg, Schema &schema)
{
    for (size_t i = 0; i < cfg.indexfield.size(); ++i) {
        const IndexschemaConfig::Indexfield & f = cfg.indexfield[i];
        if ((f.datatype == IndexschemaConfig::Indexfield::BOOLEANTREE &&
            f.collectiontype == IndexschemaConfig::Indexfield::SINGLE) ||
            (f.indextype == IndexschemaConfig::Indexfield::RISE))
        {
            LOG(warning, "Your field '%s' is a rise index. Those are no longer supported as of Vespa-5.89.\n"
                         " Redeploy and follow instructions to mitigate.", f.name.c_str());
        } else {
            schema.addIndexField(Schema::IndexField(f.name, convertIndexDataType(f.datatype),
                                                    convertIndexCollectionType(f.collectiontype)).
                                 setPrefix(f.prefix).
                                 setPhrases(f.phrases).
                                 setPositions(f.positions).
                                 setAvgElemLen(f.averageelementlen));
        }
    }
    for (size_t i = 0; i < cfg.fieldset.size(); ++i) {
        const IndexschemaConfig::Fieldset &fs = cfg.fieldset[i];
        Schema::FieldSet toAdd(fs.name);
        for (size_t j = 0; j < fs.field.size(); ++j) {
            toAdd.addField(fs.field[j].name);
        }
        schema.addFieldSet(toAdd);
    }
}


void
SchemaBuilder::build(const AttributesConfig &cfg, Schema &schema)
{
    for (const auto &attr : cfg.attribute) {
        if (attr.imported) {
            schema.addImportedAttributeField(Schema::ImportedAttributeField(attr.name,
                                                                            convertDataType(attr.datatype),
                                                                            convertCollectionType(attr.collectiontype)));
        } else {
            schema.addAttributeField(Schema::Field(attr.name,
                                                   convertDataType(attr.datatype),
                                                   convertCollectionType(attr.collectiontype)));
        }
    }
}


void
SchemaBuilder::build(const SummaryConfig &cfg, Schema &schema)
{
    for (size_t i = 0; i < cfg.classes.size(); ++i) {
        LOG(debug, "class with index %lu has id %d (default has id %d)",
            i, cfg.classes[i].id, cfg.defaultsummaryid);
    }
    for (size_t i = 0; i < cfg.classes.size(); ++i) {
        // use the default summary class that has all fields
        if (cfg.classes[i].id == cfg.defaultsummaryid) {
            for (size_t j = 0; j < cfg.classes[i].fields.size(); ++j) {
                const SummaryConfig::Classes::Fields & f =
                    cfg.classes[i].fields[j];
                schema.addSummaryField(Schema::Field(f.name,
                                               convertSummaryType(f.type)));
            }
            return;
        }
    }
    if (cfg.classes.empty()) {
        LOG(debug,
            "No summary class configured that match the default summary id %d",
            cfg.defaultsummaryid);
    } else {
        LOG(warning,
            "No summary class configured that match the default summary id %d",
            cfg.defaultsummaryid);
    }
}

void
SchemaConfigurer::configure(const IndexschemaConfig &cfg)
{
    SchemaBuilder::build(cfg, _schema);
}

void
SchemaConfigurer::configure(const AttributesConfig &cfg)
{
    SchemaBuilder::build(cfg, _schema);
}

void
SchemaConfigurer::configure(const SummaryConfig & cfg)
{
    SchemaBuilder::build(cfg, _schema);
}

SchemaConfigurer::SchemaConfigurer(Schema &schema, const vespalib::string &configId)
    : _schema(schema)
{
    search::SubscriptionProxyNg<SchemaConfigurer, IndexschemaConfig>
        indexSchemaSubscriber(*this, &SchemaConfigurer::configure);
    search::SubscriptionProxyNg<SchemaConfigurer, AttributesConfig>
        attributesSubscriber(*this, &SchemaConfigurer::configure);
    search::SubscriptionProxyNg<SchemaConfigurer, SummaryConfig>
        summarySubscriber(*this, &SchemaConfigurer::configure);
    indexSchemaSubscriber.subscribe(configId.c_str());
    attributesSubscriber.subscribe(configId.c_str());
    summarySubscriber.subscribe(configId.c_str());
}

}