-
Notifications
You must be signed in to change notification settings - Fork 25.2k
[ML] Outlier detection should only fetch docs that have the analyzed … #44944
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
Merged
dimitris-athanasiou
merged 2 commits into
elastic:master
from
dimitris-athanasiou:outlier-detection-should-query-docs-that-have-all-analyzed-fields
Jul 29, 2019
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...test/java/org/elasticsearch/xpack/ml/integration/OutlierDetectionWithMissingFieldsIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.ml.integration; | ||
|
||
import org.elasticsearch.action.bulk.BulkRequestBuilder; | ||
import org.elasticsearch.action.bulk.BulkResponse; | ||
import org.elasticsearch.action.get.GetResponse; | ||
import org.elasticsearch.action.index.IndexRequest; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
import org.elasticsearch.action.support.WriteRequest; | ||
import org.elasticsearch.search.SearchHit; | ||
import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; | ||
import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsState; | ||
import org.junit.After; | ||
|
||
import java.util.Map; | ||
|
||
import static org.hamcrest.Matchers.allOf; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.greaterThanOrEqualTo; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.lessThanOrEqualTo; | ||
|
||
public class OutlierDetectionWithMissingFieldsIT extends MlNativeDataFrameAnalyticsIntegTestCase { | ||
|
||
@After | ||
public void cleanup() { | ||
cleanUp(); | ||
} | ||
|
||
public void testMissingFields() throws Exception { | ||
String sourceIndex = "test-outlier-detection-with-missing-fields"; | ||
|
||
client().admin().indices().prepareCreate(sourceIndex) | ||
.addMapping("_doc", "numeric", "type=double", "categorical", "type=keyword") | ||
.get(); | ||
|
||
BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); | ||
bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); | ||
|
||
// 5 docs with valid value | ||
for (int i = 0; i < 5; i++) { | ||
IndexRequest indexRequest = new IndexRequest(sourceIndex); | ||
indexRequest.source("numeric", 42.0, "categorical", "foo"); | ||
bulkRequestBuilder.add(indexRequest); | ||
} | ||
|
||
// Add a doc with missing field | ||
{ | ||
IndexRequest missingIndexRequest = new IndexRequest(sourceIndex); | ||
missingIndexRequest.source("categorical", "foo"); | ||
bulkRequestBuilder.add(missingIndexRequest); | ||
} | ||
|
||
// Add a doc with numeric being array which is also treated as missing | ||
{ | ||
IndexRequest arrayIndexRequest = new IndexRequest(sourceIndex); | ||
arrayIndexRequest.source("numeric", new double[]{1.0, 2.0}, "categorical", "foo"); | ||
bulkRequestBuilder.add(arrayIndexRequest); | ||
} | ||
|
||
BulkResponse bulkResponse = bulkRequestBuilder.get(); | ||
if (bulkResponse.hasFailures()) { | ||
fail("Failed to index data: " + bulkResponse.buildFailureMessage()); | ||
} | ||
|
||
String id = "test_outlier_detection_with_missing_fields"; | ||
DataFrameAnalyticsConfig config = buildOutlierDetectionAnalytics(id, new String[] {sourceIndex}, sourceIndex + "-results", null); | ||
registerAnalytics(config); | ||
putAnalytics(config); | ||
|
||
assertState(id, DataFrameAnalyticsState.STOPPED); | ||
|
||
startAnalytics(id); | ||
waitUntilAnalyticsIsStopped(id); | ||
|
||
SearchResponse sourceData = client().prepareSearch(sourceIndex).get(); | ||
for (SearchHit hit : sourceData.getHits()) { | ||
GetResponse destDocGetResponse = client().prepareGet().setIndex(config.getDest().getIndex()).setId(hit.getId()).get(); | ||
assertThat(destDocGetResponse.isExists(), is(true)); | ||
Map<String, Object> sourceDoc = hit.getSourceAsMap(); | ||
Map<String, Object> destDoc = destDocGetResponse.getSource(); | ||
for (String field : sourceDoc.keySet()) { | ||
assertThat(destDoc.containsKey(field), is(true)); | ||
assertThat(destDoc.get(field), equalTo(sourceDoc.get(field))); | ||
} | ||
if (destDoc.containsKey("numeric") && destDoc.get("numeric") instanceof Double) { | ||
assertThat(destDoc.containsKey("ml"), is(true)); | ||
@SuppressWarnings("unchecked") | ||
Map<String, Object> resultsObject = (Map<String, Object>) destDoc.get("ml"); | ||
|
||
assertThat(resultsObject.containsKey("outlier_score"), is(true)); | ||
double outlierScore = (double) resultsObject.get("outlier_score"); | ||
assertThat(outlierScore, allOf(greaterThanOrEqualTo(0.0), lessThanOrEqualTo(1.0))); | ||
} else { | ||
assertThat(destDoc.containsKey("ml"), is(false)); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for certainty, you don't need to explicitly set analyzed_fields, because it defaults to all numeric fields?
You could index some docs that have "numeric" but are missing "categorical" to show that missing categorical field doesn't matter and "ml" object is still generated for such docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the categorical field in this case is not included in the analyzed fields. Good idea, I'll do so.