aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/SearchDefinition.java
blob: 47becde7b19f8ebe3de3ecd403e7918226018b72 (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 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude;

import com.yahoo.prelude.Index.Attribute;

import java.util.HashMap;
import java.util.Map;

import static com.yahoo.text.Lowercase.toLowerCase;

/**
 * An object for storing information about search definitions in the centralized
 * directory services.
 *
 * @author Steinar Knutsen
 */
// TODO: Make freezable!
public class SearchDefinition {

    private String name;

    /** A map of all indices in this search definition, indexed by name */
    private Map<String, Index> indices = new HashMap<>();

    /*
     * A map of all indices in this search definition, indexed by lower cased
     * name.
     */
    private Map<String, Index> lowerCase = new HashMap<>();

    private String defaultPosition;

    public SearchDefinition(String name) {
        this.name = name;
    }

    public String getName() { return name; }

    public String getDefaultPosition() {
        return defaultPosition;
    }

    public void addIndex(Index index) {
        indices.put(index.getName(), index);
        lowerCase.put(toLowerCase(index.getName()), index);
        if (index.isDefaultPosition()) {
            defaultPosition = index.getName();
        }
    }

    public void addAlias(String alias, String indexName) {
        Index old;

        if ((old = indices.get(alias)) != null) {
            if (old.getName().equals(indexName)) {
                return;
            } else {
                throw new IllegalArgumentException("Tried adding the alias \""
                        + alias + "\" for the index name \"" + indexName
                        + "\" when the name \"" + alias
                        + "\" already maps to \"" + old.getName() + "\".");
            }
        }
        Index index = indices.get(indexName);
        if (index == null) {
            throw new IllegalArgumentException("Failed adding alias \"" + alias
                    + "\" for the index name \"" + indexName
                    + "\" as there is no index with that name available.");
        }
        indices.put(alias, index);
        String lca = toLowerCase(alias);
        if (lowerCase.get(lca) == null) {
            lowerCase.put(lca, index);
        }
    }

    public Index getIndex(String name) {
        return indices.get(name);
    }

    public Index getIndexByLowerCase(String name) {
        return lowerCase.get(name);
    }

    /** Returns the indices of this as a map */
    public Map<String, Index> indices() {
        return indices;
    }

    public Index getOrCreateIndex(String name) {
        Index idx = getIndex(name);
        if (idx != null) {
            return idx;
        }
        idx = new Index(name);
        addIndex(idx);
        return idx;
    }

    public void addCommand(String indexName, String commandString) {
        Index index = getOrCreateIndex(indexName);
        index.addCommand(commandString);
        if (index.isDefaultPosition()) {
            defaultPosition = index.getName();
        }
    }

    public void fillMatchGroups() {
        for (Index i : indices.values()) {
            Attribute[] matchGroup = i.getMatchGroup();
            if (matchGroup == null) {
                continue;
            }
            for (Attribute a : matchGroup) {
                Index m = getIndex(a.name);
                if (m != null) {
                    a.setTokenizedContent(!m.isAttribute());
                }
            }
        }
    }



}