|
31 | 31 | import org.apache.lucene.index.Term;
|
32 | 32 | import org.apache.lucene.search.IndexSearcher;
|
33 | 33 | import org.apache.lucene.search.MatchAllDocsQuery;
|
| 34 | +import org.apache.lucene.search.MatchNoDocsQuery; |
34 | 35 | import org.apache.lucene.search.TermQuery;
|
35 | 36 | import org.apache.lucene.store.Directory;
|
36 | 37 | import org.apache.lucene.util.BytesRef;
|
|
42 | 43 | import org.elasticsearch.index.mapper.TextFieldMapper;
|
43 | 44 | import org.elasticsearch.index.mapper.TextFieldMapper.TextFieldType;
|
44 | 45 | import org.elasticsearch.search.aggregations.AggregationBuilder;
|
| 46 | +import org.elasticsearch.search.aggregations.AggregationExecutionException; |
45 | 47 | import org.elasticsearch.search.aggregations.AggregatorTestCase;
|
46 | 48 | import org.elasticsearch.search.aggregations.bucket.sampler.InternalSampler;
|
47 | 49 | import org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregationBuilder;
|
48 | 50 | import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper;
|
49 | 51 | import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
|
| 52 | +import org.elasticsearch.search.aggregations.support.ValueType; |
50 | 53 | import org.elasticsearch.search.aggregations.support.ValuesSourceType;
|
51 | 54 |
|
52 | 55 | import java.io.IOException;
|
|
58 | 61 |
|
59 | 62 | import static org.elasticsearch.search.aggregations.AggregationBuilders.sampler;
|
60 | 63 | import static org.elasticsearch.search.aggregations.AggregationBuilders.significantText;
|
| 64 | +import static org.hamcrest.Matchers.equalTo; |
61 | 65 |
|
62 | 66 | public class SignificantTextAggregatorTests extends AggregatorTestCase {
|
63 | 67 |
|
@@ -144,6 +148,35 @@ public void testSignificance() throws IOException {
|
144 | 148 | }
|
145 | 149 | }
|
146 | 150 |
|
| 151 | + |
| 152 | + public void testMissingField() throws IOException { |
| 153 | + TextFieldType textFieldType = new TextFieldType("text"); |
| 154 | + textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer())); |
| 155 | + |
| 156 | + IndexWriterConfig indexWriterConfig = newIndexWriterConfig(); |
| 157 | + indexWriterConfig.setMaxBufferedDocs(100); |
| 158 | + indexWriterConfig.setRAMBufferSizeMB(100); |
| 159 | + try (Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, indexWriterConfig)) { |
| 160 | + indexDocuments(w); |
| 161 | + |
| 162 | + SignificantTextAggregationBuilder sigAgg = new SignificantTextAggregationBuilder("sig_text", "this_field_does_not_exist").filterDuplicateText(true); |
| 163 | + if(randomBoolean()){ |
| 164 | + sigAgg.sourceFieldNames(Arrays.asList(new String [] {"json_only_field"})); |
| 165 | + } |
| 166 | + SamplerAggregationBuilder aggBuilder = new SamplerAggregationBuilder("sampler") |
| 167 | + .subAggregation(sigAgg); |
| 168 | + |
| 169 | + try (IndexReader reader = DirectoryReader.open(w)) { |
| 170 | + IndexSearcher searcher = new IndexSearcher(reader); |
| 171 | + |
| 172 | + |
| 173 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 174 | + () -> searchAndReduce(searcher, new TermQuery(new Term("text", "odd")), aggBuilder, textFieldType)); |
| 175 | + assertThat(e.getMessage(), equalTo("Field [this_field_does_not_exist] does not exist, SignificantText requires an analyzed field")); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
147 | 180 | public void testFieldAlias() throws IOException {
|
148 | 181 | TextFieldType textFieldType = new TextFieldType("text");
|
149 | 182 | textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer()));
|
|
0 commit comments