|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.search.query; |
| 10 | + |
| 11 | +import org.apache.lucene.analysis.Analyzer; |
| 12 | +import org.apache.lucene.analysis.TokenStream; |
| 13 | +import org.apache.lucene.analysis.Tokenizer; |
| 14 | +import org.apache.lucene.analysis.core.KeywordTokenizer; |
| 15 | +import org.elasticsearch.action.search.SearchResponse; |
| 16 | +import org.elasticsearch.index.analysis.AnalyzerProvider; |
| 17 | +import org.elasticsearch.index.analysis.AnalyzerScope; |
| 18 | +import org.elasticsearch.index.query.IntervalQueryBuilder; |
| 19 | +import org.elasticsearch.index.query.IntervalsSourceProvider; |
| 20 | +import org.elasticsearch.indices.analysis.AnalysisModule; |
| 21 | +import org.elasticsearch.plugins.AnalysisPlugin; |
| 22 | +import org.elasticsearch.plugins.Plugin; |
| 23 | +import org.elasticsearch.test.ESIntegTestCase; |
| 24 | +import org.elasticsearch.test.InternalSettingsPlugin; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +import static java.util.Collections.singletonMap; |
| 32 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 33 | + |
| 34 | +public class IntervalQueriesIT extends ESIntegTestCase { |
| 35 | + |
| 36 | + @Override |
| 37 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 38 | + return Arrays.asList(InternalSettingsPlugin.class, MockAnalysisPlugin.class); |
| 39 | + } |
| 40 | + |
| 41 | + public void testEmptyIntervalsWithNestedMappings() throws InterruptedException { |
| 42 | + assertAcked(prepareCreate("nested").setMapping(""" |
| 43 | + { "_doc" : { |
| 44 | + "properties" : { |
| 45 | + "empty_text" : { "type" : "text", "analyzer" : "empty" }, |
| 46 | + "text" : { "type" : "text" }, |
| 47 | + "nested" : { "type" : "nested", "properties" : { "nt" : { "type" : "text" } } } |
| 48 | + } |
| 49 | + }} |
| 50 | + """)); |
| 51 | + |
| 52 | + indexRandom( |
| 53 | + true, |
| 54 | + client().prepareIndex("nested").setId("1").setSource("text", "the quick brown fox jumps"), |
| 55 | + client().prepareIndex("nested").setId("2").setSource("text", "quick brown"), |
| 56 | + client().prepareIndex("nested").setId("3").setSource("text", "quick") |
| 57 | + ); |
| 58 | + |
| 59 | + SearchResponse resp = client().prepareSearch("nested") |
| 60 | + .setQuery( |
| 61 | + new IntervalQueryBuilder("empty_text", new IntervalsSourceProvider.Match("an empty query", 0, true, null, null, null)) |
| 62 | + ) |
| 63 | + .get(); |
| 64 | + assertEquals(0, resp.getFailedShards()); |
| 65 | + } |
| 66 | + |
| 67 | + private static class EmptyAnalyzer extends Analyzer { |
| 68 | + |
| 69 | + @Override |
| 70 | + protected TokenStreamComponents createComponents(String fieldName) { |
| 71 | + Tokenizer source = new KeywordTokenizer(); |
| 72 | + TokenStream sink = new TokenStream() { |
| 73 | + @Override |
| 74 | + public boolean incrementToken() throws IOException { |
| 75 | + return false; |
| 76 | + } |
| 77 | + }; |
| 78 | + return new TokenStreamComponents(source, sink); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public static class MockAnalysisPlugin extends Plugin implements AnalysisPlugin { |
| 83 | + |
| 84 | + @Override |
| 85 | + public Map<String, AnalysisModule.AnalysisProvider<AnalyzerProvider<? extends Analyzer>>> getAnalyzers() { |
| 86 | + return singletonMap("empty", (indexSettings, environment, name, settings) -> new AnalyzerProvider<>() { |
| 87 | + @Override |
| 88 | + public String name() { |
| 89 | + return "empty"; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public AnalyzerScope scope() { |
| 94 | + return AnalyzerScope.GLOBAL; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public Analyzer get() { |
| 99 | + return new EmptyAnalyzer(); |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments