mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-29 15:22:11 +00:00
DATAES-765 - Pageable.unpaged() is not used to build a query returning all documents.
Original PR: #408
This commit is contained in:
parent
f103bdb9d8
commit
745f9e9d79
@ -84,6 +84,10 @@ import org.springframework.util.StringUtils;
|
|||||||
* @since 4.0
|
* @since 4.0
|
||||||
*/
|
*/
|
||||||
class RequestFactory {
|
class RequestFactory {
|
||||||
|
|
||||||
|
// the default max result window size of Elasticsearch
|
||||||
|
static final Integer INDEX_MAX_RESULT_WINDOW = 10_000;
|
||||||
|
|
||||||
private final ElasticsearchConverter elasticsearchConverter;
|
private final ElasticsearchConverter elasticsearchConverter;
|
||||||
|
|
||||||
public RequestFactory(ElasticsearchConverter elasticsearchConverter) {
|
public RequestFactory(ElasticsearchConverter elasticsearchConverter) {
|
||||||
@ -557,6 +561,9 @@ class RequestFactory {
|
|||||||
if (query.getPageable().isPaged()) {
|
if (query.getPageable().isPaged()) {
|
||||||
sourceBuilder.from((int) query.getPageable().getOffset());
|
sourceBuilder.from((int) query.getPageable().getOffset());
|
||||||
sourceBuilder.size(query.getPageable().getPageSize());
|
sourceBuilder.size(query.getPageable().getPageSize());
|
||||||
|
} else {
|
||||||
|
sourceBuilder.from(0);
|
||||||
|
sourceBuilder.size(INDEX_MAX_RESULT_WINDOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!query.getFields().isEmpty()) {
|
if (!query.getFields().isEmpty()) {
|
||||||
@ -720,6 +727,9 @@ class RequestFactory {
|
|||||||
if (query.getPageable().isPaged()) {
|
if (query.getPageable().isPaged()) {
|
||||||
searchRequestBuilder.setFrom((int) query.getPageable().getOffset());
|
searchRequestBuilder.setFrom((int) query.getPageable().getOffset());
|
||||||
searchRequestBuilder.setSize(query.getPageable().getPageSize());
|
searchRequestBuilder.setSize(query.getPageable().getPageSize());
|
||||||
|
} else {
|
||||||
|
searchRequestBuilder.setFrom(0);
|
||||||
|
searchRequestBuilder.setSize(INDEX_MAX_RESULT_WINDOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!query.getFields().isEmpty()) {
|
if (!query.getFields().isEmpty()) {
|
||||||
|
@ -16,16 +16,25 @@
|
|||||||
package org.springframework.data.elasticsearch.core;
|
package org.springframework.data.elasticsearch.core;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
import static org.skyscreamer.jsonassert.JSONAssert.*;
|
import static org.skyscreamer.jsonassert.JSONAssert.*;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import org.elasticsearch.action.search.SearchAction;
|
||||||
import org.elasticsearch.action.search.SearchRequest;
|
import org.elasticsearch.action.search.SearchRequest;
|
||||||
|
import org.elasticsearch.action.search.SearchRequestBuilder;
|
||||||
|
import org.elasticsearch.client.Client;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
import org.springframework.core.convert.support.GenericConversionService;
|
import org.springframework.core.convert.support.GenericConversionService;
|
||||||
import org.springframework.data.annotation.Id;
|
import org.springframework.data.annotation.Id;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.data.elasticsearch.annotations.Field;
|
import org.springframework.data.elasticsearch.annotations.Field;
|
||||||
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
|
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
|
||||||
@ -35,18 +44,22 @@ import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMa
|
|||||||
import org.springframework.data.elasticsearch.core.query.Criteria;
|
import org.springframework.data.elasticsearch.core.query.Criteria;
|
||||||
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
|
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
|
||||||
import org.springframework.data.elasticsearch.core.query.GeoDistanceOrder;
|
import org.springframework.data.elasticsearch.core.query.GeoDistanceOrder;
|
||||||
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||||
|
import org.springframework.data.elasticsearch.core.query.Query;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Peter-Josef Meisch
|
* @author Peter-Josef Meisch
|
||||||
*/
|
*/
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
class RequestFactoryTests {
|
class RequestFactoryTests {
|
||||||
|
|
||||||
@Nullable private static RequestFactory requestFactory;
|
@Nullable private static RequestFactory requestFactory;
|
||||||
@Nullable private static MappingElasticsearchConverter converter;
|
@Nullable private static MappingElasticsearchConverter converter;
|
||||||
|
|
||||||
@BeforeAll
|
@Mock private Client client;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
static void setUpAll() {
|
static void setUpAll() {
|
||||||
SimpleElasticsearchMappingContext mappingContext = new SimpleElasticsearchMappingContext();
|
SimpleElasticsearchMappingContext mappingContext = new SimpleElasticsearchMappingContext();
|
||||||
mappingContext.setInitialEntitySet(Collections.singleton(Person.class));
|
mappingContext.setInitialEntitySet(Collections.singleton(Person.class));
|
||||||
@ -118,6 +131,28 @@ class RequestFactoryTests {
|
|||||||
assertThat(searchRequest.routing()).isEqualTo(route);
|
assertThat(searchRequest.routing()).isEqualTo(route);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // DATAES-765
|
||||||
|
void shouldAddMaxQueryWindowForUnpagedToRequest() {
|
||||||
|
Query query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withPageable(Pageable.unpaged()).build();
|
||||||
|
|
||||||
|
SearchRequest searchRequest = requestFactory.searchRequest(query, Person.class, IndexCoordinates.of("persons"));
|
||||||
|
|
||||||
|
assertThat(searchRequest.source().from()).isEqualTo(0);
|
||||||
|
assertThat(searchRequest.source().size()).isEqualTo(RequestFactory.INDEX_MAX_RESULT_WINDOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test // DATAES-765
|
||||||
|
void shouldAddMaxQueryWindowForUnpagedToRequestBuilder() {
|
||||||
|
when(client.prepareSearch(any())).thenReturn(new SearchRequestBuilder(client, SearchAction.INSTANCE));
|
||||||
|
Query query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withPageable(Pageable.unpaged()).build();
|
||||||
|
|
||||||
|
SearchRequestBuilder searchRequestBuilder = requestFactory.searchRequestBuilder(client, query, Person.class,
|
||||||
|
IndexCoordinates.of("persons"));
|
||||||
|
|
||||||
|
assertThat(searchRequestBuilder.request().source().from()).isEqualTo(0);
|
||||||
|
assertThat(searchRequestBuilder.request().source().size()).isEqualTo(RequestFactory.INDEX_MAX_RESULT_WINDOW);
|
||||||
|
}
|
||||||
|
|
||||||
static class Person {
|
static class Person {
|
||||||
@Nullable @Id String id;
|
@Nullable @Id String id;
|
||||||
@Nullable @Field(name = "last-name") String lastName;
|
@Nullable @Field(name = "last-name") String lastName;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user