Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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