mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-05-30 16:52:11 +00:00
Search with MoreLikeThisQuery should use Pageable.
Original Pull Request #1789 Closes #1787
This commit is contained in:
parent
91742b1114
commit
a2ca312fb2
@ -419,7 +419,7 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
|||||||
Assert.notNull(query.getId(), "No document id defined for MoreLikeThisQuery");
|
Assert.notNull(query.getId(), "No document id defined for MoreLikeThisQuery");
|
||||||
|
|
||||||
MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = requestFactory.moreLikeThisQueryBuilder(query, index);
|
MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = requestFactory.moreLikeThisQueryBuilder(query, index);
|
||||||
return search(new NativeSearchQueryBuilder().withQuery(moreLikeThisQueryBuilder).build(), clazz, index);
|
return search(new NativeSearchQueryBuilder().withQuery(moreLikeThisQueryBuilder).withPageable(query.getPageable()).build(), clazz, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -23,6 +23,7 @@ import java.util.List;
|
|||||||
|
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MoreLikeThisQuery
|
* MoreLikeThisQuery
|
||||||
@ -176,6 +177,9 @@ public class MoreLikeThisQuery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setPageable(Pageable pageable) {
|
public void setPageable(Pageable pageable) {
|
||||||
|
|
||||||
|
Assert.notNull(pageable, "pageable must not be null");
|
||||||
|
|
||||||
this.pageable = pageable;
|
this.pageable = pageable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1113,6 +1113,49 @@ public abstract class ElasticsearchTemplateTests {
|
|||||||
assertThat(content).contains(sampleEntity);
|
assertThat(content).contains(sampleEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // #1787
|
||||||
|
@DisplayName("should use Pageable on MoreLikeThis queries")
|
||||||
|
void shouldUsePageableOnMoreLikeThisQueries() {
|
||||||
|
|
||||||
|
String sampleMessage = "So we build a web site or an application and want to add search to it, "
|
||||||
|
+ "and then it hits us: getting search working is hard. We want our search solution to be fast,"
|
||||||
|
+ " we want a painless setup and a completely free search schema, we want to be able to index data simply using JSON over HTTP, "
|
||||||
|
+ "we want our search server to be always available, we want to be able to start with one machine and scale to hundreds, "
|
||||||
|
+ "we want real-time search, we want simple multi-tenancy, and we want a solution that is built for the cloud.";
|
||||||
|
String referenceId = nextIdAsString();
|
||||||
|
Collection<String> ids = IntStream.rangeClosed(1, 10).mapToObj(i -> nextIdAsString()).collect(Collectors.toList());
|
||||||
|
ids.add(referenceId);
|
||||||
|
ids.stream()
|
||||||
|
.map(id -> getIndexQuery(SampleEntity.builder().id(id).message(sampleMessage).version(System.currentTimeMillis()).build()))
|
||||||
|
.forEach(indexQuery -> operations.index(indexQuery, index));
|
||||||
|
indexOperations.refresh();
|
||||||
|
|
||||||
|
MoreLikeThisQuery moreLikeThisQuery = new MoreLikeThisQuery();
|
||||||
|
moreLikeThisQuery.setId(referenceId);
|
||||||
|
moreLikeThisQuery.addFields("message");
|
||||||
|
moreLikeThisQuery.setMinDocFreq(1);
|
||||||
|
moreLikeThisQuery.setPageable(PageRequest.of(0, 5));
|
||||||
|
|
||||||
|
SearchHits<SampleEntity> searchHits = operations.search(moreLikeThisQuery, SampleEntity.class, index);
|
||||||
|
|
||||||
|
assertThat(searchHits.getTotalHits()).isEqualTo(10);
|
||||||
|
assertThat(searchHits.getSearchHits()).hasSize(5);
|
||||||
|
|
||||||
|
Collection<String> returnedIds = searchHits.getSearchHits().stream().map(SearchHit::getId).collect(Collectors.toList());
|
||||||
|
|
||||||
|
moreLikeThisQuery.setPageable(PageRequest.of(1, 5));
|
||||||
|
|
||||||
|
searchHits = operations.search(moreLikeThisQuery, SampleEntity.class, index);
|
||||||
|
|
||||||
|
assertThat(searchHits.getTotalHits()).isEqualTo(10);
|
||||||
|
assertThat(searchHits.getSearchHits()).hasSize(5);
|
||||||
|
|
||||||
|
searchHits.getSearchHits().stream().map(SearchHit::getId).forEach(returnedIds::add);
|
||||||
|
|
||||||
|
assertThat(returnedIds).hasSize(10);
|
||||||
|
assertThat(ids).containsAll(returnedIds);
|
||||||
|
}
|
||||||
|
|
||||||
@Test // DATAES-167
|
@Test // DATAES-167
|
||||||
public void shouldReturnResultsWithScanAndScrollForGivenCriteriaQuery() {
|
public void shouldReturnResultsWithScanAndScrollForGivenCriteriaQuery() {
|
||||||
|
|
||||||
@ -3545,6 +3588,7 @@ public abstract class ElasticsearchTemplateTests {
|
|||||||
assertThat(explanation).isNotNull();
|
assertThat(explanation).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// region entities
|
||||||
@Document(indexName = INDEX_NAME_SAMPLE_ENTITY)
|
@Document(indexName = INDEX_NAME_SAMPLE_ENTITY)
|
||||||
@Setting(shards = 1, replicas = 0, refreshInterval = "-1")
|
@Setting(shards = 1, replicas = 0, refreshInterval = "-1")
|
||||||
static class SampleEntity {
|
static class SampleEntity {
|
||||||
@ -4322,4 +4366,5 @@ public abstract class ElasticsearchTemplateTests {
|
|||||||
this.text = text;
|
this.text = text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//endregion
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user