Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Outlier detection mapping for nested feature influence #62068

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[ML] Outlier detection mapping for nested feature influence
Adds mappings to for outlier detection results.
dimitris-athanasiou committed Sep 7, 2020
commit 1409191c2061598b5d2769920bc609df12bf599c
Original file line number Diff line number Diff line change
@@ -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;

@@ -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.
*/
@@ -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
Original file line number Diff line number Diff line change
@@ -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;

@@ -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() {