diff --git a/src/main/java/org/elasticsearch/action/mlt/TransportMoreLikeThisAction.java b/src/main/java/org/elasticsearch/action/mlt/TransportMoreLikeThisAction.java index 705e00c23c6..b2dbc65a23c 100644 --- a/src/main/java/org/elasticsearch/action/mlt/TransportMoreLikeThisAction.java +++ b/src/main/java/org/elasticsearch/action/mlt/TransportMoreLikeThisAction.java @@ -45,6 +45,7 @@ import org.elasticsearch.index.mapper.internal.SourceFieldMapper; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.MoreLikeThisFieldQueryBuilder; import org.elasticsearch.indices.IndicesService; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.*; @@ -196,22 +197,25 @@ public class TransportMoreLikeThisAction extends TransportAction() { @Override public void onResponse(SearchResponse response) { diff --git a/src/test/java/org/elasticsearch/mlt/MoreLikeThisActionTests.java b/src/test/java/org/elasticsearch/mlt/MoreLikeThisActionTests.java index 74c087b75fe..32fe084b820 100644 --- a/src/test/java/org/elasticsearch/mlt/MoreLikeThisActionTests.java +++ b/src/test/java/org/elasticsearch/mlt/MoreLikeThisActionTests.java @@ -20,14 +20,19 @@ package org.elasticsearch.mlt; import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; +import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.search.SearchPhaseExecutionException; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.junit.Test; +import java.util.ArrayList; +import java.util.List; + import static org.elasticsearch.client.Requests.*; import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS; import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS; @@ -292,4 +297,48 @@ public class MoreLikeThisActionTests extends ElasticsearchIntegrationTest { assertSearchHits(mltResponse, "2"); } + @Test + public void testMoreLikeThisBodyFromSize() throws Exception { + logger.info("Creating index test"); + assertAcked(prepareCreate("test").addMapping("type1", + jsonBuilder().startObject().startObject("type1").startObject("properties") + .startObject("text").field("type", "string").endObject() + .endObject().endObject().endObject())); + + logger.info("Running Cluster Health"); + assertThat(ensureGreen(), equalTo(ClusterHealthStatus.GREEN)); + + logger.info("Indexing..."); + List builders = new ArrayList<>(10); + for (int i = 1; i <= 10; i++) { + builders.add(client().prepareIndex("test", "type1").setSource("text", "lucene").setId(String.valueOf(i))); + } + indexRandom(true, builders); + + logger.info("'size' set but 'search_from' and 'search_size' kept to defaults"); + SearchResponse mltResponse = client().moreLikeThis( + moreLikeThisRequest("test").type("type1").id("1").minTermFreq(1).minDocFreq(1).include(true) + .searchSource(SearchSourceBuilder.searchSource().size(5))) + .actionGet(); + assertSearchResponse(mltResponse); + assertEquals(mltResponse.getHits().hits().length, 5); + + logger.info("'from' set but 'search_from' and 'search_size' kept to defaults"); + mltResponse = client().moreLikeThis( + moreLikeThisRequest("test").type("type1").id("1").minTermFreq(1).minDocFreq(1).include(true) + .searchSource(SearchSourceBuilder.searchSource().from(5))) + .actionGet(); + assertSearchResponse(mltResponse); + assertEquals(mltResponse.getHits().hits().length, 5); + + logger.info("When set, 'search_from' and 'search_size' should override 'from' and 'size'"); + mltResponse = client().moreLikeThis( + moreLikeThisRequest("test").type("type1").id("1").minTermFreq(1).minDocFreq(1).include(true) + .searchSize(10).searchFrom(2) + .searchSource(SearchSourceBuilder.searchSource().size(1).from(1))) + .actionGet(); + assertSearchResponse(mltResponse); + assertEquals(mltResponse.getHits().hits().length, 8); + } + }