Skip to content

Commit

Permalink
[7.x][ML] Outlier detection mapping for nested feature influence (#62068
Browse files Browse the repository at this point in the history
) (#62150)

Adds mappings for outlier detection results.

Backport of #62068
  • Loading branch information
dimitris-athanasiou authored Sep 9, 2020
1 parent 146b2e6 commit 6c1700c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
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;

Expand Down Expand Up @@ -56,6 +59,20 @@ public static OutlierDetection fromXContent(XContentParser parser, boolean ignor

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.
*/
Expand Down Expand Up @@ -229,7 +246,11 @@ public List<FieldCardinalityConstraint> getFieldCardinalityConstraints() {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
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;

Expand Down Expand Up @@ -106,7 +108,13 @@ public void testFieldCardinalityLimitsIsEmpty() {
}

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() {
Expand Down

0 comments on commit 6c1700c

Please sign in to comment.