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

import com.yahoo.config.model.application.provider.MockFileRegistry;
import com.yahoo.config.model.deploy.TestProperties;
import com.yahoo.search.query.profile.QueryProfileRegistry;
import com.yahoo.schema.derived.AttributeFields;
import com.yahoo.schema.derived.RawRankProfile;
import com.yahoo.schema.parser.ParseException;
import ai.vespa.rankingexpression.importer.configmodelview.ImportedMlModels;
import org.junit.jupiter.api.Test;

import java.util.List;

import static com.yahoo.config.model.test.TestUtil.joinLines;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author bratseth
 */
public class RankPropertiesTestCase extends AbstractSchemaTestCase {

    @Test
    void testRankPropertyInheritance() throws ParseException {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        ApplicationBuilder builder = new ApplicationBuilder(rankProfileRegistry);
        builder.addSchema(joinLines(
                "search test {",
                "    document test {",
                "        field a type string { ",
                "            indexing: index ",
                "        }",
                "    }",
                "    rank-profile parent {",
                "        first-phase {",
                "            expression: a",
                "        }",
                "        rank-properties {",
                "            query(a): 1500 ",
                "        }",
                "    }",
                "    rank-profile child inherits parent {",
                "        first-phase {",
                "            expression: a",
                "        }",
                "        rank-properties {",
                "            query(a): 2000 ",
                "        }",
                "    }",
                "}"));
        builder.build(true);
        Schema schema = builder.getSchema();
        AttributeFields attributeFields = new AttributeFields(schema);

        {
            // Check declared model
            RankProfile parent = rankProfileRegistry.get(schema, "parent");
            assertEquals("query(a) = 1500", parent.getRankProperties().get(0).toString());

            // Check derived model
            RawRankProfile rawParent = new RawRankProfile(parent, new LargeRankingExpressions(new MockFileRegistry()), new QueryProfileRegistry(), new ImportedMlModels(), attributeFields, new TestProperties());
            assertEquals("(query(a), 1500)", rawParent.configProperties().get(0).toString());
        }

        {
            // Check declared model
            RankProfile parent = rankProfileRegistry.get(schema, "child");
            assertEquals("query(a) = 2000", parent.getRankProperties().get(0).toString());

            // Check derived model
            RawRankProfile rawChild = new RawRankProfile(rankProfileRegistry.get(schema, "child"),
                    new LargeRankingExpressions(new MockFileRegistry()),
                    new QueryProfileRegistry(),
                    new ImportedMlModels(),
                    attributeFields,
                    new TestProperties());
            assertEquals("(query(a), 2000)", rawChild.configProperties().get(0).toString());
        }
    }

    @Test
    public void testDefaultRankProperties() throws ParseException {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        ApplicationBuilder builder = new ApplicationBuilder(rankProfileRegistry, new QueryProfileRegistry());
        builder.addSchema(joinLines(
                "search test {",
                "    document test {",
                "        field a type string { ",
                "            indexing: index ",
                "        }",
                "    }",
                "    rank-profile a {",
                "        first-phase {",
                "            expression: a",
                "        }",
                "    }",
                "    rank-profile b {",
                "        first-phase {",
                "            expression: a",
                "        }",
                "        rank-properties {",
                "            query(a): 2000 ",
                "        }",
                "    }",
                "}"));
        builder.build(true);
        Schema schema = builder.getSchema();
        List<RankProfile.RankProperty> props = rankProfileRegistry.get(schema, "a").getRankProperties();
        assertTrue(props.isEmpty());

        props = rankProfileRegistry.get(schema, "b").getRankProperties();
        assertEquals(1, props.size());
        assertEquals(new RankProfile.RankProperty("query(a)","2000"), props.get(0));
    }

    @Test
    void testRankProfileMutate() throws ParseException {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        ApplicationBuilder builder = new ApplicationBuilder(rankProfileRegistry);
        builder.addSchema(joinLines(
                "search test {",
                "    document test {",
                "        field a type int { ",
                "            indexing: attribute ",
                "        }",
                "    }",
                "    field synthetic_attribute_a type int {",
                "        indexing: attribute",
                "        attribute: mutable",
                "    }",
                "    field synthetic_attribute_b type double {",
                "        indexing: attribute",
                "        attribute: mutable",
                "    }",
                "    field synthetic_attribute_c type long {",
                "        indexing: attribute",
                "        attribute: mutable",
                "    }",
                "    rank-profile a {",
                "        mutate {",
                "          on-match {",
                "            synthetic_attribute_a += 7",
                "          }",
                "          on-first-phase {",
                "            synthetic_attribute_b +=1",
                "          }",
                "          on-second-phase {",
                "            synthetic_attribute_b = 1.01",
                "          }",
                "          on-summary {",
                "            synthetic_attribute_c -= 1",
                "          }",
                "        }",
                "        first-phase {",
                "            expression: a",
                "        }",
                "    }",
                "    rank-profile b {",
                "        first-phase {",
                "            expression: a",
                "        }",
                "        second-phase {",
                "            expression: a",
                "        }",
                "    }",
                "}"));
        builder.build(true);
        Schema schema = builder.getSchema();
        RankProfile a = rankProfileRegistry.get(schema, "a");
        List<RankProfile.MutateOperation> operations = a.getMutateOperations();
        assertEquals(4, operations.size());
        assertEquals(RankProfile.MutateOperation.Phase.on_match, operations.get(0).phase);
        assertEquals("synthetic_attribute_a", operations.get(0).attribute);
        assertEquals("+=7", operations.get(0).operation);
        assertEquals(RankProfile.MutateOperation.Phase.on_first_phase, operations.get(1).phase);
        assertEquals("synthetic_attribute_b", operations.get(1).attribute);
        assertEquals("+=1", operations.get(1).operation);
        assertEquals(RankProfile.MutateOperation.Phase.on_second_phase, operations.get(2).phase);
        assertEquals("synthetic_attribute_b", operations.get(2).attribute);
        assertEquals("=1.01", operations.get(2).operation);
        assertEquals(RankProfile.MutateOperation.Phase.on_summary, operations.get(3).phase);
        assertEquals("synthetic_attribute_c", operations.get(3).attribute);
        assertEquals("-=1", operations.get(3).operation);

        AttributeFields attributeFields = new AttributeFields(schema);
        RawRankProfile raw = new RawRankProfile(a, new LargeRankingExpressions(new MockFileRegistry()), new QueryProfileRegistry(), new ImportedMlModels(), attributeFields, new TestProperties());
        assertEquals(9, raw.configProperties().size());
        assertEquals("(vespa.mutate.on_match.attribute, synthetic_attribute_a)", raw.configProperties().get(0).toString());
        assertEquals("(vespa.mutate.on_match.operation, +=7)", raw.configProperties().get(1).toString());
        assertEquals("(vespa.mutate.on_first_phase.attribute, synthetic_attribute_b)", raw.configProperties().get(2).toString());
        assertEquals("(vespa.mutate.on_first_phase.operation, +=1)", raw.configProperties().get(3).toString());
        assertEquals("(vespa.mutate.on_second_phase.attribute, synthetic_attribute_b)", raw.configProperties().get(4).toString());
        assertEquals("(vespa.mutate.on_second_phase.operation, =1.01)", raw.configProperties().get(5).toString());
        assertEquals("(vespa.mutate.on_summary.attribute, synthetic_attribute_c)", raw.configProperties().get(6).toString());
        assertEquals("(vespa.mutate.on_summary.operation, -=1)", raw.configProperties().get(7).toString());
        assertEquals("(vespa.rank.firstphase, a)", raw.configProperties().get(8).toString());
    }

}