Search with MoreLikeThisQuery should use Pageable.

Original Pull Request #1789
Closes #1787
This commit is contained in:
Peter-Josef Meisch 2021-04-26 22:26:39 +02:00 committed by GitHub
parent 91742b1114
commit a2ca312fb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 1 deletions

View File

@ -419,7 +419,7 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
Assert.notNull(query.getId(), "No document id defined for MoreLikeThisQuery");
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

View File

@ -23,6 +23,7 @@ import java.util.List;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* MoreLikeThisQuery
@ -176,6 +177,9 @@ public class MoreLikeThisQuery {
}
public void setPageable(Pageable pageable) {
Assert.notNull(pageable, "pageable must not be null");
this.pageable = pageable;
}
}

View File

@ -1113,6 +1113,49 @@ public abstract class ElasticsearchTemplateTests {
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
public void shouldReturnResultsWithScanAndScrollForGivenCriteriaQuery() {
@ -3545,6 +3588,7 @@ public abstract class ElasticsearchTemplateTests {
assertThat(explanation).isNotNull();
}
// region entities
@Document(indexName = INDEX_NAME_SAMPLE_ENTITY)
@Setting(shards = 1, replicas = 0, refreshInterval = "-1")
static class SampleEntity {
@ -4322,4 +4366,5 @@ public abstract class ElasticsearchTemplateTests {
this.text = text;
}
}
//endregion
}