Query Refactoring: adding illegal argument checks to MatchQueryBuilder

This commit is contained in:
Christoph Büscher 2015-09-11 15:56:18 +02:00
parent f2b4ba9a7c
commit adaa998225
2 changed files with 56 additions and 5 deletions

View File

@ -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 * When using fuzzy or prefix type query, the number of term expansions to use.
* so its recommended to set it to a reasonable value for faster execution.
*/ */
public MatchQueryBuilder maxExpansions(int maxExpansions) { public MatchQueryBuilder maxExpansions(int maxExpansions) {
if (maxExpansions < 0 ) {
throw new IllegalArgumentException("No negative maxExpansions allowed.");
}
this.maxExpansions = maxExpansions; this.maxExpansions = maxExpansions;
return this; return this;
} }
@ -291,6 +293,9 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
* {@link MatchQuery.ZeroTermsQuery#ALL} instead. * {@link MatchQuery.ZeroTermsQuery#ALL} instead.
*/ */
public MatchQueryBuilder zeroTermsQuery(MatchQuery.ZeroTermsQuery zeroTermsQuery) { public MatchQueryBuilder zeroTermsQuery(MatchQuery.ZeroTermsQuery zeroTermsQuery) {
if (zeroTermsQuery == null) {
throw new IllegalArgumentException("[" + NAME + "] requires zeroTermsQuery to be non-null");
}
this.zeroTermsQuery = zeroTermsQuery; this.zeroTermsQuery = zeroTermsQuery;
return this; return this;
} }

View File

@ -163,10 +163,56 @@ public class MatchQueryBuilderTests extends AbstractQueryTestCase<MatchQueryBuil
} }
} }
@Test(expected = IllegalArgumentException.class) public void testIllegalValues() {
public void testNegativePrefixLengthException() { 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"); 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) @Test(expected = QueryShardException.class)