aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/schema/RankProfile.java
blob: a5b8d328a7a57e0348ab6ec623b69b69ac28d5b8 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.schema;

import com.yahoo.api.annotations.Beta;
import com.yahoo.tensor.TensorType;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Information about a rank profile
 *
 * @author bratseth
 */
@Beta
public class RankProfile {

    public record InputType(TensorType tensorType, boolean declaredString) {
        public String toString() {
            return declaredString ? "string" : tensorType.toString();
        }
        public static InputType fromSpec(String spec) {
            if ("string".equals(spec)) {
                return new InputType(TensorType.empty, true);
            }
            if ("double".equals(spec)) {
                return new InputType(TensorType.empty, false);
            }
            return new InputType(TensorType.fromSpec(spec), false);
        }
    }

    private final String name;
    private final boolean hasSummaryFeatures;
    private final boolean hasRankFeatures;
    private final Map<String, InputType> inputs;

    // Assigned when this is added to a schema
    private Schema schema = null;

    private RankProfile(Builder builder) {
        this.name = builder.name;
        this.hasSummaryFeatures = builder.hasSummaryFeatures;
        this.hasRankFeatures = builder.hasRankFeatures;
        this.inputs = Collections.unmodifiableMap(builder.inputs);
    }

    public String name() { return name; }

    /** Returns the schema owning this, or null if this is not added to a schema */
    public Schema schema() { return schema; }

    void setSchema(Schema schema) {
        if ( this.schema != null)
            throw new IllegalStateException("Cannot add rank profile '" + name + "' to schema '" + schema.name() +
                                            "' as it is already added to schema '" + this.schema.name() + "'");
        this.schema = schema;
    }

    /** Returns true if this rank profile has summary features. */
    public boolean hasSummaryFeatures() { return hasSummaryFeatures; }

    /** Returns true if this rank profile has rank features. */
    public boolean hasRankFeatures() { return hasRankFeatures; }

    /** Returns the inputs explicitly declared in this rank profile. */
    public Map<String, InputType> inputs() { return inputs; }

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if ( ! (o instanceof RankProfile other)) return false;
        if ( ! other.name.equals(this.name)) return false;
        if ( other.hasSummaryFeatures != this.hasSummaryFeatures) return false;
        if ( other.hasRankFeatures != this.hasRankFeatures) return false;
        if ( ! other.inputs.equals(this.inputs)) return false;
        return true;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, hasSummaryFeatures, hasRankFeatures, inputs);
    }

    @Override
    public String toString() {
        return "rank profile '" + name + "'" + (schema == null ? "" : " in " + schema);
    }

    public static class Builder {

        private final String name;
        private boolean hasSummaryFeatures = true;
        private boolean hasRankFeatures = true;
        private final Map<String, InputType> inputs = new LinkedHashMap<>();

        public Builder(String name) {
            this.name = Objects.requireNonNull(name);
        }

        public Builder setHasSummaryFeatures(boolean hasSummaryFeatures) {
            this.hasSummaryFeatures = hasSummaryFeatures;
            return this;
        }

        public Builder setHasRankFeatures(boolean hasRankFeatures) {
            this.hasRankFeatures = hasRankFeatures;
            return this;
        }

        public Builder addInput(String name, InputType type) {
            inputs.put(name, type);
            return this;
        }

        public RankProfile build() {
            return new RankProfile(this);
        }

    }

}