Query Refactoring: adding illegal argument checks to MatchQueryBuilder
This commit is contained in:
parent
f2b4ba9a7c
commit
adaa998225
|
@ -189,10 +189,12 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
|
|||
}
|
||||
|
||||
/**
|
||||
* When using fuzzy or prefix type query, the number of term expansions to use. Defaults to unbounded
|
||||
* so its recommended to set it to a reasonable value for faster execution.
|
||||
* When using fuzzy or prefix type query, the number of term expansions to use.
|
||||
*/
|
||||
public MatchQueryBuilder maxExpansions(int maxExpansions) {
|
||||
if (maxExpansions < 0 ) {
|
||||
throw new IllegalArgumentException("No negative maxExpansions allowed.");
|
||||
}
|
||||
this.maxExpansions = maxExpansions;
|
||||
return this;
|
||||
}
|
||||
|
@ -291,6 +293,9 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
|
|||
* {@link MatchQuery.ZeroTermsQuery#ALL} instead.
|
||||
*/
|
||||
public MatchQueryBuilder zeroTermsQuery(MatchQuery.ZeroTermsQuery zeroTermsQuery) {
|
||||
if (zeroTermsQuery == null) {
|
||||
throw new IllegalArgumentException("[" + NAME + "] requires zeroTermsQuery to be non-null");
|
||||
}
|
||||
this.zeroTermsQuery = zeroTermsQuery;
|
||||
return this;
|
||||
}
|
||||
|
|
|
@ -163,10 +163,56 @@ public class MatchQueryBuilderTests extends AbstractQueryTestCase<MatchQueryBuil
|
|||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testNegativePrefixLengthException() {
|
||||
public void testIllegalValues() {
|
||||
try {
|
||||
new MatchQueryBuilder(null, "value");
|
||||
fail("value must not be non-null");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
try {
|
||||
new MatchQueryBuilder("fieldName", null);
|
||||
fail("value must not be non-null");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
MatchQueryBuilder matchQuery = new MatchQueryBuilder("fieldName", "text");
|
||||
matchQuery.prefixLength(-1); // not allowed, should trigger expection
|
||||
try {
|
||||
matchQuery.prefixLength(-1);
|
||||
fail("must not be positive");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
try {
|
||||
matchQuery.maxExpansions(-1);
|
||||
fail("must not be positive");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
try {
|
||||
matchQuery.operator(null);
|
||||
fail("must not be non-null");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
try {
|
||||
matchQuery.type(null);
|
||||
fail("must not be non-null");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
try {
|
||||
matchQuery.zeroTermsQuery(null);
|
||||
fail("must not be non-null");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = QueryShardException.class)
|
||||
|
|
Loading…
Reference in New Issue