aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/searchdefinition/RankingExpressionConstantsTestCase.java
blob: d84d967a184c82d7a5193fa28261a1f5fb4d1332 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition;

import com.yahoo.collections.Pair;
import com.yahoo.search.query.profile.QueryProfileRegistry;
import ai.vespa.rankingexpression.importer.configmodelview.ImportedMlModels;
import com.yahoo.yolean.Exceptions;
import com.yahoo.searchdefinition.derived.AttributeFields;
import com.yahoo.searchdefinition.derived.RawRankProfile;
import com.yahoo.searchdefinition.parser.ParseException;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.*;

/**
 * @author bratseth
 */
public class RankingExpressionConstantsTestCase extends SchemaTestCase {

    @Test
    public void testConstants() throws ParseException {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        QueryProfileRegistry queryProfileRegistry = new QueryProfileRegistry();
        SearchBuilder builder = new SearchBuilder(rankProfileRegistry);
        builder.importString(
                "search test {\n" +
                        "    document test { \n" +
                        "        field a type string { \n" +
                        "            indexing: index \n" +
                        "        }\n" +
                        "    }\n" +
                        "    \n" +
                        "    rank-profile parent {\n" +
                        "        constants {\n" +
                        "            p1: 7 \n" +
                        "            p2: 0 \n" +
                        "        }\n" +
                        "        first-phase {\n" +
                        "            expression: p2 * (1.3 + p1 )\n" +
                        "        }\n" +
                        "    }\n" +
                        "    rank-profile child1 inherits parent {\n" +
                        "        first-phase {\n" +
                        "            expression: a + b + c \n" +
                        "        }\n" +
                        "        second-phase {\n" +
                        "            expression: a + p1 + c \n" +
                        "        }\n" +
                        "        constants {\n" +
                        "            a: 1.0 \n" +
                        "            b: 2 \n" +
                        "            c: 3.5 \n" +
                        "        }\n" +
                        "    }\n" +
                        "    rank-profile child2 inherits parent {\n" +
                        "        constants {\n" +
                        "            p2: 2.0 \n" +
                        "        }\n" +
                        "        function foo() {\n" +
                        "            expression: p2*p1\n" +
                        "        }\n" +
                        "    }\n" +
                        "\n" +
                        "}\n");
        builder.build();
        Search s = builder.getSearch();
        RankProfile parent = rankProfileRegistry.get(s, "parent").compile(queryProfileRegistry, new ImportedMlModels());
        assertEquals("0.0", parent.getFirstPhaseRanking().getRoot().toString());

        RankProfile child1 = rankProfileRegistry.get(s, "child1").compile(queryProfileRegistry, new ImportedMlModels());
        assertEquals("6.5", child1.getFirstPhaseRanking().getRoot().toString());
        assertEquals("11.5", child1.getSecondPhaseRanking().getRoot().toString());

        RankProfile child2 = rankProfileRegistry.get(s, "child2").compile(queryProfileRegistry, new ImportedMlModels());
        assertEquals("16.6", child2.getFirstPhaseRanking().getRoot().toString());
        assertEquals("foo: 14.0", child2.getFunctions().get("foo").function().getBody().toString());
        List<Pair<String, String>> rankProperties = new RawRankProfile(child2,
                                                                       queryProfileRegistry,
                                                                       new ImportedMlModels(),
                                                                       new AttributeFields(s)).configProperties();
        assertEquals("(rankingExpression(foo).rankingScript,14.0)", rankProperties.get(0).toString());
        assertEquals("(rankingExpression(firstphase).rankingScript,16.6)", rankProperties.get(2).toString());
    }

    @Test
    public void testNameCollision() throws ParseException {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        SearchBuilder builder = new SearchBuilder(rankProfileRegistry);
        builder.importString(
                "search test {\n" +
                        "    document test { \n" +
                        "        field a type string { \n" +
                        "            indexing: index \n" +
                        "        }\n" +
                        "    }\n" +
                        "    \n" +
                        "    rank-profile test {\n" +
                        "        constants {\n" +
                        "            c: 7 \n" +
                        "        }\n" +
                        "        function c() {\n" +
                        "            expression: p2*p1\n" +
                        "        }\n" +
                        "    }\n" +
                        "\n" +
                        "}\n");
        builder.build();
        Search s = builder.getSearch();
        try {
            rankProfileRegistry.get(s, "test").compile(new QueryProfileRegistry(), new ImportedMlModels());
            fail("Should have caused an exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("Rank profile 'test' is invalid: Cannot have both a constant and function named 'c'",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    public void testNegativeLiteralArgument() throws ParseException {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        SearchBuilder builder = new SearchBuilder(rankProfileRegistry);
        builder.importString(
                "search test {\n" +
                        "    document test { \n" +
                        "        field a type string { \n" +
                        "            indexing: index \n" +
                        "        }\n" +
                        "    }\n" +
                        "    \n" +
                        "    rank-profile test {\n" +
                        "        function POP_SLOW_SCORE() {\n" +
                        "            expression:  safeLog(popShareSlowDecaySignal, -9.21034037)\n" +
                        "        }\n" +
                        "    }\n" +
                        "\n" +
                        "}\n");
        builder.build();
        Search s = builder.getSearch();
        RankProfile profile = rankProfileRegistry.get(s, "test");
        assertEquals("safeLog(popShareSlowDecaySignal,-9.21034037)", profile.getFunctions().get("POP_SLOW_SCORE").function().getBody().getRoot().toString());
    }

    @Test
    public void testNegativeConstantArgument() throws ParseException {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        SearchBuilder builder = new SearchBuilder(rankProfileRegistry);
        builder.importString(
                "search test {\n" +
                        "    document test { \n" +
                        "        field a type string { \n" +
                        "            indexing: index \n" +
                        "        }\n" +
                        "    }\n" +
                        "    \n" +
                        "    rank-profile test {\n" +
                        "        constants {\n" +
                        "            myValue: -9.21034037\n" +
                        "        }\n" +
                        "        function POP_SLOW_SCORE() {\n" +
                        "            expression:  safeLog(popShareSlowDecaySignal, myValue)\n" +
                        "        }\n" +
                        "    }\n" +
                        "\n" +
                        "}\n");
        builder.build();
        Search s = builder.getSearch();
        RankProfile profile = rankProfileRegistry.get(s, "test");
        assertEquals("safeLog(popShareSlowDecaySignal,myValue)", profile.getFunctions().get("POP_SLOW_SCORE").function().getBody().getRoot().toString());
        assertEquals("safeLog(popShareSlowDecaySignal,-9.21034037)",
                     profile.compile(new QueryProfileRegistry(), new ImportedMlModels()).getFunctions().get("POP_SLOW_SCORE").function().getBody().getRoot().toString());
    }

    @Test
    public void testConstantDivisorInFunction() throws ParseException {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        SearchBuilder builder = new SearchBuilder(rankProfileRegistry);
        builder.importString(
                "search test {\n" +
                        "    document test { \n" +
                        "    }\n" +
                        "    \n" +
                        "    rank-profile test {\n" +
                        "        function rank_default(){\n" +
                        "            expression: k1 + (k2 + k3) / 100000000.0\n\n" +
                        "        }\n" +
                        "    }\n" +
                        "\n" +
                        "}\n");
        builder.build();
        Search s = builder.getSearch();
        RankProfile profile = rankProfileRegistry.get(s, "test");
        assertEquals("k1 + (k2 + k3) / 100000000.0",
                     profile.compile(new QueryProfileRegistry(), new ImportedMlModels()).getFunctions().get("rank_default").function().getBody().getRoot().toString());
    }

    @Test
    public void test3() throws ParseException {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        SearchBuilder builder = new SearchBuilder(rankProfileRegistry);
        builder.importString(
                "search test {\n" +
                        "    document test { \n" +
                        "        field rating_yelp type int {" +
                        "          indexing: attribute" +
                        "        }" +
                        "    }\n" +
                        "    \n" +
                        "    rank-profile test {\n" +
                        "        function rank_default(){\n" +
                        "            expression: 0.5+50*(attribute(rating_yelp)-3)\n\n" +
                        "        }\n" +
                        "    }\n" +
                        "\n" +
                        "}\n");
        builder.build();
        Search s = builder.getSearch();
        RankProfile profile = rankProfileRegistry.get(s, "test");
        assertEquals("0.5 + 50 * (attribute(rating_yelp) - 3)",
                     profile.compile(new QueryProfileRegistry(), new ImportedMlModels()).getFunctions().get("rank_default").function().getBody().getRoot().toString());
    }

}