springboot脚手架Tboot开发搜索引擎--springboot整合JestClient

1.首先我们来看最终效果

整合完成JestClient,查询出ES中所有的索引。
在这里插入图片描述

2.首先我们来看一看什么是JestClient

Jest 是ElasticSearch 的java Http Rest 客户端。

他主要是让我们更在使用java执行es语法的时候更方法,他给我们很多的封闭。

3.整合JestClient

3.1 老套路添加依赖

        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>6.3.1</version>
        </dependency>

3.2 在application.yml中添加配置文件

spring:
	elasticsearch:
		jest:
	      uris: http://127.0.0.1:9200/
	      read-timeout: 120s
	      connection-timeout: 120s

3.3 在代码中使用了

注入Bean对象

    @Autowired
    private JestClient jestClient;

在方法中写API

 @Override
    public Page<Map<String, String>> getIndex(String name, PageVo page) {

        GetMapping.Builder builder = new GetMapping.Builder();
        if (StrUtil.isNotEmpty(name)) {
            builder.addIndex(name);
        }
        GetMapping getMapping = builder.build();
        try {
            JestResult execute = jestClient.execute(getMapping);
            if (execute.isSucceeded()) {
                JsonObject jsonObject = execute.getJsonObject();
                Set<Map.Entry<String, JsonElement>> entries = jsonObject.entrySet();
                List<Map<String, String>> arrayList = entries.stream().map((el) -> {
                    Map<String, String> map = new HashMap<>();
                    map.put("indexName", el.getKey());
                    map.put("indexValue", el.getValue().toString());
                    return map;
                }).collect(Collectors.toList());

                Page<Map<String, String>> mapPage = PageUtil.listBuildMapPage(page, arrayList);
                return mapPage;
            }
            throw new TbootException("连接es失败");
        } catch (IOException e) {
            e.printStackTrace();
            throw new TbootException("连接es失败");
        }
    }

3.4 代码解释

  GetMapping.Builder builder = new GetMapping.Builder();
        if (StrUtil.isNotEmpty(name)) {
            builder.addIndex(name);
        }
        GetMapping getMapping = builder.build();

这段代码是组装请求参数, 对应ES中查询索引Mapping结构

GET http://localhost:9200/_mapping 查询索引结构

下图是ES执行结果

在这里插入图片描述

3.5 发起请求代码解释
其实也没有什么好解释的,就是将请求参数,发起一个http 的请求。

JestResult execute = jestClient.execute(getMapping);

简单小结

这篇文章就是一个JestClient的一个小小的入门,包括了springboot整合Jest,还使用了jestClient发起了一个查询请求。实现了一个功能,实现全部索引的查询。

这里有一个问题就是这个_mapping 是没有分页查询的, 这是ElasticSearch的问题,和JestClient没有关系。 不过我们自己实现了分页查询和条件查询。

如果想看效果的话可以关注我们的线上案例。

效果预览 效果预览 效果预览 推荐使用edge浏览器,谷歌浏览器多刷新几次。

我们的gitee地址

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值