aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/DefaultRankProfile.java
blob: 9ab03b8c4a1b871f283ea8ea98a0aca602b36426 (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
// 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.schema.document.ImmutableSDField;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * The rank profile containing default settings. This is derived from the fields
 * whenever this is accessed.
 *
 * @author bratseth
 */
public class DefaultRankProfile extends RankProfile {

    /**
     * Creates a new rank profile
     *
     * @param rankProfileRegistry the {@link com.yahoo.schema.RankProfileRegistry}
     *                            to use for storing and looking up rank profiles
     */
    public DefaultRankProfile(Schema schema, RankProfileRegistry rankProfileRegistry) {
        super("default", schema, rankProfileRegistry);
    }

    /** Ignore self inheriting of default as some applications may use that for historical reasons. */
    public void inherit(String inheritedName) {
        if (inheritedName.equals("default")) return;
        super.inherit(inheritedName);
    }

    @Override
    public RankSetting getRankSetting(String fieldOrIndex, RankSetting.Type type) {
        RankSetting setting = super.getRankSetting(fieldOrIndex, type);
        if (setting != null) return setting;

        ImmutableSDField field = schema().getConcreteField(fieldOrIndex);
        if (field != null) {
            setting = toRankSetting(field, type);
            if (setting != null)
                return setting;
        }

        Index index = schema().getIndex(fieldOrIndex);
        if (index != null) {
            setting = toRankSetting(index, type);
            if (setting != null)
                return setting;
        }

        return null;
    }

    private RankSetting toRankSetting(ImmutableSDField field, RankSetting.Type type) {
        if (type.equals(RankSetting.Type.WEIGHT) && field.getWeight() > 0 && field.getWeight() != 100)
            return new RankSetting(field.getName(), type, field.getWeight());
        if (type.equals(RankSetting.Type.RANKTYPE))
            return new RankSetting(field.getName(), type, field.getRankType());
        if (type.equals(RankSetting.Type.LITERALBOOST) && field.getLiteralBoost() > 0)
            return new RankSetting(field.getName(), type, field.getLiteralBoost());

        // Index level setting really
        if (type.equals(RankSetting.Type.PREFERBITVECTOR) && field.getRanking().isFilter()) {
            return new RankSetting(field.getName(), type, true);
        }

        return null;
    }

    private RankSetting toRankSetting(Index index, RankSetting.Type type) {
        /* TODO: Add support for indexes by adding a ranking object to the index
        if (type.equals(RankSetting.Type.PREFERBITVECTOR) && index.isPreferBitVector()) {
            return new RankSetting(index.getName(), type, new Boolean(true));
        }
        */
        return null;
    }

    /**
     * Returns the names of the fields which have a rank boost setting
     * explicitly in this profile or in fields
     */
    @Override
    public Set<RankSetting> rankSettings() {
        Set<RankSetting> settings = new LinkedHashSet<>(20);
        settings.addAll(this.rankSettings);
        for (ImmutableSDField field : schema().allConcreteFields() ) {
            addSetting(field, RankSetting.Type.WEIGHT, settings);
            addSetting(field, RankSetting.Type.RANKTYPE, settings);
            addSetting(field, RankSetting.Type.LITERALBOOST, settings);
            addSetting(field, RankSetting.Type.PREFERBITVECTOR, settings);
        }

        // For settings that really pertains to indexes do the explicit indexes too
        for (Index index : schema().getExplicitIndices()) {
            addSetting(index, RankSetting.Type.PREFERBITVECTOR, settings);
        }
        return settings;
    }

    private void addSetting(ImmutableSDField field, RankSetting.Type type, Set<RankSetting> settings) {
        if (type.isIndexLevel()) {
            addIndexSettings(field, type, settings);
        }
        else {
            RankSetting setting = toRankSetting(field, type);
            if (setting == null) return;
            settings.add(setting);
        }
    }

    private void addIndexSettings(ImmutableSDField field, RankSetting.Type type, Set<RankSetting> settings) {
        String indexName = field.getName();

        // TODO: Make a ranking object in the index override the field level ranking object
        if (type.equals(RankSetting.Type.PREFERBITVECTOR) && field.getRanking().isFilter()) {
            settings.add(new RankSetting(indexName, type, true));
        }
    }

    private void addSetting(Index index, RankSetting.Type type, Set<RankSetting> settings) {
        RankSetting setting = toRankSetting(index, type);
        if (setting == null) return;
        settings.add(setting);
    }

}