mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-22 03:52:10 +00:00
bug fix : custom repository methods should work with pageable request
This commit is contained in:
parent
23e7a0a9b6
commit
0d072ab114
@ -47,7 +47,8 @@ public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery
|
||||
ParametersParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters);
|
||||
CriteriaQuery query = createQuery(accessor);
|
||||
if(queryMethod.isPageQuery()){
|
||||
return elasticsearchOperations.queryForPage(query, queryMethod.getEntityInformation().getJavaType());
|
||||
query.setPageable(accessor.getPageable());
|
||||
return elasticsearchOperations.queryForPage(query, queryMethod.getEntityInformation().getJavaType());
|
||||
}
|
||||
return elasticsearchOperations.queryForObject(query, queryMethod.getEntityInformation().getJavaType());
|
||||
}
|
||||
|
@ -63,6 +63,7 @@ public class ElasticsearchStringQuery extends AbstractElasticsearchRepositoryQue
|
||||
ParametersParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters);
|
||||
StringQuery stringQuery = createQuery(accessor);
|
||||
if(queryMethod.isPageQuery()){
|
||||
stringQuery.setPageable(accessor.getPageable());
|
||||
return elasticsearchOperations.queryForPage(stringQuery, queryMethod.getEntityInformation().getJavaType());
|
||||
}
|
||||
return elasticsearchOperations.queryForObject(stringQuery, queryMethod.getEntityInformation().getJavaType());
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
@ -23,6 +24,7 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
||||
@Document(indexName = "book",type = "book")
|
||||
public class Book {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
private String name;
|
||||
private Author author;
|
||||
|
@ -23,6 +23,7 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.elasticsearch.SampleEntity;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
|
||||
@ -34,6 +35,7 @@ import javax.annotation.Resource;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.apache.commons.lang.RandomStringUtils.random;
|
||||
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
@ -413,4 +415,52 @@ public class CustomMethodRepositoryTest {
|
||||
assertThat(page.getTotalElements(), is(equalTo(1L)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnPageableResultsWithQueryAnnotationExpectedPageSize() {
|
||||
// given
|
||||
for (int i = 0; i < 30; i++) {
|
||||
String documentId = String.valueOf(i);
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setId(documentId);
|
||||
sampleEntity.setMessage("message");
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
repository.save(sampleEntity);
|
||||
}
|
||||
// when
|
||||
Page<SampleEntity> pageResult = repository.findByMessage("message", new PageRequest(0, 23, new Sort(new Sort.Order(Sort.Direction.ASC,"message"))));
|
||||
// then
|
||||
assertThat(pageResult.getTotalElements(), is(equalTo(30L)));
|
||||
assertThat(pageResult.getContent().size(), is(equalTo(23)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnPageableResultsWithGivenSortingOrder(){
|
||||
//given
|
||||
String documentId = random(5);
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setId(documentId);
|
||||
sampleEntity.setMessage("abc");
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
repository.save(sampleEntity);
|
||||
|
||||
String documentId2 = randomNumeric(5);
|
||||
SampleEntity sampleEntity2 = new SampleEntity();
|
||||
sampleEntity2.setId(documentId2);
|
||||
sampleEntity2.setMessage("abd");
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
repository.save(sampleEntity2);
|
||||
|
||||
String documentId3 = randomNumeric(5);
|
||||
SampleEntity sampleEntity3 = new SampleEntity();
|
||||
sampleEntity3.setId(documentId3);
|
||||
sampleEntity3.setMessage("abe");
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
repository.save(sampleEntity3);
|
||||
//when
|
||||
Page<SampleEntity> pageResult = repository.findByMessageContaining("a", new PageRequest(0, 23, new Sort(new Sort.Order(Sort.Direction.DESC,"message"))));
|
||||
//then
|
||||
assertThat(pageResult.getContent().isEmpty(),is(false));
|
||||
assertThat(pageResult.getContent().get(0).getMessage(),is(sampleEntity3.getMessage()));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user