Spring Data Elasticsearch排序问题

目录

 

前言

解决方案


前言

使用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);
        });
    }
}

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

 

 

 

 

 

 

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值