Skip to content

Commit 10e5902

Browse files
authored
Search - fixed Nullpointer in significant text agg when field does not exist (#64144) (#64157)
Fixed Nullpointer in significant text agg - added test Closes #64045
1 parent d727c33 commit 10e5902

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

docs/build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,9 @@ buildRestTests.setups['news'] = '''
432432
type: keyword
433433
content:
434434
type: text
435+
copy_to: custom_all
436+
custom_all:
437+
type: text
435438
- do:
436439
bulk:
437440
index: news

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

+32
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858

5959
import static org.elasticsearch.search.aggregations.AggregationBuilders.sampler;
6060
import static org.elasticsearch.search.aggregations.AggregationBuilders.significantText;
61+
import static org.hamcrest.Matchers.equalTo;
6162

6263
public class SignificantTextAggregatorTests extends AggregatorTestCase {
6364

@@ -144,6 +145,37 @@ public void testSignificance() throws IOException {
144145
}
145146
}
146147

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

0 commit comments

Comments
 (0)