[ML] Improve error when no available field exists for rule scope (#32550)

Closes #32542
This commit is contained in:
Dimitris Athanasiou 2018-08-01 18:58:20 +01:00 committed by GitHub
parent c5140170f7
commit 8bf83647f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

View File

@ -84,6 +84,10 @@ public class RuleScope implements ToXContentObject, Writeable {
public void validate(Set<String> validKeys) {
Optional<String> invalidKey = scope.keySet().stream().filter(k -> !validKeys.contains(k)).findFirst();
if (invalidKey.isPresent()) {
if (validKeys.isEmpty()) {
throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTION_RULE_SCOPE_NO_AVAILABLE_FIELDS,
invalidKey.get()));
}
throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD,
invalidKey.get(), validKeys));
}

View File

@ -95,6 +95,8 @@ public final class Messages {
"Invalid detector rule: function {0} only supports conditions that apply to time";
public static final String JOB_CONFIG_DETECTION_RULE_REQUIRES_SCOPE_OR_CONDITION =
"Invalid detector rule: at least scope or a condition is required";
public static final String JOB_CONFIG_DETECTION_RULE_SCOPE_NO_AVAILABLE_FIELDS =
"Invalid detector rule: scope field ''{0}'' is invalid; detector has no available fields for scoping";
public static final String JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD =
"Invalid detector rule: scope field ''{0}'' is invalid; select from {1}";
public static final String JOB_CONFIG_FIELDNAME_INCOMPATIBLE_FUNCTION = "field_name cannot be used with function ''{0}''";

View File

@ -10,6 +10,8 @@ import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.test.AbstractWireSerializingTestCase;
import java.util.Collections;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
@ -53,6 +55,17 @@ public class RuleScopeTests extends AbstractWireSerializingTestCase<RuleScope> {
scope.validate(Sets.newHashSet("foo", "bar", "foobar"));
}
public void testValidate_GivenNoAvailableFieldsForScope() {
RuleScope scope = RuleScope.builder()
.include("foo", "filter1")
.build();
assertThat(scope.isEmpty(), is(false));
ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> scope.validate(Collections.emptySet()));
assertThat(e.getMessage(), equalTo("Invalid detector rule: scope field 'foo' is invalid; " +
"detector has no available fields for scoping"));
}
public void testValidate_GivenMultipleFieldsIncludingInvalid() {
RuleScope scope = RuleScope.builder()
.include("foo", "filter1")