summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/search/aggregations/AggregatorParsingTests.java
blob: 4cd69ef604b9397d3a12023ce442773fc9e9598b (plain)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.search.aggregations;

import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.inject.ModulesBuilder;
import org.elasticsearch.common.inject.multibindings.Multibinder;
import org.elasticsearch.common.inject.util.Providers;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsModule;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.env.Environment;
import org.elasticsearch.env.EnvironmentModule;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.query.AbstractQueryTestCase;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.indices.IndicesModule;
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
import org.elasticsearch.script.MockScriptEngine;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptContextRegistry;
import org.elasticsearch.script.ScriptEngineRegistry;
import org.elasticsearch.script.ScriptEngineService;
import org.elasticsearch.script.ScriptModule;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptSettings;
import org.elasticsearch.search.SearchModule;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.IndexSettingsModule;
import org.elasticsearch.test.InternalSettingsPlugin;
import org.elasticsearch.test.VersionUtils;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.threadpool.ThreadPoolModule;
import org.junit.AfterClass;
import org.junit.BeforeClass;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.elasticsearch.cluster.service.ClusterServiceUtils.createClusterService;
import static org.elasticsearch.cluster.service.ClusterServiceUtils.setState;
import static org.hamcrest.Matchers.containsString;

public class AggregatorParsingTests extends ESTestCase {

    private static Injector injector;
    private static Index index;

    private static String[] currentTypes;

    protected static String[] getCurrentTypes() {
        return currentTypes;
    }

    private static NamedWriteableRegistry namedWriteableRegistry;

    protected static AggregatorParsers aggParsers;
    protected static IndicesQueriesRegistry queriesRegistry;
    protected static ParseFieldMatcher parseFieldMatcher;

    /**
     * Setup for the whole base test class.
     */
    @BeforeClass
    public static void init() throws IOException {
        // we have to prefer CURRENT since with the range of versions we support
        // it's rather unlikely to get the current actually.
        Version version = randomBoolean() ? Version.CURRENT
                : VersionUtils.randomVersionBetween(random(), Version.V_2_0_0_beta1, Version.CURRENT);
        Settings settings = Settings.settingsBuilder().put("node.name", AbstractQueryTestCase.class.toString())
                .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
                .put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING.getKey(), false).build();

        namedWriteableRegistry = new NamedWriteableRegistry();
        index = new Index(randomAsciiOfLengthBetween(1, 10), "_na_");
        Settings indexSettings = Settings.settingsBuilder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
        final ThreadPool threadPool = new ThreadPool(settings);
        final ClusterService clusterService = createClusterService(threadPool);
        setState(clusterService, new ClusterState.Builder(clusterService.state()).metaData(new MetaData.Builder()
                .put(new IndexMetaData.Builder(index.getName()).settings(indexSettings).numberOfShards(1).numberOfReplicas(0))));
        SettingsModule settingsModule = new SettingsModule(settings);
        settingsModule.registerSetting(InternalSettingsPlugin.VERSION_CREATED);
        ScriptModule scriptModule = new ScriptModule() {
            @Override
            protected void configure() {
                Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
                        // no file watching, so we don't need a
                        // ResourceWatcherService
                        .put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING.getKey(), false).build();
                MockScriptEngine mockScriptEngine = new MockScriptEngine();
                Multibinder<ScriptEngineService> multibinder = Multibinder.newSetBinder(binder(), ScriptEngineService.class);
                multibinder.addBinding().toInstance(mockScriptEngine);
                Set<ScriptEngineService> engines = new HashSet<>();
                engines.add(mockScriptEngine);
                List<ScriptContext.Plugin> customContexts = new ArrayList<>();
                ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections
                        .singletonList(new ScriptEngineRegistry.ScriptEngineRegistration(MockScriptEngine.class, MockScriptEngine.TYPES)));
                bind(ScriptEngineRegistry.class).toInstance(scriptEngineRegistry);
                ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(customContexts);
                bind(ScriptContextRegistry.class).toInstance(scriptContextRegistry);
                ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
                bind(ScriptSettings.class).toInstance(scriptSettings);
                try {
                    ScriptService scriptService = new ScriptService(settings, new Environment(settings), engines, null,
                            scriptEngineRegistry, scriptContextRegistry, scriptSettings);
                    bind(ScriptService.class).toInstance(scriptService);
                } catch (IOException e) {
                    throw new IllegalStateException("error while binding ScriptService", e);
                }
            }
        };
        scriptModule.prepareSettings(settingsModule);
        injector = new ModulesBuilder().add(new EnvironmentModule(new Environment(settings)), settingsModule,
                new ThreadPoolModule(threadPool), scriptModule, new IndicesModule() {

                    @Override
                    protected void configure() {
                        bindMapperExtension();
                    }
                }, new SearchModule(settings, namedWriteableRegistry) {
                    @Override
                    protected void configureSearch() {
                        // Skip me
                    }

                    @Override
                    protected void configureSuggesters() {
                        // Skip me
                    }
                }, new IndexSettingsModule(index, settings),

                new AbstractModule() {
                    @Override
                    protected void configure() {
                        bind(ClusterService.class).toProvider(Providers.of(clusterService));
                        bind(CircuitBreakerService.class).to(NoneCircuitBreakerService.class);
                        bind(NamedWriteableRegistry.class).toInstance(namedWriteableRegistry);
                    }
                }).createInjector();
        aggParsers = injector.getInstance(AggregatorParsers.class);
        // create some random type with some default field, those types will
        // stick around for all of the subclasses
        currentTypes = new String[randomIntBetween(0, 5)];
        for (int i = 0; i < currentTypes.length; i++) {
            String type = randomAsciiOfLengthBetween(1, 10);
            currentTypes[i] = type;
        }
        queriesRegistry = injector.getInstance(IndicesQueriesRegistry.class);
        parseFieldMatcher = ParseFieldMatcher.STRICT;
    }

    @AfterClass
    public static void afterClass() throws Exception {
        injector.getInstance(ClusterService.class).close();
        terminate(injector.getInstance(ThreadPool.class));
        injector = null;
        index = null;
        aggParsers = null;
        currentTypes = null;
        namedWriteableRegistry = null;
    }

    public void testTwoTypes() throws Exception {
        String source = JsonXContent.contentBuilder()
                .startObject()
                .startObject("in_stock")
                .startObject("filter")
                .startObject("range")
                .startObject("stock")
                .field("gt", 0)
                .endObject()
                .endObject()
                .endObject()
                .startObject("terms")
                .field("field", "stock")
                .endObject()
                .endObject()
                .endObject().string();
        try {
            XContentParser parser = XContentFactory.xContent(source).createParser(source);
            QueryParseContext parseContext = new QueryParseContext(queriesRegistry);
            parseContext.reset(parser);
            parseContext.parseFieldMatcher(parseFieldMatcher);
            assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
            aggParsers.parseAggregators(parser, parseContext);
            fail();
        } catch (ParsingException e) {
            assertThat(e.toString(), containsString("Found two aggregation type definitions in [in_stock]: [filter] and [terms]"));
        }
    }

    public void testTwoAggs() throws Exception {
        String source = JsonXContent.contentBuilder()
                .startObject()
                .startObject("by_date")
                .startObject("date_histogram")
                .field("field", "timestamp")
                .field("interval", "month")
                .endObject()
                .startObject("aggs")
                .startObject("tag_count")
                .startObject("cardinality")
                .field("field", "tag")
                .endObject()
                .endObject()
                .endObject()
                .startObject("aggs") // 2nd "aggs": illegal
                .startObject("tag_count2")
                .startObject("cardinality")
                .field("field", "tag")
                .endObject()
                .endObject()
                .endObject()
                .endObject().string();
        try {
            XContentParser parser = XContentFactory.xContent(source).createParser(source);
            QueryParseContext parseContext = new QueryParseContext(queriesRegistry);
            parseContext.reset(parser);
            parseContext.parseFieldMatcher(parseFieldMatcher);
            assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
            aggParsers.parseAggregators(parser, parseContext);
            fail();
        } catch (ParsingException e) {
            assertThat(e.toString(), containsString("Found two sub aggregation definitions under [by_date]"));
        }
    }

    public void testInvalidAggregationName() throws Exception {
        Matcher matcher = Pattern.compile("[^\\[\\]>]+").matcher("");
        String name;
        Random rand = getRandom();
        int len = randomIntBetween(1, 5);
        char[] word = new char[len];
        while (true) {
            for (int i = 0; i < word.length; i++) {
                word[i] = (char) rand.nextInt(127);
            }
            name = String.valueOf(word);
            if (!matcher.reset(name).matches()) {
                break;
            }
        }

        String source = JsonXContent.contentBuilder()
                .startObject()
                .startObject(name)
                .startObject("filter")
                .startObject("range")
                .startObject("stock")
                .field("gt", 0)
                .endObject()
                .endObject()
                .endObject()
                .endObject().string();
        try {
            XContentParser parser = XContentFactory.xContent(source).createParser(source);
            QueryParseContext parseContext = new QueryParseContext(queriesRegistry);
            parseContext.reset(parser);
            parseContext.parseFieldMatcher(parseFieldMatcher);
            assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
            aggParsers.parseAggregators(parser, parseContext);
            fail();
        } catch (ParsingException e) {
            assertThat(e.toString(), containsString("Invalid aggregation name [" + name + "]"));
        }
    }

    public void testSameAggregationName() throws Exception {
        final String name = randomAsciiOfLengthBetween(1, 10);
        String source = JsonXContent.contentBuilder()
                .startObject()
                .startObject(name)
                .startObject("terms")
                .field("field", "a")
                .endObject()
                .endObject()
                .startObject(name)
                .startObject("terms")
                .field("field", "b")
                .endObject()
                .endObject()
                .endObject().string();
        try {
            XContentParser parser = XContentFactory.xContent(source).createParser(source);
            QueryParseContext parseContext = new QueryParseContext(queriesRegistry);
            parseContext.reset(parser);
            parseContext.parseFieldMatcher(parseFieldMatcher);
            assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
            aggParsers.parseAggregators(parser, parseContext);
            fail();
        } catch (IllegalArgumentException e) {
            assertThat(e.toString(), containsString("Two sibling aggregations cannot have the same name: [" + name + "]"));
        }
    }

    public void testMissingName() throws Exception {
        String source = JsonXContent.contentBuilder()
                .startObject()
                .startObject("by_date")
                .startObject("date_histogram")
                .field("field", "timestamp")
                .field("interval", "month")
                .endObject()
                .startObject("aggs")
                // the aggregation name is missing
                //.startObject("tag_count")
                .startObject("cardinality")
                .field("field", "tag")
                .endObject()
                //.endObject()
                .endObject()
                .endObject().string();
        try {
            XContentParser parser = XContentFactory.xContent(source).createParser(source);
            QueryParseContext parseContext = new QueryParseContext(queriesRegistry);
            parseContext.reset(parser);
            parseContext.parseFieldMatcher(parseFieldMatcher);
            assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
            aggParsers.parseAggregators(parser, parseContext);
            fail();
        } catch (ParsingException e) {
            // All Good
        }
    }

    public void testMissingType() throws Exception {
        String source = JsonXContent.contentBuilder()
                .startObject()
                .startObject("by_date")
                .startObject("date_histogram")
                .field("field", "timestamp")
                .field("interval", "month")
                .endObject()
                .startObject("aggs")
                .startObject("tag_count")
                // the aggregation type is missing
                //.startObject("cardinality")
                .field("field", "tag")
                //.endObject()
                .endObject()
                .endObject()
                .endObject().string();
        try {
            XContentParser parser = XContentFactory.xContent(source).createParser(source);
            QueryParseContext parseContext = new QueryParseContext(queriesRegistry);
            parseContext.reset(parser);
            parseContext.parseFieldMatcher(parseFieldMatcher);
            assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
            aggParsers.parseAggregators(parser, parseContext);
            fail();
        } catch (ParsingException e) {
            // All Good
        }
    }
}