栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

8.10 SpringBoot集成ElasticSearch之深度分页

8.10 SpringBoot集成ElasticSearch之深度分页

1.condition开发
在项目目录“/src/main/java/com/example/es/condition”的EmployeeCondition类中实现ScrollProvider接口,具体代码如下。

@Data
public class EmployeeCondition extends SampleEmployeeCondition implements RoutingProvider, ScoreFunctionProvider, SortProvider, ScrollProvider {
	@Ignore
    private String scrollId;
    
    @Ignore
    private boolean clear;

    @Override
    public String getScrollId() {
        return scrollId;
    }
    
    @Override
    public boolean isClearScroll() {
        return clear;
    }
}

2.mapper开发
在项目目录“/src/main/java/com/example/es”的EmployeeMapper类中新增scroll查询和清除scroll快照的接口,具体代码如下。

@EasyMapper(indices = "employee", clusterRouter = "sampleCluster")
public interface EmployeeMapper {
    
    @SearchScroll(keepAlive = "5s")
    ScrollResponse searchScroll(EmployeeCondition employeeCondition, Pageable pageable);

    
    @SearchScroll(clearScroll = true)
    boolean clearSearchScroll(String scrollId, ResponseExtractor extractor);
}

3.controller开发
在项目目录“/src/main/java/com/example/es”下的EmployeeController类中修改查询员工信息接口,具体代码如下。

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Resource
    private EmployeeMapper employeeMapper;

	@ResponseBody
    @RequestMapping(value = "/queryEmployeeList", method = RequestMethod.GET)
    public List queryEmployeeList() {
        List employeeEntityList = new ArrayList<>();
        ScrollResponse response = null;
        EmployeeCondition employeeCondition = new EmployeeCondition();
        employeeCondition.setAgeRange(Range.of(25, 35).closeLeft().closeRight());
        //页数
        int count = 2;
        //每页的文档数量
        int size = 2;
        Pageable pageAble = Pageable.first(size);
        for (int i = 0; i < count; i++) {
            response = employeeMapper.searchScroll(employeeCondition, pageAble);
            employeeCondition.setScrollId(response.getScrollId());
            if (i == count - 1) {
                Iterator iterator = response.getResponse().iterator();
                while (iterator.hasNext()) {
                    employeeEntityList.add(iterator.next());
                }
            }
        }

//        employeeMapper.clearSearchScroll(response.getScrollId(), new ResponseExtractor() {
//            @Override
//            public Boolean extractData(ActionResponse response) {
//                ClearScrollResponse scrollResponse = narrow(response, ClearScrollResponse.class);
//                return scrollResponse.isSucceeded();
//            }
//        });

        return employeeEntityList;
    }
}

4.测试
启动项目,然后在postman中请求“http://localhost:8080/employee/queryEmployeePage”,成功后返回对应的信息。

[
    {
        "id": "10000001",
        "employeeId": "10000001",
        "name": "James Harden",
        "age": 31,
        "birthday": "1991-01-01",
        "job": "Java engineer",
        "salary": 30000.0,
        "hobby": [
            "swimming",
            "running",
            "basketball",
            "football"
        ],
        "profile": {
            "nickName": "squirrel",
            "avatar": "https://www.avatar.com/10000001.png",
            "grade": "diamonds"
        },
        "relative": [
            {
                "employeeId": "10000002"
            },
            {
                "employeeId": "10000004"
            }
        ]
    },
    {
        "id": "10000003",
        "employeeId": "10000003",
        "name": "LeBron James",
        "age": 35,
        "birthday": "1987-12-25",
        "job": "Technical director",
        "salary": 50000.0,
        "hobby": [
            "boxing",
            "climbing",
            "football"
        ],
        "profile": {
            "nickName": "mountain",
            "avatar": "https://www.avatar.com/10000003.png",
            "grade": "diamonds"
        },
        "relative": [
            {
                "employeeId": "10000002"
            }
        ]
    }
]
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/699393.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号