Elasticsearch Java-Api 获取索引字段列表-属性

前言

Java学习路线个人总结-博客
❤欢迎点赞👍收藏⭐留言 📝分享给需要的小伙伴

Elasticsearch mappings
在这里插入图片描述

EsConfig连接

@Component
public class EsConfig {
    
    public RestHighLevelClient getEsHighInit2() {
        RestClientBuilder http = RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
                .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
                    @Override
                    public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                        requestConfigBuilder.setConnectTimeout(700000);
                        requestConfigBuilder.setSocketTimeout(600000);
                        requestConfigBuilder.setConnectionRequestTimeout(100000);
                        return requestConfigBuilder;
                    }
                });
        return new RestHighLevelClient(http);

    }
}

TEST测试

@org.junit.Test
public void aaaa() throws IOException {
        //获取es连接
        RestHighLevelClient esHighInit = esConfig.getEsHighInit2();
        //指定索引
        GetMappingsRequest getMappings = new GetMappingsRequest().indices("test");
        //调用获取
        GetMappingsResponse getMappingResponse = esHighInit.indices().getMapping(getMappings, RequestOptions.DEFAULT);
        //处理数据
        Map<String, MappingMetaData> allMappings = getMappingResponse.mappings();
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Map.Entry<String, MappingMetaData> indexValue : allMappings.entrySet()) {
            Map<String, Object> mapping = indexValue.getValue().sourceAsMap();
            Iterator<Map.Entry<String, Object>> entries = mapping.entrySet().iterator();
            entries.forEachRemaining(stringObjectEntry -> {
                if (stringObjectEntry.getKey().equals("properties")) {
                    Map<String, Object> value = (Map<String, Object>) stringObjectEntry.getValue();
                    for (Map.Entry<String, Object> ObjectEntry : value.entrySet()) {
                        Map<String, Object> map = new HashMap<>();
                        String key = ObjectEntry.getKey();
                        Map<String, Object> value1 = (Map<String, Object>) ObjectEntry.getValue();
                        map.put(key, value1.get("type"));
                        mapList.add(map);
                    }
                }
            });
        }
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(mapList));
    }
// [{"省份":"keyword"},{"配置":"keyword"},{"车型":"keyword"},{"销售额":"integer"},{"折扣":"integer"},{"id":"keyword"},{"经销商":"keyword"},{"订单数":"integer"}]

推荐文章

Spring Cloud Alibaba 系列学习笔记
SpringCloud Alibaba Nacos
SpringCloud Alibaba Sentinel
@SentinelResource注解总结,异常、降级兜底
SpringCloud Alibaba Sentine 规则持久化
SpringCloud Alibaba RocketMQ
Seata1.4.2分布式事务整合nacos+SpringCloudAlibaba
2021最新Java面试题

觉得对您有帮助就留下个宝贵的👍吧!

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冒险的梦想家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值