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

import com.yahoo.schema.document.RankType;

import java.util.Collections;
import java.util.Map;

/**
 * A set of rank type definitions used for native rank features.
 *
 * @author geirst
 */
public class NativeRankTypeDefinitionSet {

    /** The name of this rank definition set */
    private String name;

    /** The unmodifiable rank type implementations in this set */
    private final Map<RankType, NativeRankTypeDefinition> typeDefinitions;

    /** Returns the default rank type (about) */
    public static RankType getDefaultRankType() { return RankType.ABOUT; }

    public NativeRankTypeDefinitionSet(String name) {
        this.name = name;

        Map<RankType, NativeRankTypeDefinition> typeDefinitions = new java.util.LinkedHashMap<>();
        typeDefinitions.put(RankType.IDENTITY, createIdentityRankType(RankType.IDENTITY));
        typeDefinitions.put(RankType.ABOUT, createAboutRankType(RankType.ABOUT));
        typeDefinitions.put(RankType.TAGS, createTagsRankType(RankType.TAGS));
        typeDefinitions.put(RankType.EMPTY, createEmptyRankType(RankType.EMPTY));
        this.typeDefinitions = Collections.unmodifiableMap(typeDefinitions);
    }

    private NativeRankTypeDefinition createEmptyRankType(RankType type) {
        NativeRankTypeDefinition rank = new NativeRankTypeDefinition(type);
        rank.addTable(new NativeTable(NativeTable.Type.FIRST_OCCURRENCE, "linear(0,0)"));
        rank.addTable(new NativeTable(NativeTable.Type.OCCURRENCE_COUNT, "linear(0,0)"));
        rank.addTable(new NativeTable(NativeTable.Type.PROXIMITY, "linear(0,0)"));
        rank.addTable(new NativeTable(NativeTable.Type.REVERSE_PROXIMITY, "linear(0,0)"));
        rank.addTable(new NativeTable(NativeTable.Type.WEIGHT, "linear(0,0)"));
        return rank;
    }

    private NativeRankTypeDefinition createAboutRankType(RankType type) {
        NativeRankTypeDefinition rank = new NativeRankTypeDefinition(type);
        rank.addTable(new NativeTable(NativeTable.Type.FIRST_OCCURRENCE, "expdecay(8000,12.50)"));
        rank.addTable(new NativeTable(NativeTable.Type.OCCURRENCE_COUNT, "loggrowth(1500,4000,19)"));
        rank.addTable(new NativeTable(NativeTable.Type.PROXIMITY, "expdecay(500,3)"));
        rank.addTable(new NativeTable(NativeTable.Type.REVERSE_PROXIMITY, "expdecay(400,3)"));
        rank.addTable(new NativeTable(NativeTable.Type.WEIGHT, "linear(1,0)"));
        return rank;
    }

    private NativeRankTypeDefinition createIdentityRankType(RankType type) {
        NativeRankTypeDefinition rank = new NativeRankTypeDefinition(type);
        rank.addTable(new NativeTable(NativeTable.Type.FIRST_OCCURRENCE, "expdecay(100,12.50)"));
        rank.addTable(new NativeTable(NativeTable.Type.OCCURRENCE_COUNT, "loggrowth(1500,4000,19)"));
        rank.addTable(new NativeTable(NativeTable.Type.PROXIMITY, "expdecay(5000,3)"));
        rank.addTable(new NativeTable(NativeTable.Type.REVERSE_PROXIMITY, "expdecay(3000,3)"));
        rank.addTable(new NativeTable(NativeTable.Type.WEIGHT, "linear(1,0)"));
        return rank;
    }

    private NativeRankTypeDefinition createTagsRankType(RankType type) {
        NativeRankTypeDefinition rank = new NativeRankTypeDefinition(type);
        rank.addTable(new NativeTable(NativeTable.Type.FIRST_OCCURRENCE, "expdecay(8000,12.50)"));
        rank.addTable(new NativeTable(NativeTable.Type.OCCURRENCE_COUNT, "loggrowth(1500,4000,19)"));
        rank.addTable(new NativeTable(NativeTable.Type.PROXIMITY, "expdecay(500,3)"));
        rank.addTable(new NativeTable(NativeTable.Type.REVERSE_PROXIMITY, "expdecay(400,3)"));
        rank.addTable(new NativeTable(NativeTable.Type.WEIGHT, "loggrowth(38,50,1)"));
        return rank;
    }

    /**
     * Returns a rank type definition if given an existing rank type name,
     * or null if given a rank type which has no native implementation (meaning somebody forgot to add it),
    */
    public NativeRankTypeDefinition getRankTypeDefinition(RankType type) {
        if (type == RankType.DEFAULT)
            type = getDefaultRankType();
        return typeDefinitions.get(type);
    }

    /** Returns an unmodifiable map of the type definitions in this */
    public Map<RankType, NativeRankTypeDefinition> types() { return typeDefinitions; }

    public String toString() {
        return "native rank type definitions " + name;
    }

}