aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/Index.java
blob: 286c1108135305f1f9110553277e6ac4e8f8fe47 (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 Vespa.ai. 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.BooleanIndexDefinition;
import com.yahoo.schema.document.HnswIndexParams;
import com.yahoo.schema.document.RankType;
import com.yahoo.schema.document.Stemming;

import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

/**
 * An index definition in a schema.
 * Two indices are equal if they have the same name and the same settings, except
 * alias settings (which are excluded).
 *
 * @author bratseth
 */
public class Index implements Cloneable, Serializable {

    public enum Type {

        VESPA("vespa");
        private final String name;
        Type(String name) { this.name = name; }
        public String getName() { return name; }

    }

    // Please see hashCode, equals and copy when adding attributes to this

    /** The search definition-unique name of this index */
    private String name;

    /** The rank type of this index */
    private RankType rankType = null;

   /** Whether this index supports prefix search */
    private boolean prefix;

    /** The list of aliases (Strings) to this index name */
    private Set<String> aliases = new java.util.LinkedHashSet<>(1);

    /**
     * The stemming setting of this field, or null to use the default.
     * Default is determined by the owning search definition.
     */
    private Stemming stemming = null;

    private Type type = Type.VESPA;

    /** The boolean index definition, if set */
    private BooleanIndexDefinition boolIndex;

    private Optional<HnswIndexParams> hnswIndexParams = Optional.empty();

    /** Whether the posting lists of this index field should have interleaved features (num occs, field length) in document id stream. */
    private boolean interleavedFeatures = false;

    public Index(String name) {
        this(name, false);
    }

    public Index(String name, boolean prefix) {
        this.name = name;
        this.prefix = prefix;
    }

    public void setName(String name) { this.name = name; }

    public String getName() { return name; }

    /** Sets the rank type of this field */
    public void setRankType(RankType rankType) { this.rankType = rankType; }

    /** Returns the rank type of this field, or null if nothing is set */
    public RankType getRankType() { return rankType; }

    /** Return the stemming setting of this index, may be null */
    public Stemming getStemming() { return stemming; }

    /**
     * Whether this field should be stemmed in this search definition,
     * this is never null
     */
    public Stemming getStemming(Schema schema) {
        if (stemming != null)
            return stemming;
        else
            return schema.getStemming();
    }

    /**
     * Sets how this field should be stemmed, or set to null to use the default.
     */
    public void setStemming(Stemming stemming) { this.stemming = stemming; }

    /** Returns whether this index supports prefix search, default is false */
    public boolean isPrefix() { return prefix; }

    /** Sets whether this index supports prefix search */
    public void setPrefix(boolean prefix) { this.prefix=prefix; }

    /** Adds an alias to this index name */
    public void addAlias(String alias) {
        aliases.add(alias);
    }

    /** Returns a read-only iterator of the aliases (Strings) to this index name */
    public Iterator<String> aliasIterator() {
        return Collections.unmodifiableSet(aliases).iterator();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Index index = (Index) o;
        return prefix == index.prefix &&
               interleavedFeatures == index.interleavedFeatures &&
               Objects.equals(name, index.name) &&
               rankType == index.rankType &&
               Objects.equals(aliases, index.aliases) &&
               stemming == index.stemming &&
               type == index.type &&
               Objects.equals(boolIndex, index.boolIndex) &&
               Objects.equals(hnswIndexParams, index.hnswIndexParams);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, rankType, prefix, aliases, stemming, type, boolIndex, hnswIndexParams, interleavedFeatures);
    }

    public String toString() {
        String rankTypeName = rankType == null ? "(none)" : rankType.name();
        return "index '" + name +
               "' [ranktype: " + rankTypeName +
               ", prefix: " + prefix + "]";
    }

    /** Makes a deep copy of this index */
    @Override
    public Object clone() {
        try {
            Index copy = (Index)super.clone();
            copy.aliases = new LinkedHashSet<>(this.aliases);
            return copy;
        }
        catch (CloneNotSupportedException e) {
            throw new RuntimeException("Programming error",e);
        }
    }

    public Index copy() {
        return (Index)clone();
    }

    /** Returns the index engine type */
    public Type getType() {
        return type;
    }

    /** Sets the index engine type */
    public void setType(Type type) {
        this.type = type;
    }

    /** Returns the boolean index definition */
    public BooleanIndexDefinition getBooleanIndexDefiniton() {
        return boolIndex;
    }

    /** Sets the boolean index definition */
    public void setBooleanIndexDefiniton(BooleanIndexDefinition def) {
        boolIndex = def;
    }

    public Optional<HnswIndexParams> getHnswIndexParams() {
        return hnswIndexParams;
    }

    public void setHnswIndexParams(HnswIndexParams params) {
        hnswIndexParams = Optional.of(params);
    }

    public void setInterleavedFeatures(boolean value) {
        interleavedFeatures = value;
    }

    public boolean useInterleavedFeatures() {
        return interleavedFeatures;
    }

}