Enforce MatchQueryBuilder#maxExpansions() to be strictly positive
This commit is contained in:
parent
a01ecb20ea
commit
757ccf00b2
|
@ -267,7 +267,7 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
|
||||||
*/
|
*/
|
||||||
public MatchQueryBuilder prefixLength(int prefixLength) {
|
public MatchQueryBuilder prefixLength(int prefixLength) {
|
||||||
if (prefixLength < 0 ) {
|
if (prefixLength < 0 ) {
|
||||||
throw new IllegalArgumentException("No negative prefix length allowed.");
|
throw new IllegalArgumentException("[" + NAME + "] requires prefix length to be non-negative.");
|
||||||
}
|
}
|
||||||
this.prefixLength = prefixLength;
|
this.prefixLength = prefixLength;
|
||||||
return this;
|
return this;
|
||||||
|
@ -284,8 +284,8 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
|
||||||
* When using fuzzy or prefix type query, the number of term expansions to use.
|
* When using fuzzy or prefix type query, the number of term expansions to use.
|
||||||
*/
|
*/
|
||||||
public MatchQueryBuilder maxExpansions(int maxExpansions) {
|
public MatchQueryBuilder maxExpansions(int maxExpansions) {
|
||||||
if (maxExpansions < 0 ) {
|
if (maxExpansions <= 0 ) {
|
||||||
throw new IllegalArgumentException("No negative maxExpansions allowed.");
|
throw new IllegalArgumentException("[" + NAME + "] requires maxExpansions to be positive.");
|
||||||
}
|
}
|
||||||
this.maxExpansions = maxExpansions;
|
this.maxExpansions = maxExpansions;
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -23,7 +23,6 @@ import org.apache.lucene.queries.ExtendedCommonTermsQuery;
|
||||||
import org.apache.lucene.search.BooleanClause;
|
import org.apache.lucene.search.BooleanClause;
|
||||||
import org.apache.lucene.search.BooleanQuery;
|
import org.apache.lucene.search.BooleanQuery;
|
||||||
import org.apache.lucene.search.FuzzyQuery;
|
import org.apache.lucene.search.FuzzyQuery;
|
||||||
import org.apache.lucene.search.LegacyNumericRangeQuery;
|
|
||||||
import org.apache.lucene.search.MatchAllDocsQuery;
|
import org.apache.lucene.search.MatchAllDocsQuery;
|
||||||
import org.apache.lucene.search.PhraseQuery;
|
import org.apache.lucene.search.PhraseQuery;
|
||||||
import org.apache.lucene.search.PointRangeQuery;
|
import org.apache.lucene.search.PointRangeQuery;
|
||||||
|
@ -202,64 +201,46 @@ public class MatchQueryBuilderTests extends AbstractQueryTestCase<MatchQueryBuil
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testIllegalValues() {
|
public void testIllegalValues() {
|
||||||
try {
|
{
|
||||||
new MatchQueryBuilder(null, "value");
|
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new MatchQueryBuilder(null, "value"));
|
||||||
fail("value must not be non-null");
|
assertEquals("[match] requires fieldName", e.getMessage());
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
// expected
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
{
|
||||||
new MatchQueryBuilder("fieldName", null);
|
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new MatchQueryBuilder("fieldName", null));
|
||||||
fail("value must not be non-null");
|
assertEquals("[match] requires query value", e.getMessage());
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
// expected
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MatchQueryBuilder matchQuery = new MatchQueryBuilder("fieldName", "text");
|
MatchQueryBuilder matchQuery = new MatchQueryBuilder("fieldName", "text");
|
||||||
try {
|
{
|
||||||
matchQuery.prefixLength(-1);
|
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> matchQuery.prefixLength(-1));
|
||||||
fail("must not be positive");
|
assertEquals("[match] requires prefix length to be non-negative.", e.getMessage());
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
// expected
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
{
|
||||||
matchQuery.maxExpansions(-1);
|
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
|
||||||
fail("must not be positive");
|
() -> matchQuery.maxExpansions(randomIntBetween(-10, 0)));
|
||||||
} catch (IllegalArgumentException ex) {
|
assertEquals("[match] requires maxExpansions to be positive.", e.getMessage());
|
||||||
// expected
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
{
|
||||||
matchQuery.operator(null);
|
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> matchQuery.operator(null));
|
||||||
fail("must not be non-null");
|
assertEquals("[match] requires operator to be non-null", e.getMessage());
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
// expected
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
{
|
||||||
matchQuery.type(null);
|
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> matchQuery.type(null));
|
||||||
fail("must not be non-null");
|
assertEquals("[match] requires type to be non-null", e.getMessage());
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
// expected
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
{
|
||||||
matchQuery.zeroTermsQuery(null);
|
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> matchQuery.zeroTermsQuery(null));
|
||||||
fail("must not be non-null");
|
assertEquals("[match] requires zeroTermsQuery to be non-null", e.getMessage());
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
// expected
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void testBadAnalyzer() throws IOException {
|
|
||||||
MatchQueryBuilder matchQuery = new MatchQueryBuilder("fieldName", "text");
|
|
||||||
matchQuery.analyzer("bogusAnalyzer");
|
matchQuery.analyzer("bogusAnalyzer");
|
||||||
try {
|
{
|
||||||
matchQuery.toQuery(createShardContext());
|
QueryShardException e = expectThrows(QueryShardException.class, () -> matchQuery.toQuery(createShardContext()));
|
||||||
fail("Expected QueryShardException");
|
|
||||||
} catch (QueryShardException e) {
|
|
||||||
assertThat(e.getMessage(), containsString("analyzer [bogusAnalyzer] not found"));
|
assertThat(e.getMessage(), containsString("analyzer [bogusAnalyzer] not found"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue