Skip to content

Commit e302c66

Browse files
[7.x][ML] Fix NPE when starting classification with missing dependent_variable (#59524) (#59540)
Since we have added checking the cardinality of the dependent_variable for classification, we have introduced a bug where an NPE is thrown if the dependent_variable is a missing field. This commit is fixing this issue. Backport of #59524
1 parent d477aa1 commit e302c66

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetectorFactory.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ private void getCardinalitiesForFieldsWithConstraints(String[] index,
113113

114114
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(0).query(config.getSource().getParsedQuery());
115115
for (FieldCardinalityConstraint constraint : fieldCardinalityConstraints) {
116-
for (FieldCapabilities fieldCaps : fieldCapabilitiesResponse.getField(constraint.getField()).values()) {
116+
Map<String, FieldCapabilities> fieldCapsPerType = fieldCapabilitiesResponse.getField(constraint.getField());
117+
if (fieldCapsPerType == null) {
118+
throw ExceptionsHelper.badRequestException("no mappings could be found for field [{}]", constraint.getField());
119+
}
120+
for (FieldCapabilities fieldCaps : fieldCapsPerType.values()) {
117121
if (fieldCaps.isAggregatable() == false) {
118122
throw ExceptionsHelper.badRequestException("field [{}] of type [{}] is non-aggregatable",
119123
fieldCaps.getName(), fieldCaps.getType());

x-pack/plugin/src/test/resources/rest-api-spec/test/ml/start_data_frame_analytics.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
catch: /Unable to start empty-with-compatible-fields as no documents in the source indices \[empty-index-with-compatible-fields\] contained all the fields selected for analysis/
124124
ml.start_data_frame_analytics:
125125
id: "empty-with-compatible-fields"
126+
126127
---
127128
"Test start with inconsistent body/param ids":
128129

@@ -213,3 +214,32 @@
213214
catch: /Field \[keyword_field\] must have at least \[2\] distinct values but there were \[1\]/
214215
ml.start_data_frame_analytics:
215216
id: "classification-cardinality-limits"
217+
218+
---
219+
"Test start classification analysis when the dependent variable is missing":
220+
- do:
221+
indices.create:
222+
index: index-with-missing-dep-var
223+
body:
224+
mappings:
225+
properties:
226+
numeric_field: { type: "long" }
227+
228+
- do:
229+
ml.put_data_frame_analytics:
230+
id: "classification-missing-dep-var"
231+
body: >
232+
{
233+
"source": {
234+
"index": "index-with-missing-dep-var"
235+
},
236+
"dest": {
237+
"index": "index-with-missing-dep-var-dest"
238+
},
239+
"analysis": { "classification": { "dependent_variable": "missing" } }
240+
}
241+
242+
- do:
243+
catch: /no mappings could be found for field \[missing\]/
244+
ml.start_data_frame_analytics:
245+
id: "classification-missing-dep-var"

0 commit comments

Comments
 (0)