aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/derived/ImportedFields.java
blob: 5fb73445879019b1df809748cf77ff034aac8492 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.derived;

import com.yahoo.searchdefinition.Search;
import com.yahoo.searchdefinition.document.Attribute;
import com.yahoo.searchdefinition.document.ImportedField;
import com.yahoo.vespa.config.search.ImportedFieldsConfig;

import java.util.Optional;

/**
 * This class derives imported fields from search definition and produces imported-fields.cfg as needed by the search backend.
 *
 * @author geirst
 */
public class ImportedFields extends Derived implements ImportedFieldsConfig.Producer {

    private Optional<com.yahoo.searchdefinition.document.ImportedFields> importedFields = Optional.empty();

    public ImportedFields(Search search) {
        derive(search);
    }

    @Override
    protected void derive(Search search) {
        importedFields = search.importedFields();
    }

    @Override
    protected String getDerivedName() {
        return "imported-fields";
    }

    @Override
    public void getConfig(ImportedFieldsConfig.Builder builder) {
        if (importedFields.isPresent()) {
            importedFields.get().fields().forEach( (name, field) -> considerField(builder, field));
        }
    }

    private static void considerField(ImportedFieldsConfig.Builder builder, ImportedField field) {
        if (field.targetField().doesAttributing()) {
            builder.attribute.add(createAttributeBuilder(field));
        }
    }

    private static ImportedFieldsConfig.Attribute.Builder createAttributeBuilder(ImportedField field) {
        ImportedFieldsConfig.Attribute.Builder result = new ImportedFieldsConfig.Attribute.Builder();
        Attribute targetAttribute = field.targetField().getAttributes().get(field.targetField().getName());
        result.name(field.fieldName());
        result.referencefield(field.reference().referenceField().getName());
        result.targetfield(field.targetField().getName());
        result.datatype(getDataType(targetAttribute));
        result.collectiontype(getCollectionType(targetAttribute));
        return result;
    }

    private static ImportedFieldsConfig.Attribute.Datatype.Enum getDataType(Attribute targetAttribute) {
        return ImportedFieldsConfig.Attribute.Datatype.Enum.valueOf(targetAttribute.getType().getExportAttributeTypeName());
    }

    private static ImportedFieldsConfig.Attribute.Collectiontype.Enum getCollectionType(Attribute targetAttribute) {
        return ImportedFieldsConfig.Attribute.Collectiontype.Enum.valueOf(targetAttribute.getCollectionType().getName());
    }

}