ElasticsearchRestTemplate实现from+size分页

Posted 编程老高

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ElasticsearchRestTemplate实现from+size分页相关的知识,希望对你有一定的参考价值。

UserInfo类:

package com.jd.vo;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

/**
 * @Document 用来声明ES索引信息
 * indexName 索引名称
 * shards 主分区数量,默认5
 * replicas 副本分区数量,默认1
 * createIndex true:SpringBoot启动后自动创建索引,此时映射Mapping为空,默认为true;false:SpringBoot启动后不会自动创建索引,需要手动创建索引。
 */
@Document(indexName = "userinfo_index", shards = 5, replicas = 1, createIndex = false)
public class UserInfo 

    @Id
    private Integer id;
    @Field(type = FieldType.Keyword)
    private String name;
    //type : 字段数据类型
    //analyzer : 分词器类型
    //index : 是否索引,默认为true
    @Field(type = FieldType.Text, analyzer = "ik_max_word", index = true)
    private String address;

    public UserInfo(Integer id, String name, String address) 
        this.id = id;
        this.name = name;
        this.address = address;
    

    public Integer getId() 
        return id;
    
    public void setId(Integer id) 
        this.id = id;
    
    public String getName() 
        return name;
    
    public void setName(String name) 
        this.name = name;
    
    public String getAddress() 
        return address;
    
    public void setAddress(String address) 
        this.address = address;
    
PagingTests类:
package com.jd;

import com.jd.vo.UserInfo;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@SpringBootTest
public class PagingTests 

    @Autowired
    private ElasticsearchRestTemplate template;

    //批量操作,bulk [bʌlk] n. 巨大的体重(或重量、形状、身体等);
    @Test
    public void bulk() throws IOException 
        List<UserInfo> list = new ArrayList<>();
        list.add(new UserInfo(1, "Jim", "河南郑州市高新技术开发区朗月公园茂"));
        list.add(new UserInfo(2, "Lucy", "内蒙古自治区呼和浩特市回民区万嘉康城"));
        list.add(new UserInfo(3, "Kate", "江西省南昌市红谷滩区天赐良园"));
        list.add(new UserInfo(4, "Jhon", "福建省厦门市湖里区碧湖嘉园"));
        list.add(new UserInfo(5, "Tom", "吉林省长春市宽城区中正御华庭"));
        list.add(new UserInfo(6, "Jimmy", "广东省东莞市东城街道天宝花园"));
        list.add(new UserInfo(7, "Robert", "新疆自治区乌鲁木齐市头屯河区乌西五街小区"));
        Iterable iterable = template.save(list);
        System.out.println(iterable);
    

    @Test
    public void fromSize() throws IOException 
        int pageNo = 2;
        int pageSize = 2;
        //条件
        MatchAllQueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        //排序
        FieldSortBuilder sortBuilder = new FieldSortBuilder("id").order(SortOrder.ASC);
        //分页
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
        //搜索
        NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(queryBuilder).withPageable(pageable).build();
        SearchHits<UserInfo> hits = template.search(query, UserInfo.class);

        //解析
        ArrayList<UserInfo> list = new ArrayList<>();
        for (SearchHit<UserInfo> hit : hits) 
            UserInfo book = hit.getContent();
            System.out.println(book.getId() + " " + book.getName() + " " + book.getAddress());
        
    

以上是关于ElasticsearchRestTemplate实现from+size分页的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch 7.X SpringBoot 使用 ElasticsearchRestTemplate 操作 ES

ElasticsearchRestTemplate实现scroll分页

ElasticsearchRestTemplate实现scroll分页

ElasticsearchRestTemplate实现scroll分页

ElasticsearchRestTemplate实现from+size分页

ElasticsearchRestTemplate实现from+size分页