java API方式获取索引mapping信息

Elasticsearch | 作者 jony | 发布于2018年04月26日 | 阅读数:5437

如题,如果用http方式,发个请求即可,即
但是如果用java APIfan方式怎么写呢...找了很久没有看到
已邀请:

jony

赞同来自: zyen

额,好绕.....找到获取方式了,如下:
ImmutableOpenMap<String, MappingMetaData> mappings = client.admin().cluster().prepareState().execute()
                .actionGet().getState().getMetaData().getIndices().get("tese_index").getMappings();
        String  mapping = mappings.get("products").source().toString();

yayg2008

赞同来自:

用Jest,非常方便:
String indexName = ""; 
String indexType = "";
GetMapping action = new GetMapping.Builder().addIndex(indexName).addType(indexType).build();
client.execute(action);

 

hapjin

赞同来自:

可以用 ES 官方客户端:low level client,也即:RestClient类,因为官方推荐使用 high level restful api,但是可以从high level client 获取到 low level client
代码如下:
GetMappingsRequest mappingsRequest = new GetMappingsRequest();
mappingsRequest.indices(indexName);
mappingsRequest.indicesOptions(IndicesOptions.lenientExpandOpen());

RestClient restClient = restHighLevelClient.getLowLevelClient();

Response response = restClient.performRequest("GET", "/" + indexName + "/" + "_mapping");
HttpEntity entity = response.getEntity();
//mapping信息
String mappingInfoResult = EntityUtils.toString(entity);

 
附:ES集群版本和客户端版本都是5.6.3
 
参考:

以及官方文档:
 
多说一句:ES最新客户端版本,比如7.x,就提供了直接获取mapping的API了。比5.6方便多了
 

pony_maggie - 公众号:犀牛饲养员的技术笔记

赞同来自:

high level级别的API都是应用层常用的,获取mapping这种操作是应用层比较少用的

要回复问题请先登录注册