More Like This: Fix correct use of size and from parameters
More Like This API would not take into account 'size' and 'from' in request body parameters. Instead these values would always be overriden by the default values of REST parameters 'search_size' and 'search_from'. Closes #5981
This commit is contained in:
parent
d4d6c3459e
commit
513f25ae97
|
@ -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<MoreLikeThisReq
|
|||
if (searchTypes == null) {
|
||||
searchTypes = new String[]{request.type()};
|
||||
}
|
||||
int size = request.searchSize() != 0 ? request.searchSize() : 10;
|
||||
int from = request.searchFrom() != 0 ? request.searchFrom() : 0;
|
||||
SearchRequest searchRequest = searchRequest(searchIndices)
|
||||
.types(searchTypes)
|
||||
.searchType(request.searchType())
|
||||
.scroll(request.searchScroll())
|
||||
.extraSource(searchSource()
|
||||
.query(boolBuilder)
|
||||
.from(from)
|
||||
.size(size)
|
||||
)
|
||||
.listenerThreaded(request.listenerThreaded());
|
||||
|
||||
SearchSourceBuilder extraSource = searchSource().query(boolBuilder);
|
||||
if (request.searchFrom() != 0) {
|
||||
extraSource.from(request.searchFrom());
|
||||
}
|
||||
if (request.searchSize() != 0) {
|
||||
extraSource.size(request.searchSize());
|
||||
}
|
||||
searchRequest.extraSource(extraSource);
|
||||
|
||||
if (request.searchSource() != null) {
|
||||
searchRequest.source(request.searchSource(), request.searchSourceUnsafe());
|
||||
}
|
||||
|
||||
searchAction.execute(searchRequest, new ActionListener<SearchResponse>() {
|
||||
@Override
|
||||
public void onResponse(SearchResponse response) {
|
||||
|
|
|
@ -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<IndexRequestBuilder> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue