Skip to content

Commit 85af546

Browse files
committed
Search with MoreLikeThisQuery should use Pageable.
Original Pull Request #1789 Closes #1787 (cherry picked from commit a2ca312)
1 parent 105607f commit 85af546

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

Diff for: src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ public <T> SearchHits<T> search(MoreLikeThisQuery query, Class<T> clazz, IndexCo
419419
Assert.notNull(query.getId(), "No document id defined for MoreLikeThisQuery");
420420

421421
MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = requestFactory.moreLikeThisQueryBuilder(query, index);
422-
return search(new NativeSearchQueryBuilder().withQuery(moreLikeThisQueryBuilder).build(), clazz, index);
422+
return search(new NativeSearchQueryBuilder().withQuery(moreLikeThisQueryBuilder).withPageable(query.getPageable()).build(), clazz, index);
423423
}
424424

425425
@Override

Diff for: src/main/java/org/springframework/data/elasticsearch/core/query/MoreLikeThisQuery.java

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.data.domain.Pageable;
2525
import org.springframework.lang.Nullable;
26+
import org.springframework.util.Assert;
2627

2728
/**
2829
* MoreLikeThisQuery
@@ -176,6 +177,9 @@ public Pageable getPageable() {
176177
}
177178

178179
public void setPageable(Pageable pageable) {
180+
181+
Assert.notNull(pageable, "pageable must not be null");
182+
179183
this.pageable = pageable;
180184
}
181185
}

Diff for: src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java

+45
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,49 @@ public void shouldReturnSimilarResultsGivenMoreLikeThisQuery() {
11161116
assertThat(content).contains(sampleEntity);
11171117
}
11181118

1119+
@Test // #1787
1120+
@DisplayName("should use Pageable on MoreLikeThis queries")
1121+
void shouldUsePageableOnMoreLikeThisQueries() {
1122+
1123+
String sampleMessage = "So we build a web site or an application and want to add search to it, "
1124+
+ "and then it hits us: getting search working is hard. We want our search solution to be fast,"
1125+
+ " we want a painless setup and a completely free search schema, we want to be able to index data simply using JSON over HTTP, "
1126+
+ "we want our search server to be always available, we want to be able to start with one machine and scale to hundreds, "
1127+
+ "we want real-time search, we want simple multi-tenancy, and we want a solution that is built for the cloud.";
1128+
String referenceId = nextIdAsString();
1129+
Collection<String> ids = IntStream.rangeClosed(1, 10).mapToObj(i -> nextIdAsString()).collect(Collectors.toList());
1130+
ids.add(referenceId);
1131+
ids.stream()
1132+
.map(id -> getIndexQuery(SampleEntity.builder().id(id).message(sampleMessage).version(System.currentTimeMillis()).build()))
1133+
.forEach(indexQuery -> operations.index(indexQuery, index));
1134+
indexOperations.refresh();
1135+
1136+
MoreLikeThisQuery moreLikeThisQuery = new MoreLikeThisQuery();
1137+
moreLikeThisQuery.setId(referenceId);
1138+
moreLikeThisQuery.addFields("message");
1139+
moreLikeThisQuery.setMinDocFreq(1);
1140+
moreLikeThisQuery.setPageable(PageRequest.of(0, 5));
1141+
1142+
SearchHits<SampleEntity> searchHits = operations.search(moreLikeThisQuery, SampleEntity.class, index);
1143+
1144+
assertThat(searchHits.getTotalHits()).isEqualTo(10);
1145+
assertThat(searchHits.getSearchHits()).hasSize(5);
1146+
1147+
Collection<String> returnedIds = searchHits.getSearchHits().stream().map(SearchHit::getId).collect(Collectors.toList());
1148+
1149+
moreLikeThisQuery.setPageable(PageRequest.of(1, 5));
1150+
1151+
searchHits = operations.search(moreLikeThisQuery, SampleEntity.class, index);
1152+
1153+
assertThat(searchHits.getTotalHits()).isEqualTo(10);
1154+
assertThat(searchHits.getSearchHits()).hasSize(5);
1155+
1156+
searchHits.getSearchHits().stream().map(SearchHit::getId).forEach(returnedIds::add);
1157+
1158+
assertThat(returnedIds).hasSize(10);
1159+
assertThat(ids).containsAll(returnedIds);
1160+
}
1161+
11191162
@Test // DATAES-167
11201163
public void shouldReturnResultsWithScanAndScrollForGivenCriteriaQuery() {
11211164

@@ -3751,6 +3794,7 @@ void shouldReturnExplanationWhenRequested() {
37513794
assertThat(explanation).isNotNull();
37523795
}
37533796

3797+
// region entities
37543798
@Document(indexName = INDEX_NAME_SAMPLE_ENTITY)
37553799
@Setting(shards = 1, replicas = 0, refreshInterval = "-1")
37563800
static class SampleEntity {
@@ -4528,4 +4572,5 @@ public void setText(@Nullable String text) {
45284572
this.text = text;
45294573
}
45304574
}
4575+
//endregion
45314576
}

0 commit comments

Comments
 (0)