Skip to content
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

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public boolean isEmpty() {
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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public final class Messages {
"Invalid detector rule: function {0} does not support rules with conditions";
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}''";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
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;
Expand Down Expand Up @@ -53,6 +55,17 @@ public void testValidate_GivenMultipleValidFields() {
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")
Expand Down