Spring Data Elasticsearch排序问题

    科技2022-07-11  97

    目录

     

    前言

    解决方案


    前言

    使用Spring Data Elasticsearch查询数据时,想要进行数据排序,查询抛出异常。

    Caused by: ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [id] in order to load field data by uninverting the inverted index. Note that this can use significant memory.]]

    我是继承Repository进行数据操作,代码如下:

    package cn.jack.elasticsearchdemo.domain.repository; import cn.jack.elasticsearchdemo.domain.Person; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import java.util.List; public interface PersonRepository extends ElasticsearchRepository<Person, String> { List<Person> findByGenderOrderByIdDesc(String id); } @Test public void testFind2() { List<Person> list = this.personRepository.findByGenderOrderByIdDesc("man"); for(Person person : list) { System.out.println(person); } }

    百度得知,elasticsearch中要进行聚合排序操作,text类型的变量不支持,需要使用keyword类型。通过kibana可以看到,Spring Data 已创建keyword类型的id字段:

    但是,根据命名规范,写不出查询方法。。。

    解决方案

    聚合查询等较为复杂的操作,可以使用ElasticsearchRestTemplate模板类完成。

    package cn.jack.elasticsearchdemo.test; import cn.jack.elasticsearchdemo.ElasticsearchdemoApplication; import cn.jack.elasticsearchdemo.domain.Person; import cn.jack.elasticsearchdemo.domain.repository.PersonRepository; import lombok.extern.slf4j.Slf4j; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.sort.SortOrder; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.IndexBoost; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest(classes = ElasticsearchdemoApplication.class) @Slf4j public class EsTest { @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate; @Test public void testFind2() { List<IndexBoost> indexBoostList = new ArrayList<>(); indexBoostList.add(new IndexBoost("jack_person", 1f)); NativeSearchQuery query = new NativeSearchQueryBuilder() .withIndicesBoost(indexBoostList) .withQuery(QueryBuilders.matchAllQuery()) .withSort(new FieldSortBuilder("id.keyword").order(SortOrder.ASC)) .withPageable(PageRequest.of(0, 20)) .build(); IndexCoordinates indexCoordinates = IndexCoordinates.of("jack_person"); AggregatedPage<Person> peoples = this.elasticsearchRestTemplate.queryForPage(query, Person.class, indexCoordinates); peoples.forEach((Person person)-> { System.out.println(person); }); } }

    运行测试,实现数据排序。

     

     

     

     

     

     

    Processed: 0.012, SQL: 8