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

Elasticsearch mappings
Elasticsearch Java-Api 获取索引字段列表-属性_第1张图片

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面试题

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

你可能感兴趣的:(ELK,elasticsearch)