|
1 | 1 | /*
|
2 |
| - * Copyright 2014-2020 the original author or authors. |
| 2 | + * Copyright 2014-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
21 | 21 | import static org.elasticsearch.index.query.QueryBuilders.*;
|
22 | 22 | import static org.springframework.data.elasticsearch.annotations.FieldType.*;
|
23 | 23 | import static org.springframework.data.elasticsearch.core.document.Document.*;
|
| 24 | +import static org.springframework.data.elasticsearch.utils.IdGenerator.*; |
24 | 25 | import static org.springframework.data.elasticsearch.utils.IndexBuilder.*;
|
25 | 26 |
|
26 | 27 | import lombok.AllArgsConstructor;
|
|
41 | 42 | import java.util.Map;
|
42 | 43 | import java.util.UUID;
|
43 | 44 | import java.util.stream.Collectors;
|
| 45 | +import java.util.stream.IntStream; |
44 | 46 |
|
45 | 47 | import org.assertj.core.util.Lists;
|
46 | 48 | import org.elasticsearch.action.search.SearchRequest;
|
@@ -1076,6 +1078,48 @@ public void shouldReturnSimilarResultsGivenMoreLikeThisQuery() {
|
1076 | 1078 | assertThat(content).contains(sampleEntity);
|
1077 | 1079 | }
|
1078 | 1080 |
|
| 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 | + |
1079 | 1123 | @Test // DATAES-167
|
1080 | 1124 | public void shouldReturnResultsWithScanAndScrollForGivenCriteriaQuery() {
|
1081 | 1125 |
|
|
0 commit comments