summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/processing/FieldSetValidity.java
blob: 11b7bd86dbe30d09552c2d5c087eb8907d606262 (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
// 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.searchdefinition.Search;
import com.yahoo.searchdefinition.document.FieldSet;
import com.yahoo.searchdefinition.document.ImmutableSDField;
import com.yahoo.searchdefinition.document.Matching;
import com.yahoo.searchdefinition.document.NormalizeLevel;
import com.yahoo.searchdefinition.document.Stemming;
import com.yahoo.vespa.model.container.search.QueryProfiles;

/**
 * Computes the right "index commands" for each fieldset in a search definition.
 *
 * @author vegardh
 * @author bratseth
 */
public class FieldSetValidity extends Processor {

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

    @Override
    public void process() {
        for (FieldSet fieldSet : search.fieldSets().userFieldSets().values()) {
            checkFieldNames(search, fieldSet);
            checkMatching(search, fieldSet);
            checkNormalization(search, fieldSet);
            checkStemming(search, fieldSet);
        }
    }

    private static void checkFieldNames(Search search, FieldSet fieldSet) {
        for (String fld : fieldSet.getFieldNames()) {
            ImmutableSDField field = search.getField(fld);
            if (field == null) {
                throw new IllegalArgumentException(
                        "For search '" + search.getName() + "': Field '"+ fld + "' in " + fieldSet + " does not exist.");
            }
        }
    }

    private void checkMatching(Search search, FieldSet fieldSet) {
        Matching fsMatching = null;
        for (String fld : fieldSet.getFieldNames()) {
            ImmutableSDField field = search.getField(fld);
            Matching fieldMatching = field.getMatching();
            if (fsMatching==null) {
                fsMatching = fieldMatching;
            } else {
                if (fsMatching!=null && !fsMatching.equals(fieldMatching)) {
                    warn(search, field.asField(),
                            "The matching settings for the fields in " + fieldSet + " are inconsistent " +
                                    "(explicitly or because of field type). This may lead to recall and ranking issues.");
                    return;
                }
            }
        }
    }

    private void checkNormalization(Search search, FieldSet fieldSet) {
        NormalizeLevel.Level fsNorm = null;
        for (String fld : fieldSet.getFieldNames()) {
            ImmutableSDField field = search.getField(fld);
            NormalizeLevel.Level fieldNorm = field.getNormalizing().getLevel();
            if (fsNorm==null) {
                fsNorm = fieldNorm;
            } else {
                if (fsNorm!=null && !fsNorm.equals(fieldNorm)) {
                    warn(search, field.asField(),
                            "The normalization settings for the fields in " + fieldSet + " are inconsistent " +
                                    "(explicitly or because of field type). This may lead to recall and ranking issues.");
                    return;
                }
            }
        }
    }

    private void checkStemming(Search search, FieldSet fieldSet) {
        Stemming fsStemming = null;
        for (String fld : fieldSet.getFieldNames()) {
            ImmutableSDField field = search.getField(fld);
            Stemming fieldStemming = field.getStemming();
            if (fsStemming==null) {
                fsStemming = fieldStemming;
            } else {
                if (fsStemming!=null && !fsStemming.equals(fieldStemming)) {
                    warn(search, field.asField(),
                            "The stemming settings for the fields in the fieldset '"+fieldSet.getName()+
                                    "' are inconsistent (explicitly or because of field type). " +
                                    "This may lead to recall and ranking issues.");
                    return;
                }
            }
        }
    }

}