From 056c698540b1bb51eb09f28b4e51e7be7e98dbbc Mon Sep 17 00:00:00 2001 From: Vishnu Chilamakuru Date: Mon, 9 Dec 2019 19:09:05 +0530 Subject: [PATCH] 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 --- .../common/lucene/search/MoreLikeThisQuery.java | 3 +++ .../index/query/MoreLikeThisQueryBuilder.java | 3 +++ .../search/morelikethis/MoreLikeThisQueryTests.java | 12 ++++++++++++ .../index/query/MoreLikeThisQueryBuilderTests.java | 12 +++++++++++- 4 files changed, 29 insertions(+), 1 deletion(-) 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 120036f5484c4..4c7823c802958 100644 --- a/server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java @@ -641,6 +641,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 9b588eb2f167b..c17422e3f31a7 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java @@ -174,7 +174,7 @@ protected MoreLikeThisQueryBuilder doCreateTestQueryBuilder() { queryBuilder.unlike(randomUnlikeItems); } if (randomBoolean()) { - queryBuilder.maxQueryTerms(randomInt(25)); + queryBuilder.maxQueryTerms(randomIntBetween(1, 25)); } if (randomBoolean()) { queryBuilder.minTermFreq(randomInt(5)); @@ -351,6 +351,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();