• ElasticSearch Client问题整理2


    ElasticsearchClient中提供了获取所有索引信息的方法

    IndicesResponse indices = client.cat().indices();
    List<IndicesRecord> records = indices.valueBody();
    
    • 1
    • 2

    IndicesRecord 是索引的信息,字段很多,下面列举出了比较重要的几个。

    @Nullable
    private final String health;
    
    @Nullable
    private final String status;
    
    @Nullable
    private final String index;
    
    @Nullable
    private final String uuid;
    
    @Nullable
    private final String pri;
    
    @Nullable
    private final String rep;
    
    @Nullable
    private final String docsCount;
    
    @Nullable
    private final String docsDeleted;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    Kibana 中使用 GET /_cat/indices?v 也能查出索引信息

    indices.valueBody() 中也能看到ElasticSearch返回了这些信息,但是通过Postman访问自己封装的获取索引信息接口时,得到的响应少了很多东西

    @GetMapping("/list")
    public List<IndicesRecord> list() {
        try {
            return seaElasticSearchClient.getIndexs();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    但是前端收到的响应信息为

    {
        "code": 200,
        "message": "success",
        "data": [
            {
                "current": null,
                "time": null,
                "total": null,
                "existsTime": null,
                "existsTotal": null,
                "missingTime": null,
                "missingTotal": null
            },
            {
                "current": null,
                "time": null,
                "total": null,
                "existsTime": null,
                "existsTotal": null,
                "missingTime": null,
                "missingTotal": null
            }
        ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    这里数量对应不上是因为我没把所有的内容都放上来

    嗯,这几个字段在IndicesRecord中是没有定义了,但是呢,IndicesRecord中定义了以下几个字段

    @Nullable
    private final String getTime;
    
    @Nullable
    private final String getTotal;
    
    @Nullable
    private final String getExistsTime;
    
    @Nullable
    private final String getExistsTotal;
    
    @Nullable
    private final String getMissingTime;
    
    @Nullable
    private final String getMissingTotal;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    并定义了对应的方法,如getMissingTime字段对应getMissingTotal()方法,返回getMissingTotal的值

    @Nullable
    public final String getMissingTotal() {
        return this.getMissingTotal;
    }
    
    • 1
    • 2
    • 3
    • 4

    然后,SpringMVC里面就给转换成missingTotal了。

    其他的如index、status字段没有get方法,所以就没转换,没有返回到前端

    @Nullable
    public final String status() {
        return this.status;
    }
    
    
    @Nullable
    public final String index() {
        return this.index;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    醉了,我前后检查了很多配置项,以为自己返回响应的切片出问题了,搞了2个多小时,才发现这个坑

    解决方法:

    如果想把索引的信息返回给前端,那么可以自己定义一个索引实体,对结果进行一次转换,将转换后的实体返回给前端。

    public List<Index> getAllIndex() throws IOException {
        List<Index> indexs = new ArrayList<>();
        IndicesResponse indices = client.cat().indices();
        List<IndicesRecord> records = indices.valueBody();
        for (IndicesRecord record : records) {
            Index index = new Index();
            index.setIndex(record.index());
            index.setStatus(record.status());
            index.setPri(record.pri());
            index.setRep(record.rep());
            indexs.add(index);
        }
        return indexs;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    《Java编程思想》读书笔记(三)
    2022牛客蔚来杯补题(第九场)
    重塑科普展厅魅力,以用户体验为核心的策略性规划新探索!
    14 - 多线程之锁优化(下):使用乐观锁优化并行操作
    【Js】数据处理
    MySQL下载安装 & 完美卸载
    javase_io_异常_回顾
    图论|841钥匙和房间
    Java web程序实现请求后重启服务动作
    Vue学习:回顾Object.defineProperty(给对象添加或者定义属性的)
  • 原文地址:https://blog.csdn.net/u010514052/article/details/126922619