mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-04 09:42:25 +00:00
DATAES-51 - allowed to pass multiple sort builders for NativeSearchQuery
Can now add multiple sort builders to a NativeSearchQuery
This commit is contained in:
parent
1352c9f45b
commit
eafdbed68e
@ -57,6 +57,7 @@ import org.elasticsearch.index.query.QueryBuilders;
|
|||||||
import org.elasticsearch.search.SearchHit;
|
import org.elasticsearch.search.SearchHit;
|
||||||
import org.elasticsearch.search.facet.FacetBuilder;
|
import org.elasticsearch.search.facet.FacetBuilder;
|
||||||
import org.elasticsearch.search.highlight.HighlightBuilder;
|
import org.elasticsearch.search.highlight.HighlightBuilder;
|
||||||
|
import org.elasticsearch.search.sort.SortBuilder;
|
||||||
import org.elasticsearch.search.sort.SortOrder;
|
import org.elasticsearch.search.sort.SortOrder;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
@ -451,8 +452,10 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
|||||||
searchRequest.setPostFilter(searchQuery.getFilter());
|
searchRequest.setPostFilter(searchQuery.getFilter());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (searchQuery.getElasticsearchSort() != null) {
|
if (CollectionUtils.isNotEmpty(searchQuery.getElasticsearchSorts())) {
|
||||||
searchRequest.addSort(searchQuery.getElasticsearchSort());
|
for (SortBuilder sort : searchQuery.getElasticsearchSorts()) {
|
||||||
|
searchRequest.addSort(sort);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CollectionUtils.isNotEmpty(searchQuery.getFacets())) {
|
if (CollectionUtils.isNotEmpty(searchQuery.getFacets())) {
|
||||||
|
@ -35,7 +35,7 @@ public class NativeSearchQuery extends AbstractQuery implements SearchQuery {
|
|||||||
|
|
||||||
private QueryBuilder query;
|
private QueryBuilder query;
|
||||||
private FilterBuilder filter;
|
private FilterBuilder filter;
|
||||||
private SortBuilder sort;
|
private List<SortBuilder> sorts;
|
||||||
private List<FacetRequest> facets;
|
private List<FacetRequest> facets;
|
||||||
private HighlightBuilder.Field[] highlightFields;
|
private HighlightBuilder.Field[] highlightFields;
|
||||||
|
|
||||||
@ -49,16 +49,16 @@ public class NativeSearchQuery extends AbstractQuery implements SearchQuery {
|
|||||||
this.filter = filter;
|
this.filter = filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NativeSearchQuery(QueryBuilder query, FilterBuilder filter, SortBuilder sort) {
|
public NativeSearchQuery(QueryBuilder query, FilterBuilder filter, List<SortBuilder> sorts) {
|
||||||
this.query = query;
|
this.query = query;
|
||||||
this.filter = filter;
|
this.filter = filter;
|
||||||
this.sort = sort;
|
this.sorts = sorts;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NativeSearchQuery(QueryBuilder query, FilterBuilder filter, SortBuilder sort, HighlightBuilder.Field[] highlightFields) {
|
public NativeSearchQuery(QueryBuilder query, FilterBuilder filter, List<SortBuilder> sorts, HighlightBuilder.Field[] highlightFields) {
|
||||||
this.query = query;
|
this.query = query;
|
||||||
this.filter = filter;
|
this.filter = filter;
|
||||||
this.sort = sort;
|
this.sorts = sorts;
|
||||||
this.highlightFields = highlightFields;
|
this.highlightFields = highlightFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,8 +70,8 @@ public class NativeSearchQuery extends AbstractQuery implements SearchQuery {
|
|||||||
return filter;
|
return filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SortBuilder getElasticsearchSort() {
|
public List<SortBuilder> getElasticsearchSorts() {
|
||||||
return sort;
|
return sorts;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -38,7 +38,7 @@ public class NativeSearchQueryBuilder {
|
|||||||
|
|
||||||
private QueryBuilder queryBuilder;
|
private QueryBuilder queryBuilder;
|
||||||
private FilterBuilder filterBuilder;
|
private FilterBuilder filterBuilder;
|
||||||
private SortBuilder sortBuilder;
|
private List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
|
||||||
private List<FacetRequest> facetRequests = new ArrayList<FacetRequest>();
|
private List<FacetRequest> facetRequests = new ArrayList<FacetRequest>();
|
||||||
private HighlightBuilder.Field[] highlightFields;
|
private HighlightBuilder.Field[] highlightFields;
|
||||||
private Pageable pageable;
|
private Pageable pageable;
|
||||||
@ -58,7 +58,7 @@ public class NativeSearchQueryBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public NativeSearchQueryBuilder withSort(SortBuilder sortBuilder) {
|
public NativeSearchQueryBuilder withSort(SortBuilder sortBuilder) {
|
||||||
this.sortBuilder = sortBuilder;
|
this.sortBuilders.add(sortBuilder);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +98,7 @@ public class NativeSearchQueryBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public NativeSearchQuery build() {
|
public NativeSearchQuery build() {
|
||||||
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilder, highlightFields);
|
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders, highlightFields);
|
||||||
if (pageable != null) {
|
if (pageable != null) {
|
||||||
nativeSearchQuery.setPageable(pageable);
|
nativeSearchQuery.setPageable(pageable);
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ public interface SearchQuery extends Query {
|
|||||||
|
|
||||||
FilterBuilder getFilter();
|
FilterBuilder getFilter();
|
||||||
|
|
||||||
SortBuilder getElasticsearchSort();
|
List<SortBuilder> getElasticsearchSorts();
|
||||||
|
|
||||||
List<FacetRequest> getFacets();
|
List<FacetRequest> getFacets();
|
||||||
|
|
||||||
|
@ -322,6 +322,64 @@ public class ElasticsearchTemplateTests {
|
|||||||
assertThat(sampleEntities.getContent().get(0).getRate(), is(sampleEntity2.getRate()));
|
assertThat(sampleEntities.getContent().get(0).getRate(), is(sampleEntity2.getRate()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldSortResultsGivenMultipleSortCriteria() {
|
||||||
|
// given
|
||||||
|
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||||
|
// first document
|
||||||
|
String documentId = randomNumeric(5);
|
||||||
|
SampleEntity sampleEntity1 = new SampleEntity();
|
||||||
|
sampleEntity1.setId(documentId);
|
||||||
|
sampleEntity1.setMessage("abc");
|
||||||
|
sampleEntity1.setRate(15);
|
||||||
|
sampleEntity1.setVersion(System.currentTimeMillis());
|
||||||
|
|
||||||
|
IndexQuery indexQuery1 = new IndexQuery();
|
||||||
|
indexQuery1.setId(documentId);
|
||||||
|
indexQuery1.setObject(sampleEntity1);
|
||||||
|
|
||||||
|
// second document
|
||||||
|
String documentId2 = randomNumeric(5);
|
||||||
|
SampleEntity sampleEntity2 = new SampleEntity();
|
||||||
|
sampleEntity2.setId(documentId2);
|
||||||
|
sampleEntity2.setMessage("xyz");
|
||||||
|
sampleEntity2.setRate(5);
|
||||||
|
sampleEntity2.setVersion(System.currentTimeMillis());
|
||||||
|
|
||||||
|
IndexQuery indexQuery2 = new IndexQuery();
|
||||||
|
indexQuery2.setId(documentId2);
|
||||||
|
indexQuery2.setObject(sampleEntity2);
|
||||||
|
|
||||||
|
// third document
|
||||||
|
String documentId3 = randomNumeric(5);
|
||||||
|
SampleEntity sampleEntity3 = new SampleEntity();
|
||||||
|
sampleEntity3.setId(documentId3);
|
||||||
|
sampleEntity3.setMessage("xyz");
|
||||||
|
sampleEntity3.setRate(15);
|
||||||
|
sampleEntity3.setVersion(System.currentTimeMillis());
|
||||||
|
|
||||||
|
IndexQuery indexQuery3 = new IndexQuery();
|
||||||
|
indexQuery3.setId(documentId3);
|
||||||
|
indexQuery3.setObject(sampleEntity3);
|
||||||
|
|
||||||
|
indexQueries.add(indexQuery1);
|
||||||
|
indexQueries.add(indexQuery2);
|
||||||
|
indexQueries.add(indexQuery3);
|
||||||
|
|
||||||
|
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||||
|
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||||
|
|
||||||
|
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||||
|
.withSort(new FieldSortBuilder("rate").ignoreUnmapped(true).order(SortOrder.ASC))
|
||||||
|
.withSort(new FieldSortBuilder("message").ignoreUnmapped(true).order(SortOrder.ASC)).build();
|
||||||
|
// when
|
||||||
|
Page<SampleEntity> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class);
|
||||||
|
// then
|
||||||
|
assertThat(sampleEntities.getTotalElements(), equalTo(3L));
|
||||||
|
assertThat(sampleEntities.getContent().get(0).getRate(), is(sampleEntity2.getRate()));
|
||||||
|
assertThat(sampleEntities.getContent().get(1).getMessage(), is(sampleEntity1.getMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldExecuteStringQuery() {
|
public void shouldExecuteStringQuery() {
|
||||||
// given
|
// given
|
||||||
|
Loading…
x
Reference in New Issue
Block a user