Add Validation for maxQueryTerms to be greater than 0 for MoreLikeThisQuery (#49966)

Adds validation for maxQueryTerms to be greater than 0 for MoreLikeThisQuery 
and MoreLikeThisQueryBuilder.

Closes #49927
This commit is contained in:
Vishnu Chilamakuru 2019-12-09 19:09:05 +05:30 committed by Christoph Büscher
parent 1918a21baf
commit 056c698540
4 changed files with 29 additions and 1 deletions

View File

@ -308,6 +308,9 @@ public class MoreLikeThisQuery extends Query {
} }
public void setMaxQueryTerms(int maxQueryTerms) { public void setMaxQueryTerms(int maxQueryTerms) {
if (maxQueryTerms <= 0) {
throw new IllegalArgumentException("requires 'maxQueryTerms' to be greater than 0");
}
this.maxQueryTerms = maxQueryTerms; this.maxQueryTerms = maxQueryTerms;
} }

View File

@ -641,6 +641,9 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
* Defaults to {@code 25}. * Defaults to {@code 25}.
*/ */
public MoreLikeThisQueryBuilder maxQueryTerms(int maxQueryTerms) { public MoreLikeThisQueryBuilder maxQueryTerms(int maxQueryTerms) {
if (maxQueryTerms <= 0) {
throw new IllegalArgumentException("requires 'maxQueryTerms' to be greater than 0");
}
this.maxQueryTerms = maxQueryTerms; this.maxQueryTerms = maxQueryTerms;
return this; return this;
} }

View File

@ -33,6 +33,7 @@ import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.lucene.search.MoreLikeThisQuery; import org.elasticsearch.common.lucene.search.MoreLikeThisQuery;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
public class MoreLikeThisQueryTests extends ESTestCase { public class MoreLikeThisQueryTests extends ESTestCase {
@ -64,4 +65,15 @@ public class MoreLikeThisQueryTests extends ESTestCase {
reader.close(); reader.close();
indexWriter.close(); indexWriter.close();
} }
public void testValidateMaxQueryTerms() {
IllegalArgumentException e1 = expectThrows(IllegalArgumentException.class,
() -> new MoreLikeThisQuery("lucene", new String[]{"text"}, Lucene.STANDARD_ANALYZER).setMaxQueryTerms(0));
assertThat(e1.getMessage(), containsString("requires 'maxQueryTerms' to be greater than 0"));
IllegalArgumentException e2 = expectThrows(IllegalArgumentException.class,
() -> new MoreLikeThisQuery("lucene", new String[]{"text"}, Lucene.STANDARD_ANALYZER).setMaxQueryTerms(-3));
assertThat(e2.getMessage(), containsString("requires 'maxQueryTerms' to be greater than 0"));
}
} }

View File

@ -174,7 +174,7 @@ public class MoreLikeThisQueryBuilderTests extends AbstractQueryTestCase<MoreLik
queryBuilder.unlike(randomUnlikeItems); queryBuilder.unlike(randomUnlikeItems);
} }
if (randomBoolean()) { if (randomBoolean()) {
queryBuilder.maxQueryTerms(randomInt(25)); queryBuilder.maxQueryTerms(randomIntBetween(1, 25));
} }
if (randomBoolean()) { if (randomBoolean()) {
queryBuilder.minTermFreq(randomInt(5)); queryBuilder.minTermFreq(randomInt(5));
@ -351,6 +351,16 @@ public class MoreLikeThisQueryBuilderTests extends AbstractQueryTestCase<MoreLik
assertThat(mltQuery.getMaxQueryTerms(), equalTo(12)); assertThat(mltQuery.getMaxQueryTerms(), equalTo(12));
} }
public void testValidateMaxQueryTerms() {
IllegalArgumentException e1 = expectThrows(IllegalArgumentException.class,
() -> new MoreLikeThisQueryBuilder(new String[]{"name.first", "name.last"}, new String[]{"something"}, null).maxQueryTerms(0));
assertThat(e1.getMessage(), containsString("requires 'maxQueryTerms' to be greater than 0"));
IllegalArgumentException e2 = expectThrows(IllegalArgumentException.class,
() -> new MoreLikeThisQueryBuilder(new String[]{"name.first", "name.last"}, new String[]{"something"}, null).maxQueryTerms(-3));
assertThat(e2.getMessage(), containsString("requires 'maxQueryTerms' to be greater than 0"));
}
public void testItemSerialization() throws IOException { public void testItemSerialization() throws IOException {
Item expectedItem = generateRandomItem(); Item expectedItem = generateRandomItem();
BytesStreamOutput output = new BytesStreamOutput(); BytesStreamOutput output = new BytesStreamOutput();