[7.x][ML] Outlier detection mapping for nested feature influence (#62068) (#62150)

Adds mappings for outlier detection results.

Backport of #62068
This commit is contained in:
Dimitris Athanasiou 2020-09-09 12:41:26 +03:00 committed by GitHub
parent 146b2e6b1a
commit 6c1700c343
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 3 deletions

View File

@ -13,6 +13,9 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.KeywordFieldMapper;
import org.elasticsearch.index.mapper.NumberFieldMapper;
import org.elasticsearch.index.mapper.ObjectMapper;
import org.elasticsearch.xpack.core.ml.inference.trainedmodel.InferenceConfig;
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
@ -56,6 +59,20 @@ public class OutlierDetection implements DataFrameAnalysis {
private static final List<String> PROGRESS_PHASES = Collections.singletonList("computing_outliers");
static final Map<String, Object> FEATURE_INFLUENCE_MAPPING;
static {
Map<String, Object> properties = new HashMap<>();
properties.put("feature_name", Collections.singletonMap("type", KeywordFieldMapper.CONTENT_TYPE));
properties.put("influence", Collections.singletonMap("type", NumberFieldMapper.NumberType.DOUBLE.typeName()));
Map<String, Object> mapping = new HashMap<>();
mapping.put("dynamic", false);
mapping.put("type", ObjectMapper.NESTED_CONTENT_TYPE);
mapping.put("properties", properties);
FEATURE_INFLUENCE_MAPPING = Collections.unmodifiableMap(mapping);
}
/**
* The number of neighbors. Leave unspecified for dynamic detection.
*/
@ -229,7 +246,11 @@ public class OutlierDetection implements DataFrameAnalysis {
@Override
public Map<String, Object> getExplicitlyMappedFields(Map<String, Object> mappingsProperties, String resultsFieldName) {
return Collections.emptyMap();
Map<String, Object> additionalProperties = new HashMap<>();
additionalProperties.put(resultsFieldName + ".outlier_score",
Collections.singletonMap("type", NumberFieldMapper.NumberType.DOUBLE.typeName()));
additionalProperties.put(resultsFieldName + ".feature_influence", FEATURE_INFLUENCE_MAPPING);
return additionalProperties;
}
@Override

View File

@ -8,15 +8,17 @@ package org.elasticsearch.xpack.core.ml.dataframe.analyses;
import org.elasticsearch.Version;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.NumberFieldMapper;
import org.elasticsearch.xpack.core.ml.AbstractBWCSerializationTestCase;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
@ -106,7 +108,13 @@ public class OutlierDetectionTests extends AbstractBWCSerializationTestCase<Outl
}
public void testGetExplicitlyMappedFields() {
assertThat(createTestInstance().getExplicitlyMappedFields(null, null), is(anEmptyMap()));
Map<String, Object> mappedFields = createTestInstance().getExplicitlyMappedFields(null, "test");
assertThat(mappedFields.size(), equalTo(2));
assertThat(mappedFields, hasKey("test.outlier_score"));
assertThat(mappedFields.get("test.outlier_score"),
equalTo(Collections.singletonMap("type", NumberFieldMapper.NumberType.DOUBLE.typeName())));
assertThat(mappedFields, hasKey("test.feature_influence"));
assertThat(mappedFields.get("test.feature_influence"), equalTo(OutlierDetection.FEATURE_INFLUENCE_MAPPING));
}
public void testGetStateDocId() {