aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/schema/processing/DictionaryTestCase.java
blob: 45a546259ae924cc2c44f1aeba520d6bda907f89 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.schema.processing;

import com.yahoo.config.model.test.TestUtil;
import com.yahoo.schema.Schema;
import com.yahoo.schema.ApplicationBuilder;
import com.yahoo.schema.derived.AttributeFields;
import com.yahoo.schema.document.Case;
import com.yahoo.schema.document.Dictionary;
import com.yahoo.schema.document.ImmutableSDField;
import com.yahoo.schema.parser.ParseException;
import com.yahoo.vespa.config.search.AttributesConfig;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

/**
 * Test configuration of dictionary control.
 *
 * @author baldersheim
 */
public class DictionaryTestCase {
    private static AttributesConfig getConfig(Schema schema) {
        AttributeFields attributes = new AttributeFields(schema);
        AttributesConfig.Builder builder = new AttributesConfig.Builder();
        attributes.getConfig(builder, AttributeFields.FieldSet.ALL, 130000, true);
        return builder.build();
    }
    private Schema createSearch(String def) throws ParseException {
        ApplicationBuilder sb = ApplicationBuilder.createFromString(def);
        return sb.getSchema();
    }
    @Test
    public void testDefaultDictionarySettings() throws ParseException {
        String def = TestUtil.joinLines(
                        "search test {",
                        "    document test {",
                        "        field s1 type string {",
                        "            indexing: attribute | summary",
                        "        }",
                        "        field n1 type int {",
                        "            indexing: summary | attribute",
                        "        }",
                        "    }",
                        "}");
        Schema schema = createSearch(def);
        assertNull(schema.getAttribute("s1").getDictionary());
        assertNull(schema.getAttribute("n1").getDictionary());
        assertEquals(AttributesConfig.Attribute.Dictionary.Type.BTREE,
                getConfig(schema).attribute().get(0).dictionary().type());
        assertEquals(AttributesConfig.Attribute.Dictionary.Type.BTREE,
                getConfig(schema).attribute().get(1).dictionary().type());
        assertEquals(AttributesConfig.Attribute.Dictionary.Match.UNCASED,
                getConfig(schema).attribute().get(0).dictionary().match());
        assertEquals(AttributesConfig.Attribute.Dictionary.Match.UNCASED,
                getConfig(schema).attribute().get(1).dictionary().match());
    }

    Schema verifyDictionaryControl(Dictionary.Type expected, String type, String ... cfg) throws ParseException
    {
        String def = TestUtil.joinLines(
                "schema test {",
                "    document test {",
                "        field n1 type " + type + " {",
                "            indexing: summary | attribute",
                "            attribute:fast-search",
                TestUtil.joinLines(cfg),
                "        }",
                "    }",
                "}");
        Schema schema = createSearch(def);
        AttributesConfig.Attribute.Dictionary.Type.Enum expectedConfig = toCfg(expected);
        assertEquals(expected, schema.getAttribute("n1").getDictionary().getType());
        assertEquals(expectedConfig, getConfig(schema).attribute().get(0).dictionary().type());
        return schema;
    }

    AttributesConfig.Attribute.Dictionary.Type.Enum toCfg(Dictionary.Type v) {
        return (v == Dictionary.Type.HASH)
                ? AttributesConfig.Attribute.Dictionary.Type.Enum.HASH
                : (v == Dictionary.Type.BTREE)
                    ? AttributesConfig.Attribute.Dictionary.Type.Enum.BTREE
                    : AttributesConfig.Attribute.Dictionary.Type.Enum.BTREE_AND_HASH;
    }
    AttributesConfig.Attribute.Dictionary.Match.Enum toCfg(Case v) {
        return (v == Case.CASED)
                ? AttributesConfig.Attribute.Dictionary.Match.Enum.CASED
                : AttributesConfig.Attribute.Dictionary.Match.Enum.UNCASED;
    }

    void verifyStringDictionaryControl(Dictionary.Type expectedType, Case expectedCase, Case matchCasing,
                                       String ... cfg) throws ParseException
    {
        Schema schema = verifyDictionaryControl(expectedType, "string", cfg);
        ImmutableSDField f = schema.getField("n1");
        AttributesConfig.Attribute.Dictionary.Match.Enum expectedCaseCfg = toCfg(expectedCase);
        assertEquals(matchCasing, f.getMatching().getCase());
        assertEquals(expectedCase, schema.getAttribute("n1").getDictionary().getMatch());
        assertEquals(expectedCaseCfg, getConfig(schema).attribute().get(0).dictionary().match());
    }

    @Test
    public void testCasedBtreeSettings() throws ParseException {
        verifyDictionaryControl(Dictionary.Type.BTREE, "int", "dictionary:cased");
    }

    @Test
    public void testNumericBtreeSettings() throws ParseException {
        verifyDictionaryControl(Dictionary.Type.BTREE, "int", "dictionary:btree");
    }
    @Test
    public void testNumericHashSettings() throws ParseException {
        verifyDictionaryControl(Dictionary.Type.HASH, "int", "dictionary:hash");
    }
    @Test
    public void testNumericBtreeAndHashSettings() throws ParseException {
        verifyDictionaryControl(Dictionary.Type.BTREE_AND_HASH, "int", "dictionary:btree", "dictionary:hash");
    }
    @Test
    public void testNumericArrayBtreeAndHashSettings() throws ParseException {
        verifyDictionaryControl(Dictionary.Type.BTREE_AND_HASH, "array<int>", "dictionary:btree", "dictionary:hash");
    }
    @Test
    public void testNumericWSetBtreeAndHashSettings() throws ParseException {
        verifyDictionaryControl(Dictionary.Type.BTREE_AND_HASH, "weightedset<int>", "dictionary:btree", "dictionary:hash");
    }
    @Test
    public void testStringBtreeSettings() throws ParseException {
        verifyStringDictionaryControl(Dictionary.Type.BTREE, Case.UNCASED, Case.UNCASED, "dictionary:btree");
    }
    @Test
    public void testStringBtreeUnCasedSettings() throws ParseException {
        verifyStringDictionaryControl(Dictionary.Type.BTREE, Case.UNCASED, Case.UNCASED, "dictionary { btree\nuncased\n}");
    }
    @Test
    public void testStringBtreeCasedSettings() throws ParseException {
        verifyStringDictionaryControl(Dictionary.Type.BTREE, Case.CASED, Case.CASED, "dictionary { btree\ncased\n}", "match:cased");
    }
    @Test
    public void testStringHashSettings() throws ParseException {
        try {
            verifyStringDictionaryControl(Dictionary.Type.HASH, Case.UNCASED, Case.UNCASED, "dictionary:hash");
        } catch (IllegalArgumentException e) {
            assertEquals("For schema 'test', field 'n1': hash dictionary require cased match", e.getMessage());
        }
    }
    @Test
    public void testStringHashUnCasedSettings() throws ParseException {
        try {
            verifyStringDictionaryControl(Dictionary.Type.HASH, Case.UNCASED, Case.UNCASED, "dictionary { hash\nuncased\n}");
        } catch (IllegalArgumentException e) {
            assertEquals("For schema 'test', field 'n1': hash dictionary require cased match", e.getMessage());
        }
    }
    @Test
    public void testStringHashBothCasedSettings() throws ParseException {
        verifyStringDictionaryControl(Dictionary.Type.HASH, Case.CASED, Case.CASED, "dictionary { hash\ncased\n}", "match:cased");
    }
    @Test
    public void testStringHashCasedSettings() throws ParseException {
        try {
            verifyStringDictionaryControl(Dictionary.Type.HASH, Case.CASED, Case.CASED, "dictionary { hash\ncased\n}");
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("For schema 'test', field 'n1': Dictionary casing 'CASED' does not match field match casing 'UNCASED'", e.getMessage());
        }
    }
    @Test
    public void testStringBtreeHashSettings() throws ParseException {
        verifyStringDictionaryControl(Dictionary.Type.BTREE_AND_HASH, Case.UNCASED, Case.UNCASED, "dictionary{hash\nbtree\n}");
    }
    @Test
    public void testStringBtreeHashUnCasedSettings() throws ParseException {
        verifyStringDictionaryControl(Dictionary.Type.BTREE_AND_HASH, Case.UNCASED, Case.UNCASED, "dictionary { hash\nbtree\nuncased\n}");
    }
    @Test
    public void testStringBtreeHashCasedSettings() throws ParseException {
        try {
            verifyStringDictionaryControl(Dictionary.Type.BTREE_AND_HASH, Case.CASED, Case.CASED, "dictionary { btree\nhash\ncased\n}");
        } catch (IllegalArgumentException e) {
            assertEquals("For schema 'test', field 'n1': Dictionary casing 'CASED' does not match field match casing 'UNCASED'", e.getMessage());
        }
    }
    @Test
    public void testNonNumericFieldsFailsDictionaryControl() throws ParseException {
        String def = TestUtil.joinLines(
                "schema test {",
                "    document test {",
                "        field n1 type bool {",
                "            indexing: summary | attribute",
                "            dictionary:btree",
                "        }",
                "    }",
                "}");
        try {
            ApplicationBuilder sb = ApplicationBuilder.createFromString(def);
            fail("Controlling dictionary for non-numeric fields are not yet supported.");
        } catch (IllegalArgumentException e) {
            assertEquals("For schema 'test', field 'n1': You can only specify 'dictionary:' for numeric or string fields", e.getMessage());
        }
    }
    @Test
    public void testNonFastSearchNumericFieldsFailsDictionaryControl() throws ParseException {
        String def = TestUtil.joinLines(
                "schema test {",
                "    document test {",
                "        field n1 type int {",
                "            indexing: summary | attribute",
                "            dictionary:btree",
                "        }",
                "    }",
                "}");
        try {
            ApplicationBuilder sb = ApplicationBuilder.createFromString(def);
            fail("Controlling dictionary for non-fast-search fields are not allowed.");
        } catch (IllegalArgumentException e) {
            assertEquals("For schema 'test', field 'n1': You must specify 'attribute:fast-search' to allow dictionary control", e.getMessage());
        }
    }

    @Test
    public void testCasingForNonFastSearch() throws ParseException {
        String def = TestUtil.joinLines(
                "schema test {",
                "    document test {",
                "        field s1 type string {",
                "            indexing: attribute | summary",
                "        }",
                "        field s2 type string {",
                "            indexing: attribute | summary",
                "            match:uncased",
                "        }",
                "        field s3 type string {",
                "            indexing: attribute | summary",
                "            match:cased",
                "        }",
                "    }",
                "}");
        Schema schema = createSearch(def);
        assertEquals(Case.UNCASED, schema.getAttribute("s1").getCase());
        assertEquals(Case.UNCASED, schema.getAttribute("s2").getCase());
        assertEquals(Case.CASED, schema.getAttribute("s3").getCase());
        assertEquals(AttributesConfig.Attribute.Match.UNCASED, getConfig(schema).attribute().get(0).match());
        assertEquals(AttributesConfig.Attribute.Match.UNCASED, getConfig(schema).attribute().get(1).match());
        assertEquals(AttributesConfig.Attribute.Match.CASED, getConfig(schema).attribute().get(2).match());
    }
}