Skip to content

Commit

Permalink
Add Validation for maxQueryTerms to be greater than 0 for MoreLikeThi…
Browse files Browse the repository at this point in the history
…sQuery (elastic#49966)

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

Closes elastic#49927
  • Loading branch information
vishnuchilamakuru authored and SivagurunathanV committed Jan 21, 2020
1 parent dde9e59 commit 64b5044
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ public int getMaxQueryTerms() {
}

public void setMaxQueryTerms(int maxQueryTerms) {
if (maxQueryTerms <= 0) {
throw new IllegalArgumentException("requires 'maxQueryTerms' to be greater than 0");
}
this.maxQueryTerms = maxQueryTerms;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,9 @@ public Item[] unlikeItems() {
* Defaults to {@code 25}.
*/
public MoreLikeThisQueryBuilder maxQueryTerms(int maxQueryTerms) {
if (maxQueryTerms <= 0) {
throw new IllegalArgumentException("requires 'maxQueryTerms' to be greater than 0");
}
this.maxQueryTerms = maxQueryTerms;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.elasticsearch.common.lucene.search.MoreLikeThisQuery;
import org.elasticsearch.test.ESTestCase;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

public class MoreLikeThisQueryTests extends ESTestCase {
Expand Down Expand Up @@ -64,4 +65,15 @@ public void testSimple() throws Exception {
reader.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"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ protected MoreLikeThisQueryBuilder doCreateTestQueryBuilder() {
queryBuilder.unlike(randomUnlikeItems);
}
if (randomBoolean()) {
queryBuilder.maxQueryTerms(randomInt(25));
queryBuilder.maxQueryTerms(randomIntBetween(1, 25));
}
if (randomBoolean()) {
queryBuilder.minTermFreq(randomInt(5));
Expand Down Expand Up @@ -340,6 +340,16 @@ public void testMoreLikeThisBuilder() throws Exception {
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 {
Item expectedItem = generateRandomItem();
BytesStreamOutput output = new BytesStreamOutput();
Expand Down

0 comments on commit 64b5044

Please sign in to comment.