aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/application/validation/change/search/IndexingScriptChangeValidatorTest.java
blob: 7a5b235737a0a8bfe9bd2c3ef3740eb89c0e669f (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.application.validation.change.search;

import com.yahoo.vespa.indexinglanguage.expressions.ScriptExpression;
import com.yahoo.config.application.api.ValidationOverrides;
import com.yahoo.vespa.model.application.validation.change.VespaConfigChangeAction;
import com.yahoo.vespa.model.application.validation.change.VespaRefeedAction;
import org.junit.Test;

import java.time.Instant;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertTrue;

public class IndexingScriptChangeValidatorTest {

    private static class Fixture extends ContentClusterFixture {
        IndexingScriptChangeValidator validator;

        public Fixture(String currentSd, String nextSd) throws Exception {
            super(currentSd, nextSd);
            validator = new IndexingScriptChangeValidator(currentDb().getDerivedConfiguration().getSearch(),
                    nextDb().getDerivedConfiguration().getSearch());
        }

        @Override
        public List<VespaConfigChangeAction> validate() {
            return validator.validate(ValidationOverrides.empty, Instant.now());
        }
    }

    private static class ScriptFixture {
        private final ScriptExpression currentScript;
        private final ScriptExpression nextScript;

        public ScriptFixture(String currentScript, String nextScript) throws Exception {
            this.currentScript = ScriptExpression.fromString(currentScript);
            this.nextScript = ScriptExpression.fromString(nextScript);
        }

        public boolean validate() {
            return IndexingScriptChangeValidator.equalScripts(currentScript, nextScript);
        }
    }

    private static String FIELD = "field f1 type string";
    private static String FIELD_F2 = "field f2 type string";

    private static VespaConfigChangeAction expectedAction(String changedMsg, String fromScript, String toScript) {
        return expectedAction("f1", changedMsg, fromScript, toScript);
    }

    private static VespaConfigChangeAction expectedAction(String field, String changedMsg, String fromScript, String toScript) {
        return VespaRefeedAction.of("indexing-change",
                                    ValidationOverrides.empty,
                                    "Field '" + field + "' changed: " +
                                    (changedMsg.isEmpty() ? "" : changedMsg + ", ") +
                                    "indexing script: '" + fromScript + "' -> '" + toScript + "'", 
                                    Instant.now());
    }

    @Test
    public void requireThatAddingIndexAspectRequireRefeed() throws Exception {
        new Fixture(FIELD + " { indexing: summary }",
                    FIELD + " { indexing: index | summary }").
        assertValidation(expectedAction("add index aspect",
                "{ input f1 | summary f1; }",
                "{ input f1 | tokenize normalize stem:\"BEST\" | index f1 | summary f1; }"));
    }

    @Test
    public void requireThatRemovingIndexAspectRequireRefeed() throws Exception {
        new Fixture(FIELD + " { indexing: index | summary }",
                    FIELD + " { indexing: summary }").
                assertValidation(expectedAction("remove index aspect",
                        "{ input f1 | tokenize normalize stem:\"BEST\" | index f1 | summary f1; }",
                        "{ input f1 | summary f1; }"));
    }

    @Test
    public void requireThatChangingStemmingRequireRefeed() throws Exception {
        new Fixture(FIELD + " { indexing: index }",
                    FIELD + " { indexing: index \n stemming: none }").
                assertValidation(expectedAction("stemming: 'best' -> 'none'",
                        "{ input f1 | tokenize normalize stem:\"BEST\" | index f1; }",
                        "{ input f1 | tokenize normalize | index f1; }"));
    }

    @Test
    public void requireThatChangingNormalizingRequireRefeed() throws Exception {
        new Fixture(FIELD + " { indexing: index }",
                    FIELD + " { indexing: index \n normalizing: none }").
                assertValidation(expectedAction("normalizing: 'ACCENT' -> 'NONE'",
                        "{ input f1 | tokenize normalize stem:\"BEST\" | index f1; }",
                        "{ input f1 | tokenize stem:\"BEST\" | index f1; }"));
    }

    @Test
    public void requireThatChangingMatchingRequireRefeed() throws Exception {
        new Fixture(FIELD + " { indexing: index \n match: exact }",
                    FIELD + " { indexing: index \n match { gram \n gram-size: 3 } }").
                assertValidation(expectedAction("matching: 'exact' -> 'gram (size 3)', normalizing: 'LOWERCASE' -> 'CODEPOINT'",
                        "{ input f1 | exact | index f1; }",
                        "{ input f1 | ngram 3 | index f1; }"));
    }

    @Test
    public void requireThatSettingDynamicSummaryRequireRefeed() throws Exception {
        new Fixture(FIELD + " { indexing: summary }",
                    FIELD + " { indexing: summary \n summary: dynamic }").
                assertValidation(expectedAction("summary field 'f1' transform: 'none' -> 'dynamicteaser'",
                        "{ input f1 | summary f1; }",
                        "{ input f1 | tokenize normalize stem:\"BEST\" | summary f1; }"));
    }

    @Test
    public void requireThatMultipleChangesRequireRefeed() throws Exception {
         new Fixture(FIELD + " { indexing: index } " + FIELD_F2 + " { indexing: index }",
                     FIELD + " { indexing: index \n stemming: none } " + FIELD_F2 + " { indexing: index \n normalizing: none }").
                 assertValidation(Arrays.asList(expectedAction("f1", "stemming: 'best' -> 'none'",
                                 "{ input f1 | tokenize normalize stem:\"BEST\" | index f1; }",
                                 "{ input f1 | tokenize normalize | index f1; }"),
                         expectedAction("f2", "normalizing: 'ACCENT' -> 'NONE'",
                                 "{ input f2 | tokenize normalize stem:\"BEST\" | index f2; }",
                                 "{ input f2 | tokenize stem:\"BEST\" | index f2; }")));
    }

    @Test
    public void requireThatAddingIndexFieldIsOk() throws Exception {
        new Fixture("", "field f1 type string { indexing: index | summary }").
                assertValidation();
    }

    @Test
    public void requireThatRemovingIndexFieldIsOk() throws Exception {
        new Fixture("field f1 type string { indexing: index | summary }", "").
                assertValidation();
    }

    @Test
    public void requireThatAddingFieldIsOk() throws Exception {
        new Fixture("", FIELD + " { indexing: attribute | summary }").
                assertValidation();
    }

    @Test
    public void requireThatAddingSummaryAspectIsOk() throws Exception {
        new Fixture(FIELD + " { indexing: attribute }",
                    FIELD + " { indexing: attribute | summary }").
                assertValidation();
    }

    @Test
    public void requireThatSettingDynamicSummaryOnIndexFieldIsOk() throws Exception {
        new Fixture(FIELD + " { indexing: index | summary }",
                    FIELD + " { indexing: index | summary \n summary: dynamic }").
                assertValidation();
    }

    @Test
    public void requireThatOutputExpressionsAreIgnoredInAdvancedScript() throws Exception {
        assertTrue(new ScriptFixture("{ input foo | switch { case \"audio\": input bar | index; case \"video\": input baz | index; default: 0 | index; }; }",
                "{ input foo | switch { case \"audio\": input bar | attribute; case \"video\": input baz | attribute; default: 0 | attribute; }; }").
                validate());
    }

}