mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-17 10:25:15 +00:00
[ML] Improve error when no available field exists for rule scope (#32550)
Closes #32542
This commit is contained in:
parent
c5140170f7
commit
8bf83647f5
@ -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));
|
||||
}
|
||||
|
@ -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}''";
|
||||
|
@ -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")
|
||||
|
Loading…
x
Reference in New Issue
Block a user