aboutsummaryrefslogtreecommitdiffstats
path: root/model-evaluation/src/test/java/ai/vespa/models/evaluation/RankProfileImportingTest.java
blob: 3aeb0038a12cc0b3ce77f319f40ca3c58c883a80 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.models.evaluation;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * @author bratseth
 */
public class RankProfileImportingTest {

    @Test
    public void testImportingRankExpressions() {
        ModelTester tester = new ModelTester("src/test/resources/config/rankexpression/");

        assertEquals(18, tester.models().size());

        Model macros = tester.models().get("macros");
        assertEquals("macros", macros.name());
        assertEquals(4, macros.functions().size());
        tester.assertFunction("fourtimessum", "4 * (var1 + var2)", macros);
        tester.assertFunction("firstphase", "match + fieldMatch(title) + rankingExpression(myfeature)", macros);
        tester.assertFunction("secondphase", "rankingExpression(fourtimessum@5cf279212355b980.67f1e87166cfef86)", macros);
        tester.assertFunction("myfeature",
                              "70 * fieldMatch(title).completeness * pow(0 - fieldMatch(title).earliness,2) + " +
                              "30 * pow(0 - fieldMatch(description).earliness,2)",
                              macros);
        assertEquals(4, macros.referencedFunctions().size());
        tester.assertBoundFunction("rankingExpression(fourtimessum@5cf279212355b980.67f1e87166cfef86)",
                                   "4 * (match + rankBoost)", macros);
    }

    @Test
    public void testImportingSimpleGlobalPhase() {
        ModelTester tester = new ModelTester("src/test/resources/config/dotproduct/");
        assertEquals(1, tester.models().size());
        Model m = tester.models().get("default");
        assertEquals("default", m.name());
        assertEquals(1, m.functions().size());
        tester.assertFunction("globalphase", "reduce(attribute(aa) * query(zz), sum)", m);
        var f = m.functions().get(0);
        assertEquals("globalphase", f.getName());
        assertEquals(2, f.arguments().size());
        assertEquals("tensor(d0[3])", f.getArgumentType("query(zz)").toString());
        assertEquals("tensor(d0[3])", f.getArgumentType("attribute(aa)").toString());
        var rt = f.returnType();
        assertEquals(true, rt.isPresent());
        assertEquals("tensor()", rt.get().toString());
    }

    @Test
    public void testImportingExpressionsAsArguments() {
        ModelTester tester = new ModelTester("src/test/resources/config/expressions-as-arguments/");
        assertEquals(3, tester.models().size());
    }

    @Test
    public void testImportingWithMacros() {
        ModelTester tester = new ModelTester("src/test/resources/config/ranking-macros/");
        assertEquals(5, tester.models().size());
    }

    @Test
    public void testImportingAdvancedGlobalPhase() {
        ModelTester tester = new ModelTester("src/test/resources/config/advanced-global-phase/");
        assertEquals(6, tester.models().size());
        Model m = tester.models().get("global_phase");
        assertEquals("global_phase", m.name());
        var func = m.function("globalphase");
        assertEquals("globalphase", func.getName());
        var args = func.argumentTypes();
        assertEquals(2, args.size());
        assertEquals("tensor(d0[2])", args.get("attribute(doc_vec)").toString());
        assertEquals("tensor(d0[2])", args.get("query(query_vec)").toString());
    }

    @Test
    public void testImportingLayeredGlobalPhase() {
        ModelTester tester = new ModelTester("src/test/resources/config/layered/");
        assertEquals(1, tester.models().size());
        Model m = tester.models().get("layered");
        assertEquals("layered", m.name());
        var func = m.function("globalphase");
        assertEquals("globalphase", func.getName());
        var args = func.argumentTypes();
        assertEquals(1, args.size());
        assertEquals("tensor(m{},v[3])", args.get("rankingExpression(mymul)").toString());
    }
}