Skip to content

Commit 843ce78

Browse files
[ML] Fix exception when field is not included and excluded at the same time (#50192)
Executing the data frame analytics _explain API with a config that contains a field that is not in the includes list but at the same time is the excludes list results to trying to remove the field twice from the iterator. That causes an `IllegalStateException`. This commit fixes this issue and adds a test that captures the scenario.
1 parent 9f069f7 commit 843ce78

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,14 @@ private void applyIncludesExcludes(Set<String> fields, Set<String> includes, Set
235235
if (IGNORE_FIELDS.contains(field)) {
236236
throw ExceptionsHelper.badRequestException("field [{}] cannot be analyzed", field);
237237
}
238+
if (excludes.contains(field)) {
239+
fieldsIterator.remove();
240+
addExcludedField(field, "field in excludes list", fieldSelection);
241+
}
238242
} else {
239243
fieldsIterator.remove();
240244
addExcludedField(field, "field not in includes list", fieldSelection);
241245
}
242-
if (excludes.contains(field)) {
243-
fieldsIterator.remove();
244-
addExcludedField(field, "field in excludes list", fieldSelection);
245-
}
246246
}
247247
}
248248

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetectorTests.java

+21
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,27 @@ public void testDetect_GivenFieldIsBothIncludedAndExcluded() {
233233
);
234234
}
235235

236+
public void testDetect_GivenFieldIsNotIncludedAndIsExcluded() {
237+
FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder()
238+
.addAggregatableField("foo", "float")
239+
.addAggregatableField("bar", "float")
240+
.build();
241+
analyzedFields = new FetchSourceContext(true, new String[] {"foo"}, new String[] {"bar"});
242+
243+
ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector(
244+
SOURCE_INDEX, buildOutlierDetectionConfig(), false, 100, fieldCapabilities, Collections.emptyMap());
245+
Tuple<ExtractedFields, List<FieldSelection>> fieldExtraction = extractedFieldsDetector.detect();
246+
247+
List<ExtractedField> allFields = fieldExtraction.v1().getAllFields();
248+
assertThat(allFields, hasSize(1));
249+
assertThat(allFields.stream().map(ExtractedField::getName).collect(Collectors.toList()), contains("foo"));
250+
251+
assertFieldSelectionContains(fieldExtraction.v2(),
252+
FieldSelection.excluded("bar", Collections.singleton("float"), "field not in includes list"),
253+
FieldSelection.included("foo", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL)
254+
);
255+
}
256+
236257
public void testDetect_GivenRegressionAndRequiredFieldHasInvalidType() {
237258
FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder()
238259
.addAggregatableField("some_float", "float")

0 commit comments

Comments
 (0)