From 7e4e1dabcff7876caaafd753c23179a66851199c Mon Sep 17 00:00:00 2001 From: David Kyle Date: Tue, 10 Apr 2018 13:19:00 +0100 Subject: [PATCH] [ML] Add categorical exclude condition (elastic/x-pack-elasticsearch#4326) Original commit: elastic/x-pack-elasticsearch@6c80988e0891a325338cd5571d0eae4a60b83343 --- .../core/ml/job/config/DetectionRule.java | 2 +- .../xpack/core/ml/job/config/Detector.java | 1 + .../core/ml/job/config/RuleCondition.java | 1 + .../core/ml/job/config/RuleConditionType.java | 26 ++++++++++++++----- .../ml/job/config/RuleConditionTests.java | 14 +++++----- .../ml/job/config/RuleConditionTypeTests.java | 12 +++++++++ 6 files changed, 40 insertions(+), 16 deletions(-) diff --git a/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DetectionRule.java b/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DetectionRule.java index a2f71f611f4..0948e978c88 100644 --- a/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DetectionRule.java +++ b/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DetectionRule.java @@ -251,7 +251,7 @@ public class DetectionRule implements ToXContentObject, Writeable { throw ExceptionsHelper.badRequestException(msg); } for (RuleCondition condition : conditions) { - if (condition.getType() == RuleConditionType.CATEGORICAL && targetFieldName != null) { + if (condition.getType().isCategorical() && targetFieldName != null) { String msg = Messages.getMessage(Messages.JOB_CONFIG_DETECTION_RULE_CONDITION_CATEGORICAL_INVALID_OPTION, DetectionRule.TARGET_FIELD_NAME_FIELD.getPreferredName()); throw ExceptionsHelper.badRequestException(msg); diff --git a/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Detector.java b/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Detector.java index f1a42fa2b7e..e5cf4b16f6e 100644 --- a/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Detector.java +++ b/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Detector.java @@ -698,6 +698,7 @@ public class Detector implements ToXContentObject, Writeable { List validOptions = Collections.emptyList(); switch (condition.getType()) { case CATEGORICAL: + case CATEGORICAL_COMPLEMENT: validOptions = extractAnalysisFields(); break; case NUMERICAL_ACTUAL: diff --git a/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleCondition.java b/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleCondition.java index 3b406e3ec34..6ca24c518d8 100644 --- a/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleCondition.java +++ b/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleCondition.java @@ -197,6 +197,7 @@ public class RuleCondition implements ToXContentObject, Writeable { private static void verifyFieldsBoundToType(RuleCondition ruleCondition) throws ElasticsearchParseException { switch (ruleCondition.getType()) { case CATEGORICAL: + case CATEGORICAL_COMPLEMENT: verifyCategorical(ruleCondition); break; case NUMERICAL_ACTUAL: diff --git a/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleConditionType.java b/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleConditionType.java index 2ddd0dac2cd..aa563d001b5 100644 --- a/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleConditionType.java +++ b/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleConditionType.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.core.ml.job.config; +import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; @@ -14,22 +15,29 @@ import java.io.IOException; import java.util.Locale; public enum RuleConditionType implements Writeable { - CATEGORICAL(false), - NUMERICAL_ACTUAL(true), - NUMERICAL_TYPICAL(true), - NUMERICAL_DIFF_ABS(true), - TIME(false); + CATEGORICAL(false, true), + NUMERICAL_ACTUAL(true, false), + NUMERICAL_TYPICAL(true, false), + NUMERICAL_DIFF_ABS(true, false), + TIME(false, false), + CATEGORICAL_COMPLEMENT(false, true); private final boolean isNumerical; + private final boolean isCategorical; - RuleConditionType(boolean isNumerical) { + RuleConditionType(boolean isNumerical, boolean isCategorical) { this.isNumerical = isNumerical; + this.isCategorical = isCategorical; } public boolean isNumerical() { return isNumerical; } + public boolean isCategorical() { + return isCategorical; + } + /** * Case-insensitive from string method. * @@ -47,7 +55,11 @@ public enum RuleConditionType implements Writeable { @Override public void writeTo(StreamOutput out) throws IOException { - out.writeEnum(this); + if (this == CATEGORICAL_COMPLEMENT && out.getVersion().before(Version.V_6_3_0)) { + out.writeEnum(CATEGORICAL); + } else { + out.writeEnum(this); + } } @Override diff --git a/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleConditionTests.java b/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleConditionTests.java index 62d174c05da..882c590983a 100644 --- a/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleConditionTests.java +++ b/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleConditionTests.java @@ -19,25 +19,22 @@ public class RuleConditionTests extends AbstractSerializingTestCase