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

Make 0 as invalid value for min_children in has_child query #33073

Closed
wants to merge 3 commits into from
Closed
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 @@ -66,7 +66,7 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
/**
* The default minimum number of children that are required to match for the parent to be considered a match.
*/
public static final int DEFAULT_MIN_CHILDREN = 0;
public static final int DEFAULT_MIN_CHILDREN = 1;

/**
* The default value for ignore_unmapped.
Expand Down Expand Up @@ -133,8 +133,8 @@ protected void doWriteTo(StreamOutput out) throws IOException {
* the maximum number of children that are required to match for the parent to be considered a match.
*/
public HasChildQueryBuilder minMaxChildren(int minChildren, int maxChildren) {
if (minChildren < 0) {
throw new IllegalArgumentException("[" + NAME + "] requires non-negative 'min_children' field");
if (minChildren < 1) {
throw new IllegalArgumentException("[" + NAME + "] requires non-negative, non-zero 'min_children' field");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of "non-negative, non-zero", we can just use "positive"

}
if (maxChildren < 0) {
throw new IllegalArgumentException("[" + NAME + "] requires non-negative 'max_children' field");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ public void testIllegalValues() {
int positiveValue = randomIntBetween(0, Integer.MAX_VALUE);
HasChildQueryBuilder foo = hasChildQuery("foo", query, ScoreMode.None); // all good
e = expectThrows(IllegalArgumentException.class, () -> foo.minMaxChildren(randomIntBetween(Integer.MIN_VALUE, -1), positiveValue));
assertEquals("[has_child] requires non-negative 'min_children' field", e.getMessage());
assertEquals("[has_child] requires non-negative, non-zero 'min_children' field", e.getMessage());

e = expectThrows(IllegalArgumentException.class, () -> foo.minMaxChildren(0, positiveValue));
assertEquals("[has_child] requires non-negative, non-zero 'min_children' field", e.getMessage());

e = expectThrows(IllegalArgumentException.class, () -> foo.minMaxChildren(positiveValue, randomIntBetween(Integer.MIN_VALUE, -1)));
assertEquals("[has_child] requires non-negative 'max_children' field", e.getMessage());
Expand Down