|
58 | 58 |
|
59 | 59 | import static org.elasticsearch.search.aggregations.AggregationBuilders.sampler;
|
60 | 60 | import static org.elasticsearch.search.aggregations.AggregationBuilders.significantText;
|
| 61 | +import static org.hamcrest.Matchers.equalTo; |
61 | 62 |
|
62 | 63 | public class SignificantTextAggregatorTests extends AggregatorTestCase {
|
63 | 64 |
|
@@ -144,6 +145,37 @@ public void testSignificance() throws IOException {
|
144 | 145 | }
|
145 | 146 | }
|
146 | 147 |
|
| 148 | + |
| 149 | + public void testMissingField() throws IOException { |
| 150 | + TextFieldType textFieldType = new TextFieldType("text"); |
| 151 | + textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer())); |
| 152 | + |
| 153 | + IndexWriterConfig indexWriterConfig = newIndexWriterConfig(); |
| 154 | + indexWriterConfig.setMaxBufferedDocs(100); |
| 155 | + indexWriterConfig.setRAMBufferSizeMB(100); |
| 156 | + try (Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, indexWriterConfig)) { |
| 157 | + indexDocuments(w); |
| 158 | + |
| 159 | + SignificantTextAggregationBuilder sigAgg = new SignificantTextAggregationBuilder("sig_text", "this_field_does_not_exist") |
| 160 | + .filterDuplicateText(true); |
| 161 | + if(randomBoolean()){ |
| 162 | + sigAgg.sourceFieldNames(Arrays.asList(new String [] {"json_only_field"})); |
| 163 | + } |
| 164 | + SamplerAggregationBuilder aggBuilder = new SamplerAggregationBuilder("sampler") |
| 165 | + .subAggregation(sigAgg); |
| 166 | + |
| 167 | + try (IndexReader reader = DirectoryReader.open(w)) { |
| 168 | + IndexSearcher searcher = new IndexSearcher(reader); |
| 169 | + |
| 170 | + |
| 171 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 172 | + () -> searchAndReduce(searcher, new TermQuery(new Term("text", "odd")), aggBuilder, textFieldType)); |
| 173 | + assertThat(e.getMessage(), equalTo("Field [this_field_does_not_exist] does not exist, SignificantText " |
| 174 | + + "requires an analyzed field")); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
147 | 179 | public void testFieldAlias() throws IOException {
|
148 | 180 | TextFieldType textFieldType = new TextFieldType("text");
|
149 | 181 | textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer()));
|
|
0 commit comments