Search in sources :

Example 1 with Integer

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);
    }
}
Also used : FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) Integer(java.lang.Integer) Integer(org.springframework.data.elasticsearch.annotations.FieldType.Integer) Long(java.lang.Long) Object(java.lang.Object) Object(java.lang.Object) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Aggregations

Integer (java.lang.Integer)1 Long (java.lang.Long)1 Object (java.lang.Object)1 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)1 Test (org.junit.jupiter.api.Test)1 Integer (org.springframework.data.elasticsearch.annotations.FieldType.Integer)1 IndexCoordinates (org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)1 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)1