Skip to content

Commit 975a8dd

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

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

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

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

281281
MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = requestFactory.moreLikeThisQueryBuilder(query, index);
282-
return search(new NativeSearchQueryBuilder().withQuery(moreLikeThisQueryBuilder).build(), clazz, index);
282+
return search(new NativeSearchQueryBuilder().withQuery(moreLikeThisQueryBuilder).withPageable(query.getPageable()).build(), clazz, index);
283283
}
284284

285285
@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-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2020 the original author or authors.
2+
* Copyright 2014-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import static org.elasticsearch.index.query.QueryBuilders.*;
2222
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
2323
import static org.springframework.data.elasticsearch.core.document.Document.*;
24+
import static org.springframework.data.elasticsearch.utils.IdGenerator.*;
2425
import static org.springframework.data.elasticsearch.utils.IndexBuilder.*;
2526

2627
import lombok.AllArgsConstructor;
@@ -41,6 +42,7 @@
4142
import java.util.Map;
4243
import java.util.UUID;
4344
import java.util.stream.Collectors;
45+
import java.util.stream.IntStream;
4446

4547
import org.assertj.core.util.Lists;
4648
import org.elasticsearch.action.search.SearchRequest;
@@ -1076,6 +1078,48 @@ public void shouldReturnSimilarResultsGivenMoreLikeThisQuery() {
10761078
assertThat(content).contains(sampleEntity);
10771079
}
10781080

1081+
@Test // #1787
1082+
void shouldUsePageableOnMoreLikeThisQueries() {
1083+
1084+
String sampleMessage = "So we build a web site or an application and want to add search to it, "
1085+
+ "and then it hits us: getting search working is hard. We want our search solution to be fast,"
1086+
+ " we want a painless setup and a completely free search schema, we want to be able to index data simply using JSON over HTTP, "
1087+
+ "we want our search server to be always available, we want to be able to start with one machine and scale to hundreds, "
1088+
+ "we want real-time search, we want simple multi-tenancy, and we want a solution that is built for the cloud.";
1089+
String referenceId = nextIdAsString();
1090+
Collection<String> ids = IntStream.rangeClosed(1, 10).mapToObj(i -> nextIdAsString()).collect(Collectors.toList());
1091+
ids.add(referenceId);
1092+
ids.stream()
1093+
.map(id -> getIndexQuery(SampleEntity.builder().id(id).message(sampleMessage).version(System.currentTimeMillis()).build()))
1094+
.forEach(indexQuery -> operations.index(indexQuery, index));
1095+
indexOperations.refresh();
1096+
1097+
MoreLikeThisQuery moreLikeThisQuery = new MoreLikeThisQuery();
1098+
moreLikeThisQuery.setId(referenceId);
1099+
moreLikeThisQuery.addFields("message");
1100+
moreLikeThisQuery.setMinDocFreq(1);
1101+
moreLikeThisQuery.setPageable(PageRequest.of(0, 5));
1102+
1103+
SearchHits<SampleEntity> searchHits = operations.search(moreLikeThisQuery, SampleEntity.class, index);
1104+
1105+
assertThat(searchHits.getTotalHits()).isEqualTo(10);
1106+
assertThat(searchHits.getSearchHits()).hasSize(5);
1107+
1108+
Collection<String> returnedIds = searchHits.getSearchHits().stream().map(SearchHit::getId).collect(Collectors.toList());
1109+
1110+
moreLikeThisQuery.setPageable(PageRequest.of(1, 5));
1111+
1112+
searchHits = operations.search(moreLikeThisQuery, SampleEntity.class, index);
1113+
1114+
assertThat(searchHits.getTotalHits()).isEqualTo(10);
1115+
assertThat(searchHits.getSearchHits()).hasSize(5);
1116+
1117+
searchHits.getSearchHits().stream().map(SearchHit::getId).forEach(returnedIds::add);
1118+
1119+
assertThat(returnedIds).hasSize(10);
1120+
assertThat(ids).containsAll(returnedIds);
1121+
}
1122+
10791123
@Test // DATAES-167
10801124
public void shouldReturnResultsWithScanAndScrollForGivenCriteriaQuery() {
10811125

0 commit comments

Comments
 (0)