|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.ml.integration; |
| 7 | + |
| 8 | +import org.elasticsearch.action.bulk.BulkRequestBuilder; |
| 9 | +import org.elasticsearch.action.bulk.BulkResponse; |
| 10 | +import org.elasticsearch.action.get.GetResponse; |
| 11 | +import org.elasticsearch.action.index.IndexRequest; |
| 12 | +import org.elasticsearch.action.search.SearchResponse; |
| 13 | +import org.elasticsearch.action.support.WriteRequest; |
| 14 | +import org.elasticsearch.search.SearchHit; |
| 15 | +import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; |
| 16 | +import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsState; |
| 17 | +import org.junit.After; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import static org.hamcrest.Matchers.allOf; |
| 22 | +import static org.hamcrest.Matchers.equalTo; |
| 23 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 24 | +import static org.hamcrest.Matchers.is; |
| 25 | +import static org.hamcrest.Matchers.lessThanOrEqualTo; |
| 26 | + |
| 27 | +public class OutlierDetectionWithMissingFieldsIT extends MlNativeDataFrameAnalyticsIntegTestCase { |
| 28 | + |
| 29 | + @After |
| 30 | + public void cleanup() { |
| 31 | + cleanUp(); |
| 32 | + } |
| 33 | + |
| 34 | + public void testMissingFields() throws Exception { |
| 35 | + String sourceIndex = "test-outlier-detection-with-missing-fields"; |
| 36 | + |
| 37 | + client().admin().indices().prepareCreate(sourceIndex) |
| 38 | + .addMapping("_doc", "numeric", "type=double", "categorical", "type=keyword") |
| 39 | + .get(); |
| 40 | + |
| 41 | + BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); |
| 42 | + bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); |
| 43 | + |
| 44 | + // 5 docs with valid numeric value and missing categorical field (which should be ignored as it's not analyzed) |
| 45 | + for (int i = 0; i < 5; i++) { |
| 46 | + IndexRequest indexRequest = new IndexRequest(sourceIndex); |
| 47 | + indexRequest.source("numeric", 42.0); |
| 48 | + bulkRequestBuilder.add(indexRequest); |
| 49 | + } |
| 50 | + |
| 51 | + // Add a doc with missing field |
| 52 | + { |
| 53 | + IndexRequest missingIndexRequest = new IndexRequest(sourceIndex); |
| 54 | + missingIndexRequest.source("categorical", "foo"); |
| 55 | + bulkRequestBuilder.add(missingIndexRequest); |
| 56 | + } |
| 57 | + |
| 58 | + // Add a doc with numeric being array which is also treated as missing |
| 59 | + { |
| 60 | + IndexRequest arrayIndexRequest = new IndexRequest(sourceIndex); |
| 61 | + arrayIndexRequest.source("numeric", new double[]{1.0, 2.0}, "categorical", "foo"); |
| 62 | + bulkRequestBuilder.add(arrayIndexRequest); |
| 63 | + } |
| 64 | + |
| 65 | + BulkResponse bulkResponse = bulkRequestBuilder.get(); |
| 66 | + if (bulkResponse.hasFailures()) { |
| 67 | + fail("Failed to index data: " + bulkResponse.buildFailureMessage()); |
| 68 | + } |
| 69 | + |
| 70 | + String id = "test_outlier_detection_with_missing_fields"; |
| 71 | + DataFrameAnalyticsConfig config = buildOutlierDetectionAnalytics(id, new String[] {sourceIndex}, sourceIndex + "-results", null); |
| 72 | + registerAnalytics(config); |
| 73 | + putAnalytics(config); |
| 74 | + |
| 75 | + assertState(id, DataFrameAnalyticsState.STOPPED); |
| 76 | + |
| 77 | + startAnalytics(id); |
| 78 | + waitUntilAnalyticsIsStopped(id); |
| 79 | + |
| 80 | + SearchResponse sourceData = client().prepareSearch(sourceIndex).get(); |
| 81 | + for (SearchHit hit : sourceData.getHits()) { |
| 82 | + GetResponse destDocGetResponse = client().prepareGet().setIndex(config.getDest().getIndex()).setId(hit.getId()).get(); |
| 83 | + assertThat(destDocGetResponse.isExists(), is(true)); |
| 84 | + Map<String, Object> sourceDoc = hit.getSourceAsMap(); |
| 85 | + Map<String, Object> destDoc = destDocGetResponse.getSource(); |
| 86 | + for (String field : sourceDoc.keySet()) { |
| 87 | + assertThat(destDoc.containsKey(field), is(true)); |
| 88 | + assertThat(destDoc.get(field), equalTo(sourceDoc.get(field))); |
| 89 | + } |
| 90 | + if (destDoc.containsKey("numeric") && destDoc.get("numeric") instanceof Double) { |
| 91 | + assertThat(destDoc.containsKey("ml"), is(true)); |
| 92 | + @SuppressWarnings("unchecked") |
| 93 | + Map<String, Object> resultsObject = (Map<String, Object>) destDoc.get("ml"); |
| 94 | + |
| 95 | + assertThat(resultsObject.containsKey("outlier_score"), is(true)); |
| 96 | + double outlierScore = (double) resultsObject.get("outlier_score"); |
| 97 | + assertThat(outlierScore, allOf(greaterThanOrEqualTo(0.0), lessThanOrEqualTo(1.0))); |
| 98 | + } else { |
| 99 | + assertThat(destDoc.containsKey("ml"), is(false)); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments