DATAES-714 - Sort results should be returned in the SearchHits.
Original PR: #361
This commit is contained in:
committed by
GitHub
parent
e55bae725e
commit
d19e699b32
@@ -128,6 +128,9 @@ public abstract class ElasticsearchTemplateTests {
|
||||
|
||||
indexOperations.createIndex(SampleEntityUUIDKeyed.class);
|
||||
indexOperations.putMapping(SampleEntityUUIDKeyed.class);
|
||||
|
||||
indexOperations.createIndex(SearchHitsEntity.class);
|
||||
indexOperations.putMapping(SearchHitsEntity.class);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
@@ -146,6 +149,7 @@ public abstract class ElasticsearchTemplateTests {
|
||||
indexOperations.deleteIndex(INDEX_1_NAME);
|
||||
indexOperations.deleteIndex(INDEX_2_NAME);
|
||||
indexOperations.deleteIndex(INDEX_3_NAME);
|
||||
indexOperations.deleteIndex(SearchHitsEntity.class);
|
||||
}
|
||||
|
||||
@Test // DATAES-106
|
||||
@@ -2856,6 +2860,41 @@ public abstract class ElasticsearchTemplateTests {
|
||||
assertThat(map).containsKey("index.max_result_window");
|
||||
}
|
||||
|
||||
@Test // DATAES-714
|
||||
void shouldReturnSortFieldsInSearchHits() {
|
||||
IndexCoordinates index = IndexCoordinates.of("test-index-searchhits-entity-template");
|
||||
SearchHitsEntity entity = SearchHitsEntity.builder().id("1").number(1000L).keyword("thousands").build();
|
||||
IndexQuery indexQuery = new IndexQueryBuilder().withId(entity.getId()).withObject(entity).build();
|
||||
operations.index(indexQuery, index);
|
||||
indexOperations.refresh(index);
|
||||
|
||||
NativeSearchQuery query = new NativeSearchQueryBuilder() //
|
||||
.withQuery(matchAllQuery()) //
|
||||
.withSort(new FieldSortBuilder("keyword").order(SortOrder.ASC))
|
||||
.withSort(new FieldSortBuilder("number").order(SortOrder.DESC)).build();
|
||||
|
||||
SearchHits<SearchHitsEntity> searchHits = operations.search(query, SearchHitsEntity.class, index);
|
||||
|
||||
assertThat(searchHits).isNotNull();
|
||||
assertThat(searchHits.getSearchHits()).hasSize(1);
|
||||
|
||||
SearchHit<SearchHitsEntity> searchHit = searchHits.getSearchHit(0);
|
||||
List<Object> sortValues = searchHit.getSortValues();
|
||||
assertThat(sortValues).hasSize(2);
|
||||
assertThat(sortValues.get(0)).isInstanceOf(String.class).isEqualTo("thousands");
|
||||
// transport client returns Long, rest client Integer
|
||||
java.lang.Object o = sortValues.get(1);
|
||||
if (o instanceof Integer) {
|
||||
Integer i = (Integer) o;
|
||||
assertThat(o).isInstanceOf(Integer.class).isEqualTo(1000);
|
||||
} else if (o instanceof Long) {
|
||||
Long l = (Long) o;
|
||||
assertThat(o).isInstanceOf(Long.class).isEqualTo(1000L);
|
||||
} else {
|
||||
fail("unexpected object type " + o);
|
||||
}
|
||||
}
|
||||
|
||||
protected RequestFactory getRequestFactory() {
|
||||
return ((AbstractElasticsearchTemplate) operations).getRequestFactory();
|
||||
}
|
||||
@@ -3007,4 +3046,14 @@ public abstract class ElasticsearchTemplateTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
@Document(indexName = "test-index-searchhits-entity-template")
|
||||
static class SearchHitsEntity {
|
||||
@Id private String id;
|
||||
@Field(type = FieldType.Long) Long number;
|
||||
@Field(type = FieldType.Keyword) String keyword;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.elasticsearch.ElasticsearchStatusException;
|
||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -693,6 +695,27 @@ public class ReactiveElasticsearchTemplateTests {
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnSortFields() {
|
||||
SampleEntity entity = randomEntity("test message");
|
||||
entity.rate = 42;
|
||||
index(entity);
|
||||
|
||||
NativeSearchQuery query = new NativeSearchQueryBuilder() //
|
||||
.withQuery(matchAllQuery()) //
|
||||
.withSort(new FieldSortBuilder("rate").order(SortOrder.DESC)) //
|
||||
.build();
|
||||
|
||||
template.search(query, SampleEntity.class) //
|
||||
.as(StepVerifier::create) //
|
||||
.consumeNextWith(it -> {
|
||||
List<Object> sortValues = it.getSortValues();
|
||||
assertThat(sortValues).hasSize(1);
|
||||
assertThat(sortValues.get(0)).isEqualTo(42);
|
||||
}) //
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Data
|
||||
@Document(indexName = "marvel", type = "characters")
|
||||
static class Person {
|
||||
|
||||
Reference in New Issue
Block a user