use of org.springframework.data.elasticsearch.annotations.FieldType.Integer in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchTemplateTests method shouldReturnSortFieldsInSearchHits.
// DATAES-714
@Test
void shouldReturnSortFieldsInSearchHits() {
IndexCoordinates index = IndexCoordinates.of(indexNameProvider.indexName());
IndexOperations indexOperations = operations.indexOps(SearchHitsEntity.class);
indexOperations.delete();
indexOperations.createWithMapping();
SearchHitsEntity entity = new SearchHitsEntity();
entity.setId("1");
entity.setNumber(1000L);
entity.setKeyword("thousands");
IndexQuery indexQuery = new IndexQueryBuilder().withId(entity.getId()).withObject(entity).build();
operations.index(indexQuery, 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);
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);
}
}
Aggregations