aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/schema/NoNormalizersTestCase.java
blob: 6e2efadfa2c02eb5c92d1c2040520cfee7b39eb9 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema;

import com.yahoo.search.query.profile.QueryProfileRegistry;
import com.yahoo.schema.parser.ParseException;
import com.yahoo.yolean.Exceptions;
import ai.vespa.rankingexpression.importer.configmodelview.ImportedMlModels;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
 * Tests rank profiles with normalizers in bad places
 *
 * @author arnej
 */
public class NoNormalizersTestCase extends AbstractSchemaTestCase {

    void compileSchema(String schema) throws ParseException {
        RankProfileRegistry registry = new RankProfileRegistry();
        var qp = new QueryProfileRegistry();
        ApplicationBuilder builder = new ApplicationBuilder(registry, qp);
        builder.addSchema(schema);
        builder.build(true);
        for (RankProfile rp : registry.all()) {
            rp.compile(qp, new ImportedMlModels());
        }
    }

    @Test
    void requireThatNormalizerInFirstPhaseIsChecked() throws ParseException {
        try {
            compileSchema("""
                          search test {
                            document test { }
                            rank-profile p1 {
                              first-phase {
                                expression: normalize_linear(nativeRank)
                              }
                            }
                          }
                          """);
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Rank profile 'p1' is invalid: " +
                         "Cannot use normalize_linear(nativeRank) from first-phase expression, only valid in global-phase expression",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void requireThatNormalizerInSecondPhaseIsChecked() throws ParseException {
        try {
            compileSchema("""
                          search test {
                            document test {
                                field title type string {
                                    indexing: index
                                }
                            }
                            rank-profile p2 {
                                function foobar() {
                                    expression: 42 + reciprocal_rank(whatever, 1.0)
                                }
                                function whatever() {
                                    expression: fieldMatch(title)
                                }
                                first-phase {
                                    expression: nativeRank
                                }
                                second-phase {
                                    expression: foobar
                                }
                            }
                          }
                          """);
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Rank profile 'p2' is invalid: " +
                         "Cannot use reciprocal_rank(whatever,1.0) from second-phase expression, only valid in global-phase expression",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void requireThatNormalizerInMatchFeatureIsChecked() throws ParseException {
        try {
            compileSchema("""
                          search test {
                            document test { }
                            rank-profile p3 {
                                function foobar() {
                                    expression: normalize_linear(nativeRank)
                                }
                                first-phase {
                                    expression: nativeRank
                                }
                                match-features {
                                    nativeRank
                                    foobar
                                }
                            }
                          }
                          """);
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Rank profile 'p3' is invalid: " +
                         "Cannot use normalize_linear(nativeRank) from match-feature foobar, only valid in global-phase expression",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void requireThatNormalizerInSummaryFeatureIsChecked() throws ParseException {
        try {
            compileSchema("""
                          search test {
                            document test { }
                            rank-profile p4 {
                                function foobar() {
                                    expression: normalize_linear(nativeRank)
                                }
                                first-phase {
                                    expression: nativeRank
                                }
                                summary-features {
                                    nativeRank
                                    foobar
                                }
                            }
                          }
                          """);
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Rank profile 'p4' is invalid: " +
                         "Cannot use normalize_linear(nativeRank) from summary-feature foobar, only valid in global-phase expression",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void requireThatNormalizerInNormalizerIsChecked() throws ParseException {
        try {
            compileSchema("""
                          search test {
                            document test {
                                field title type string {
                                    indexing: index
                                }
                            }
                            rank-profile p5 {
                                function foobar() {
                                    expression: reciprocal_rank(nativeRank)
                                }
                                first-phase {
                                    expression: nativeRank
                                }
                                global-phase {
                                    expression: normalize_linear(fieldMatch(title)) + normalize_linear(foobar)
                                }
                            }
                          }
                          """);
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Rank profile 'p5' is invalid: " +
                         "Cannot use reciprocal_rank(nativeRank) from normalizer input foobar, only valid in global-phase expression",
                         Exceptions.toMessageString(e));
        }
    }
}