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 394b8bbe65d..23764a18a3e 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 class MoreLikeThisQuery extends Query { } 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 120036f5484..4c7823c8029 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 class MoreLikeThisQueryBuilder extends AbstractQueryBuilder 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 9b588eb2f16..c17422e3f31 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 @@ public class MoreLikeThisQueryBuilderTests extends AbstractQueryTestCase 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();