diff --git a/server/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java b/server/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java index 394b8bbe65d38..23764a18a3e1c 100644 --- a/server/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java +++ b/server/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java @@ -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; } diff --git a/server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java index 597d81215c3a3..4e94b9e6327a7 100644 --- a/server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java @@ -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; } diff --git a/server/src/test/java/org/elasticsearch/common/lucene/search/morelikethis/MoreLikeThisQueryTests.java b/server/src/test/java/org/elasticsearch/common/lucene/search/morelikethis/MoreLikeThisQueryTests.java index fb09ceb839c37..337c61243baa3 100644 --- a/server/src/test/java/org/elasticsearch/common/lucene/search/morelikethis/MoreLikeThisQueryTests.java +++ b/server/src/test/java/org/elasticsearch/common/lucene/search/morelikethis/MoreLikeThisQueryTests.java @@ -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 { @@ -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")); + } + } diff --git a/server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java index 8099f9243466f..8a9ced02fb920 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java @@ -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)); @@ -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();