使用JavaAPI获取映射中每个字段的类型(字符串、布尔值、Int、嵌套等)
我想用JavaAPI获取映射中所有字段的类型 通过此操作,我可以检索所有字段:使用JavaAPI获取映射中每个字段的类型(字符串、布尔值、Int、嵌套等),java,
elasticsearch,Java,
elasticsearch,我想用JavaAPI获取映射中所有字段的类型 通过此操作,我可以检索所有字段: ClusterState cs = Client.admin().cluster().prepareState().execute().actionGet().getState(); IndexMetaData imd = cs.getMetaData().index(customerName); MappingMetaData mmd = imd.mapping(type); Map<String, Obje
ClusterState cs = Client.admin().cluster().prepareState().execute().actionGet().getState();
IndexMetaData imd = cs.getMetaData().index(customerName);
MappingMetaData mmd = imd.mapping(type);
Map<String, Object> source = mmd.sourceAsMap();
for (String i : source.keySet()) {
JSONObject jsonSource = null;
try {
jsonSource = new JSONObject(source.get(i).toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Iterator<?> iterator = jsonSource.keys();
while (iterator.hasNext()) {
listFields.add(iterator.next().toString());
}
}
ClusterState cs=Client.admin().cluster().prepareState().execute().actionGet().getState();
IndexMetaData imd=cs.getMetaData().index(customerName);
MappingMetaData mmd=imd.mapping(类型);
Map source=mmd.sourceAsMap();
for(字符串i:source.keySet()){
JSONObject jsonSource=null;
试一试{
jsonSource=newJSONObject(source.get(i.toString());
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
迭代器迭代器=jsonSource.keys();
while(iterator.hasNext()){
add(iterator.next().toString());
}
}
我在MappingMetaData中寻找方法,但没有得到任何可以提供字段类型的方法(例如:string、float、int等)。我只需要在字段列表中重新驱动核心类型(不带嵌套或内部对象)您可以使用Jackson的JsonNode:
MappingMetaData mmd = imd.mapping(type);
CompressedString source = mmd.source();
JsonNode mappingNode = new ObjectMapper().readTree(source));
JsonNode propertiesNode = mappingNode.get("properties");
Iterator<Entry<String, JsonNode>> properties = propertiesNode.fields();
while (properties.hasNext()) {
Entry<String, JsonNode> node = properties.next();
String name = node.getKey();
JsonNode valueNode = node.getValue();
if (valueNode != null) {
String type = valueNode.get("type").asText();//gives you the type
}
}
MappingMetaData mmd=imd.mapping(类型);
压缩字符串源=mmd.source();
JsonNode mappingNode=new ObjectMapper().readTree(source));
JsonNode propertiesNode=mappingNode.get(“属性”);
迭代器属性=propertiesNode.fields();
while(properties.hasNext()){
Entry node=properties.next();
字符串名称=node.getKey();
JsonNode valueNode=node.getValue();
if(valueNode!=null){
String type=valueNode.get(“type”).asText();//提供类型
}
}
如果使用System.out.println(i+“=”+source.get(i)),会得到什么代码>?谢谢,这就是解决方案!