mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-25 13:32:10 +00:00
DATAES-657 - Sort by _score not supported by ElasticsearchRestTemplate.
Original pull request: #325
This commit is contained in:
parent
0899df94eb
commit
5f78ab8e25
@ -79,6 +79,7 @@ import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||
import org.elasticsearch.search.sort.ScoreSortBuilder;
|
||||
import org.elasticsearch.search.sort.SortBuilder;
|
||||
import org.elasticsearch.search.sort.SortBuilders;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
@ -93,7 +94,6 @@ import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.elasticsearch.ElasticsearchException;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Mapping;
|
||||
import org.springframework.data.elasticsearch.annotations.Setting;
|
||||
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
|
||||
import org.springframework.data.elasticsearch.core.client.support.AliasData;
|
||||
@ -148,6 +148,7 @@ public class ElasticsearchRestTemplate extends AbstractElasticsearchTemplate
|
||||
implements ElasticsearchOperations, EsClient<RestHighLevelClient>, ApplicationContextAware {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ElasticsearchRestTemplate.class);
|
||||
|
||||
private RestHighLevelClient client;
|
||||
private ResultsMapper resultsMapper;
|
||||
private String searchTimeout;
|
||||
@ -1353,19 +1354,34 @@ public class ElasticsearchRestTemplate extends AbstractElasticsearchTemplate
|
||||
|
||||
private void prepareSort(Query query, SearchSourceBuilder sourceBuilder,
|
||||
@Nullable ElasticsearchPersistentEntity<?> entity) {
|
||||
|
||||
for (Sort.Order order : query.getSort()) {
|
||||
ElasticsearchPersistentProperty property = entity != null //
|
||||
? entity.getPersistentProperty(order.getProperty()) //
|
||||
: null;
|
||||
String fieldName = property != null ? property.getFieldName() : order.getProperty();
|
||||
FieldSortBuilder sort = SortBuilders.fieldSort(fieldName)
|
||||
.order(order.getDirection().isDescending() ? SortOrder.DESC : SortOrder.ASC);
|
||||
if (order.getNullHandling() == Sort.NullHandling.NULLS_FIRST) {
|
||||
sort.missing("_first");
|
||||
} else if (order.getNullHandling() == Sort.NullHandling.NULLS_LAST) {
|
||||
sort.missing("_last");
|
||||
SortOrder sortOrder = order.getDirection().isDescending() ? SortOrder.DESC : SortOrder.ASC;
|
||||
|
||||
if (ScoreSortBuilder.NAME.equals(order.getProperty())) {
|
||||
ScoreSortBuilder sort = SortBuilders //
|
||||
.scoreSort() //
|
||||
.order(sortOrder);
|
||||
|
||||
sourceBuilder.sort(sort);
|
||||
} else {
|
||||
ElasticsearchPersistentProperty property = (entity != null) //
|
||||
? entity.getPersistentProperty(order.getProperty()) //
|
||||
: null;
|
||||
String fieldName = property != null ? property.getFieldName() : order.getProperty();
|
||||
|
||||
FieldSortBuilder sort = SortBuilders //
|
||||
.fieldSort(fieldName) //
|
||||
.order(sortOrder);
|
||||
|
||||
if (order.getNullHandling() == Sort.NullHandling.NULLS_FIRST) {
|
||||
sort.missing("_first");
|
||||
} else if (order.getNullHandling() == Sort.NullHandling.NULLS_LAST) {
|
||||
sort.missing("_last");
|
||||
}
|
||||
|
||||
sourceBuilder.sort(sort);
|
||||
}
|
||||
sourceBuilder.sort(sort);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -128,7 +128,6 @@ public class ElasticsearchTemplate extends AbstractElasticsearchTemplate impleme
|
||||
private static final Logger QUERY_LOGGER = LoggerFactory
|
||||
.getLogger("org.springframework.data.elasticsearch.core.QUERY");
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchTemplate.class);
|
||||
private static final String FIELD_SCORE = "_score";
|
||||
|
||||
private Client client;
|
||||
private ResultsMapper resultsMapper;
|
||||
@ -1128,7 +1127,7 @@ public class ElasticsearchTemplate extends AbstractElasticsearchTemplate impleme
|
||||
for (Sort.Order order : query.getSort()) {
|
||||
SortOrder sortOrder = order.getDirection().isDescending() ? SortOrder.DESC : SortOrder.ASC;
|
||||
|
||||
if (FIELD_SCORE.equals(order.getProperty())) {
|
||||
if (ScoreSortBuilder.NAME.equals(order.getProperty())) {
|
||||
ScoreSortBuilder sort = SortBuilders //
|
||||
.scoreSort() //
|
||||
.order(sortOrder);
|
||||
|
@ -786,26 +786,30 @@ public class ElasticsearchTemplateTests {
|
||||
assertThat(sampleEntities.getContent().get(1).getMessage()).isEqualTo(sampleEntity2.getMessage());
|
||||
}
|
||||
|
||||
@Test // DATAES-467
|
||||
@Test // DATAES-467, DATAES-657
|
||||
public void shouldSortResultsByScore() {
|
||||
|
||||
// given
|
||||
List<SampleEntity> entities = Arrays.asList( //
|
||||
SampleEntity.builder().id("1").message("abc").build(), //
|
||||
SampleEntity.builder().id("2").message("def").build(), //
|
||||
SampleEntity.builder().id("3").message("ghi").build());
|
||||
SampleEntity.builder().id("1").message("green").build(), //
|
||||
SampleEntity.builder().id("2").message("yellow green").build(), //
|
||||
SampleEntity.builder().id("3").message("blue").build());
|
||||
|
||||
elasticsearchTemplate.bulkIndex(getIndexQueries(entities));
|
||||
elasticsearchTemplate.refresh(SampleEntity.class);
|
||||
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withPageable(PageRequest.of(0, 10, Sort.by(Sort.Order.asc("_score")))).build();
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder() //
|
||||
.withQuery(matchQuery("message", "green")) //
|
||||
.withPageable(PageRequest.of(0, 10, Sort.by(Sort.Order.asc("_score")))) //
|
||||
.build();
|
||||
|
||||
// when
|
||||
Page<SampleEntity> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class);
|
||||
Page<SampleEntity> page = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class);
|
||||
|
||||
// then
|
||||
assertThat(sampleEntities.getTotalElements()).isEqualTo(3);
|
||||
assertThat(page.getTotalElements()).isEqualTo(2);
|
||||
assertThat(page.getContent().get(0).getId()).isEqualTo("2");
|
||||
assertThat(page.getContent().get(1).getId()).isEqualTo("1");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
x
Reference in New Issue
Block a user