aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/fieldoperation/IndexOperation.java
blob: 459bb247e5f192b207f9ae86dc6a0491288970fd (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.fieldoperation;

import com.yahoo.searchdefinition.Index;
import com.yahoo.searchdefinition.Index.Type;
import com.yahoo.searchdefinition.document.BooleanIndexDefinition;
import com.yahoo.searchdefinition.document.SDField;
import com.yahoo.searchdefinition.document.Stemming;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;

/**
 * @author Einar M R Rosenvinge
 */
public class IndexOperation implements FieldOperation {

    private String indexName;
    private Optional<Boolean> prefix = Optional.empty();
    private List<String> aliases = new LinkedList<>();
    private Optional<String> stemming = Optional.empty();
    private Optional<Type> type = Optional.empty();

    private OptionalInt arity = OptionalInt.empty(); // For predicate data type
    private OptionalLong lowerBound = OptionalLong.empty();
    private OptionalLong upperBound = OptionalLong.empty();
    private OptionalDouble densePostingListThreshold = OptionalDouble.empty();
    // TODO: Remove when experimental posting list format is made default
    private Optional<Boolean> experimentalPostingListFormat = Optional.empty();

    public String getIndexName() {
        return indexName;
    }

    public void setIndexName(String indexName) {
        this.indexName = indexName;
    }

    public boolean getPrefix() {
        return prefix.get();
    }

    public void setPrefix(Boolean prefix) {
        this.prefix = Optional.of(prefix);
    }

    public void addAlias(String alias) {
        aliases.add(alias);
    }

    public String getStemming() {
        return stemming.get();
    }

    public void setStemming(String stemming) {
        this.stemming = Optional.of(stemming);
    }

    public void apply(SDField field) {
        Index index = field.getIndex(indexName);

        if (index == null) {
            index = new Index(indexName);
            field.addIndex(index);
        }

        applyToIndex(index);
    }

    public void applyToIndex(Index index) {
        if (prefix.isPresent()) {
            index.setPrefix(prefix.get());
        }
        for (String alias : aliases) {
            index.addAlias(alias);
        }
        if (stemming.isPresent()) {
            index.setStemming(Stemming.get(stemming.get()));
        }
        if (type.isPresent()) {
            index.setType(type.get());
        }
        if (arity.isPresent() || lowerBound.isPresent() ||
                upperBound.isPresent() || densePostingListThreshold.isPresent()) {
            index.setBooleanIndexDefiniton(
                    new BooleanIndexDefinition(arity, lowerBound, upperBound, densePostingListThreshold));
        }
        if (experimentalPostingListFormat.isPresent()) {
            index.setExperimentalPostingListFormat(experimentalPostingListFormat.get());
        }
    }

    public Type getType() {
        return type.get();
    }

    public void setType(Type type) {
        this.type = Optional.of(type);
    }

    public void setArity(int arity) {
        this.arity = OptionalInt.of(arity);
    }

    public void setLowerBound(long value) {
        this.lowerBound = OptionalLong.of(value);
    }

    public void setUpperBound(long value) {
        this.upperBound = OptionalLong.of(value);
    }

    public void setDensePostingListThreshold(double densePostingListThreshold) {
        this.densePostingListThreshold = OptionalDouble.of(densePostingListThreshold);
    }
    public void setExperimentalPostingListFormat(boolean value) {
        experimentalPostingListFormat = Optional.of(value);
    }

}