Skip to content

Commit 95fd59c

Browse files
committed
Fixed Nullpointer in significant text agg - added test
1 parent 22b554e commit 95fd59c

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTextAggregatorFactory.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ public SignificantTextAggregatorFactory(String name,
8181
Map<String, Object> metadata) throws IOException {
8282
super(name, context, parent, subFactoriesBuilder, metadata);
8383

84-
// Note that if the field is unmapped (its field type is null), we don't fail,
85-
// and just use the given field name as a placeholder.
8684
this.fieldType = context.getFieldType(fieldName);
85+
if (fieldType == null ) {
86+
throw new IllegalArgumentException("Field [" + fieldName + "] does not exist, SignificantText " +
87+
"requires an analyzed field");
88+
}
8789
if (fieldType != null && fieldType.indexAnalyzer() == null) {
8890
throw new IllegalArgumentException("Field [" + fieldType.name() + "] has no analyzer, but SignificantText " +
8991
"requires an analyzed field");

server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTextAggregatorTests.java

+33
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.lucene.index.Term;
3232
import org.apache.lucene.search.IndexSearcher;
3333
import org.apache.lucene.search.MatchAllDocsQuery;
34+
import org.apache.lucene.search.MatchNoDocsQuery;
3435
import org.apache.lucene.search.TermQuery;
3536
import org.apache.lucene.store.Directory;
3637
import org.apache.lucene.util.BytesRef;
@@ -42,11 +43,13 @@
4243
import org.elasticsearch.index.mapper.TextFieldMapper;
4344
import org.elasticsearch.index.mapper.TextFieldMapper.TextFieldType;
4445
import org.elasticsearch.search.aggregations.AggregationBuilder;
46+
import org.elasticsearch.search.aggregations.AggregationExecutionException;
4547
import org.elasticsearch.search.aggregations.AggregatorTestCase;
4648
import org.elasticsearch.search.aggregations.bucket.sampler.InternalSampler;
4749
import org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregationBuilder;
4850
import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper;
4951
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
52+
import org.elasticsearch.search.aggregations.support.ValueType;
5053
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
5154

5255
import java.io.IOException;
@@ -58,6 +61,7 @@
5861

5962
import static org.elasticsearch.search.aggregations.AggregationBuilders.sampler;
6063
import static org.elasticsearch.search.aggregations.AggregationBuilders.significantText;
64+
import static org.hamcrest.Matchers.equalTo;
6165

6266
public class SignificantTextAggregatorTests extends AggregatorTestCase {
6367

@@ -144,6 +148,35 @@ public void testSignificance() throws IOException {
144148
}
145149
}
146150

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+
147180
public void testFieldAlias() throws IOException {
148181
TextFieldType textFieldType = new TextFieldType("text");
149182
textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer()));

0 commit comments

Comments
 (0)