[7.x][ML] Fix exception when field is not included and excluded at the same time (#50192) (#50223)

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.

Backport of #50192
This commit is contained in:
Dimitris Athanasiou 2019-12-16 11:30:06 +00:00 committed by GitHub
parent 761d6e8e4b
commit 73add726d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 4 deletions

View File

@ -235,14 +235,14 @@ public class ExtractedFieldsDetector {
if (IGNORE_FIELDS.contains(field)) { if (IGNORE_FIELDS.contains(field)) {
throw ExceptionsHelper.badRequestException("field [{}] cannot be analyzed", field); throw ExceptionsHelper.badRequestException("field [{}] cannot be analyzed", field);
} }
if (excludes.contains(field)) {
fieldsIterator.remove();
addExcludedField(field, "field in excludes list", fieldSelection);
}
} else { } else {
fieldsIterator.remove(); fieldsIterator.remove();
addExcludedField(field, "field not in includes list", fieldSelection); addExcludedField(field, "field not in includes list", fieldSelection);
} }
if (excludes.contains(field)) {
fieldsIterator.remove();
addExcludedField(field, "field in excludes list", fieldSelection);
}
} }
} }

View File

@ -233,6 +233,27 @@ public class ExtractedFieldsDetectorTests extends ESTestCase {
); );
} }
public void testDetect_GivenFieldIsNotIncludedAndIsExcluded() {
FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder()
.addAggregatableField("foo", "float")
.addAggregatableField("bar", "float")
.build();
analyzedFields = new FetchSourceContext(true, new String[] {"foo"}, new String[] {"bar"});
ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector(
SOURCE_INDEX, buildOutlierDetectionConfig(), false, 100, fieldCapabilities, Collections.emptyMap());
Tuple<ExtractedFields, List<FieldSelection>> fieldExtraction = extractedFieldsDetector.detect();
List<ExtractedField> allFields = fieldExtraction.v1().getAllFields();
assertThat(allFields, hasSize(1));
assertThat(allFields.stream().map(ExtractedField::getName).collect(Collectors.toList()), contains("foo"));
assertFieldSelectionContains(fieldExtraction.v2(),
FieldSelection.excluded("bar", Collections.singleton("float"), "field not in includes list"),
FieldSelection.included("foo", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL)
);
}
public void testDetect_GivenRegressionAndRequiredFieldHasInvalidType() { public void testDetect_GivenRegressionAndRequiredFieldHasInvalidType() {
FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder()
.addAggregatableField("some_float", "float") .addAggregatableField("some_float", "float")