summaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionWithTensorTestCase.java
blob: 595644b21ac0cfd8a96ebf5d72ec1a32913eddd3 (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
// 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.searchdefinition.RankProfile;
import com.yahoo.searchdefinition.RankProfileRegistry;
import com.yahoo.searchdefinition.Search;
import com.yahoo.searchdefinition.SearchBuilder;
import com.yahoo.searchdefinition.parser.ParseException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.List;

import static org.junit.Assert.assertEquals;

/**
 * @author geirst
 */
public class RankingExpressionWithTensorTestCase {

    private static class SearchFixture {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        Search search;
        SearchFixture(String rankProfiles) throws ParseException {
            SearchBuilder builder = new SearchBuilder(rankProfileRegistry);
            String sdContent = "search test {\n" +
                "  document test {\n" +
                "  }\n" +
                rankProfiles +
                "\n" +
                "}";
            builder.importString(sdContent);
            builder.build();
            search = builder.getSearch();
        }
        public void assertFirstPhaseExpression(String expExpression, String rankProfile) {
            assertEquals(expExpression, getRankProfile(rankProfile).getFirstPhaseRanking().getRoot().toString());
        }
        public void assertSecondPhaseExpression(String expExpression, String rankProfile) {
            assertEquals(expExpression, getRankProfile(rankProfile).getSecondPhaseRanking().getRoot().toString());
        }
        public void assertRankProperty(String expValue, String name, String rankProfile) {
            List<RankProfile.RankProperty> rankPropertyList = getRankProfile(rankProfile).getRankPropertyMap().get(name);
            assertEquals(1, rankPropertyList.size());
            assertEquals(expValue, rankPropertyList.get(0).getValue());
        }
        public void assertMacro(String expExpression, String macroName, String rankProfile) {
            assertEquals(expExpression, getRankProfile(rankProfile).getMacros().get(macroName).getRankingExpression().getRoot().toString());
        }
        private RankProfile getRankProfile(String rankProfile) {
            return rankProfileRegistry.getRankProfile(search, rankProfile).compile();
        }
    }

    @Test
    public void requireThatSingleLineConstantTensorAndTypeCanBeParsed() throws ParseException {
        SearchFixture f = new SearchFixture(
                "  rank-profile my_profile {\n" +
                "    first-phase {\n" +
                "      expression: sum(my_tensor)\n" +
                "    }\n" +
                "    constants {\n" +
                "      my_tensor {\n" +
                "        value: { {x:1,y:2}:1, {x:2,y:1}:2 }\n" +
                "        type: tensor(x{},y{})\n" +
                "      }\n" +
                "    }\n" +
                "  }");
        f.assertFirstPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile");
        f.assertRankProperty("{{x:1,y:2}:1.0,{x:2,y:1}:2.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{},y{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    public void requireThatMultiLineConstantTensorAndTypeCanBeParsed() throws ParseException {
        SearchFixture f = new SearchFixture(
                "  rank-profile my_profile {\n" +
                "    first-phase {\n" +
                "      expression: sum(my_tensor)\n" +
                "    }\n" +
                "    constants {\n" +
                "      my_tensor {\n" +
                "        value {\n" +
                "          { {x:1,y:2}:1,\n" +
                "            {x:2,y:1}:2 }\n" +
                "        }\n" +
                "        type: tensor(x{},y{})\n" +
                "      }\n" +
                "    }\n" +
                "  }");
        f.assertFirstPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile");
        f.assertRankProperty("{{x:1,y:2}:1.0,{x:2,y:1}:2.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{},y{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    public void requireThatConstantTensorsCanBeUsedInSecondPhaseExpression() throws ParseException {
        SearchFixture f = new SearchFixture(
                "  rank-profile my_profile {\n" +
                "    second-phase {\n" +
                "      expression: sum(my_tensor)\n" +
                "    }\n" +
                "    constants {\n" +
                "      my_tensor {\n" +
                "        value: { {x:1}:1 }\n" +
                "      }\n" +
                "    }\n" +
                "  }");
        f.assertSecondPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile");
        f.assertRankProperty("{{x:1}:1.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    public void requireThatConstantTensorsCanBeUsedInInheritedRankProfile() throws ParseException {
        SearchFixture f = new SearchFixture(
                "  rank-profile parent {\n" +
                "    constants {\n" +
                "      my_tensor {\n" +
                "        value: { {x:1}:1 }\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "  rank-profile my_profile inherits parent {\n" +
                "    first-phase {\n" +
                "      expression: sum(my_tensor)\n" +
                "    }\n" +
                "  }");
        f.assertFirstPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile");
        f.assertRankProperty("{{x:1}:1.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    public void requireThatConstantTensorsCanBeUsedInMacro() throws ParseException {
        SearchFixture f = new SearchFixture(
                "  rank-profile my_profile {\n" +
                "    macro my_macro() {\n" +
                "      expression: sum(my_tensor)\n" +
                "    }\n" +
                "    first-phase {\n" +
                "      expression: 5.0 + my_macro\n" +
                "    }\n" +
                "    constants {\n" +
                "      my_tensor {\n" +
                "        value: { {x:1}:1 }\n" +
                "      }\n" +
                "    }\n" +
                "  }");
        f.assertFirstPhaseExpression("5.0 + my_macro", "my_profile");
        f.assertMacro("reduce(constant(my_tensor), sum)", "my_macro", "my_profile");
        f.assertRankProperty("{{x:1}:1.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    public void requireThatCombinationOfConstantTensorsAndConstantValuesCanBeUsed() throws ParseException {
        SearchFixture f = new SearchFixture(
                "  rank-profile my_profile {\n" +
                "    first-phase {\n" +
                "      expression: my_number_1 + sum(my_tensor) + my_number_2\n" +
                "    }\n" +
                "    constants {\n" +
                "      my_number_1: 3.0\n" +
                "      my_tensor {\n" +
                "        value: { {x:1}:1 }\n" +
                "      }\n" +
                "      my_number_2: 5.0\n" +
                "    }\n" +
                "  }");
        f.assertFirstPhaseExpression("3.0 + reduce(constant(my_tensor), sum) + 5.0", "my_profile");
        f.assertRankProperty("{{x:1}:1.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{})", "constant(my_tensor).type", "my_profile");
    }

    @Rule
    public ExpectedException exception = ExpectedException.none();

    @Test
    public void requireThatInvalidTensorTypeSpecThrowsException() throws ParseException {
        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("For constant tensor 'my_tensor' in rank profile 'my_profile': Illegal tensor type spec: Failed parsing element 'x' in type spec 'tensor(x)'");
        new SearchFixture(
                "  rank-profile my_profile {\n" +
                "    constants {\n" +
                "      my_tensor {\n" +
                "        value: { {x:1}:1 }\n" +
                "        type: tensor(x)\n" +
                "      }\n" +
                "    }\n" +
                "  }");
    }

}