一、获取索引的所有mapping
通过java客户端获取mapping:
package elasticsearch.in.action.client;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
public class TransClient {
public static String clusterName = "elasticsearch";// 集群名称
public static String serverIP = "127.0.0.1";// 服务器IP
public static void main(String[] args) {
System.out.println(getMapping("news", "sportnews"));
}
public static String getMapping(String indexname, String typename) {
Settings settings = Settings.settingsBuilder().put("cluster.name", clusterName).build();
String mapping="";
try {
TransportClient client = TransportClient.builder().settings(settings).build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(serverIP), 9300));
ImmutableOpenMap<String, MappingMetaData> mappings = client.admin().cluster().prepareState().execute()
.actionGet().getState().getMetaData().getIndices().get(indexname).getMappings();
mapping = mappings.get(typename).source().toString();
client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return mapping;
}
}
二、获取所有的type以及每个type的mapping
mapping信息都是ImmutableOpenMap
for (ObjectObjectCursor<String, MappingMetaData> cursor : mappings) {
System.out.println(cursor.key); // 索引下的每个type
System.out.println(cursor.value.getSourceAsMap()); // 每个type的mapping
}