Skip to content

Commit f330d53

Browse files
[ML] Improve error when no available field exists for rule scope (#32550)
Closes #32542
1 parent 7b111e8 commit f330d53

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleScope.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public boolean isEmpty() {
8484
public void validate(Set<String> validKeys) {
8585
Optional<String> invalidKey = scope.keySet().stream().filter(k -> !validKeys.contains(k)).findFirst();
8686
if (invalidKey.isPresent()) {
87+
if (validKeys.isEmpty()) {
88+
throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTION_RULE_SCOPE_NO_AVAILABLE_FIELDS,
89+
invalidKey.get()));
90+
}
8791
throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD,
8892
invalidKey.get(), validKeys));
8993
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/messages/Messages.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public final class Messages {
9595
"Invalid detector rule: function {0} only supports conditions that apply to time";
9696
public static final String JOB_CONFIG_DETECTION_RULE_REQUIRES_SCOPE_OR_CONDITION =
9797
"Invalid detector rule: at least scope or a condition is required";
98+
public static final String JOB_CONFIG_DETECTION_RULE_SCOPE_NO_AVAILABLE_FIELDS =
99+
"Invalid detector rule: scope field ''{0}'' is invalid; detector has no available fields for scoping";
98100
public static final String JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD =
99101
"Invalid detector rule: scope field ''{0}'' is invalid; select from {1}";
100102
public static final String JOB_CONFIG_FIELDNAME_INCOMPATIBLE_FUNCTION = "field_name cannot be used with function ''{0}''";

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleScopeTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.elasticsearch.common.util.set.Sets;
1111
import org.elasticsearch.test.AbstractWireSerializingTestCase;
1212

13+
import java.util.Collections;
14+
1315
import static org.hamcrest.Matchers.contains;
1416
import static org.hamcrest.Matchers.equalTo;
1517
import static org.hamcrest.Matchers.is;
@@ -53,6 +55,17 @@ public void testValidate_GivenMultipleValidFields() {
5355
scope.validate(Sets.newHashSet("foo", "bar", "foobar"));
5456
}
5557

58+
public void testValidate_GivenNoAvailableFieldsForScope() {
59+
RuleScope scope = RuleScope.builder()
60+
.include("foo", "filter1")
61+
.build();
62+
assertThat(scope.isEmpty(), is(false));
63+
64+
ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> scope.validate(Collections.emptySet()));
65+
assertThat(e.getMessage(), equalTo("Invalid detector rule: scope field 'foo' is invalid; " +
66+
"detector has no available fields for scoping"));
67+
}
68+
5669
public void testValidate_GivenMultipleFieldsIncludingInvalid() {
5770
RuleScope scope = RuleScope.builder()
5871
.include("foo", "filter1")

0 commit comments

Comments
 (0)