diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetector.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetector.java index 682cc94433c..4580a4f0ef6 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetector.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetector.java @@ -203,7 +203,7 @@ public class ExtractedFieldsDetector { .expand(includes, false); // If the exclusion set does not match anything, that means the fields are already not present // no need to raise if nothing matched - Set excludedSet = NameResolver.newUnaliased(fields, + Set excludedSet = NameResolver.newUnaliased(fieldCapabilitiesResponse.get().keySet(), (ex) -> new ResourceNotFoundException( Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_BAD_FIELD_FILTER, ex))) .expand(excludes, true); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetectorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetectorTests.java index 5f7bd650a1c..25553627a9e 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetectorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetectorTests.java @@ -300,6 +300,42 @@ public class ExtractedFieldsDetectorTests extends ESTestCase { assertThat(e.getMessage(), equalTo("No field [_id] could be detected")); } + public void testDetect_GivenExcludedFieldIsMissing() { + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() + .addAggregatableField("foo", "float") + .build(); + FetchSourceContext analyzedFields = new FetchSourceContext(true, new String[]{"*"}, new String[] {"bar"}); + + ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( + SOURCE_INDEX, buildOutlierDetectionConfig(analyzedFields), false, 100, fieldCapabilities, Collections.emptyMap()); + ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> extractedFieldsDetector.detect()); + + assertThat(e.getMessage(), equalTo("No field [bar] could be detected")); + } + + public void testDetect_GivenExcludedFieldIsUnsupported() { + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() + .addAggregatableField("numeric", "float") + .addAggregatableField("categorical", "keyword") + .build(); + FetchSourceContext analyzedFields = new FetchSourceContext(true, null, new String[] {"categorical"}); + + ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( + SOURCE_INDEX, buildOutlierDetectionConfig(analyzedFields), false, 100, fieldCapabilities, Collections.emptyMap()); + + Tuple> fieldExtraction = extractedFieldsDetector.detect(); + + List allFields = fieldExtraction.v1().getAllFields(); + assertThat(allFields.size(), equalTo(1)); + assertThat(allFields.get(0).getName(), equalTo("numeric")); + + assertFieldSelectionContains(fieldExtraction.v2(), + FieldSelection.excluded("categorical", Collections.singleton("keyword"), + "unsupported type; supported types are [boolean, byte, double, float, half_float, integer, long, scaled_float, short]"), + FieldSelection.included("numeric", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL) + ); + } + public void testDetect_ShouldSortFieldsAlphabetically() { int fieldCount = randomIntBetween(10, 20); List fields = new ArrayList<>();