summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/processing/SetLanguage.java
blob: 4cb6c3067135804476a145730d9878b3abbb99d9 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.processing;

import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.searchdefinition.RankProfileRegistry;
import com.yahoo.document.DataType;
import com.yahoo.searchdefinition.document.SDField;
import com.yahoo.searchdefinition.Search;
import com.yahoo.vespa.indexinglanguage.expressions.SetLanguageExpression;
import com.yahoo.vespa.model.container.search.QueryProfiles;

import java.util.ArrayList;
import java.util.List;

/**
 * Check that no text field appears before a field that sets language.
 *
 * @author <a href="mailto:gunnarga@yahoo-inc.com">Gunnar Gauslaa Bergem</a>
 */
public class SetLanguage extends Processor {

    public SetLanguage(Search search, DeployLogger deployLogger, RankProfileRegistry rankProfileRegistry, QueryProfiles queryProfiles) {
        super(search, deployLogger, rankProfileRegistry, queryProfiles);
    }

    @Override
    public void process() {
        List<String> textFieldsWithoutLanguage = new ArrayList<>();

        for (SDField field : search.allConcreteFields()) {
            if (fieldMustComeAfterLanguageSettingField(field)) {
                textFieldsWithoutLanguage.add(field.getName());
            }
            if (field.containsExpression(SetLanguageExpression.class) && !textFieldsWithoutLanguage.isEmpty()) {
                StringBuffer fieldString = new StringBuffer();
                for (String fieldName : textFieldsWithoutLanguage) {
                    fieldString.append(fieldName).append(" ");
                }
                warn(search, field, "Field '" + field.getName() + "' sets the language for this document, " +
                        "and should be defined as the first field in the searchdefinition. If you have both header and body fields, this field "+
                        "should be header, if you require it to affect subsequent header fields and/or any body fields. " +
                        "Preceding text fields that will not have their language set: " +
                        fieldString.toString() +
                        " (This warning is omitted for any subsequent fields that also do set_language.)");
                return;
            }
        }
    }

    private boolean fieldMustComeAfterLanguageSettingField(SDField field) {
        return (!field.containsExpression(SetLanguageExpression.class) &&
                (field.getDataType() == DataType.STRING));
    }

}