The following is sample Java program to get the list of type for a given index name in an Elasticsearch instance.
Sample Program:
package com.report.test; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest; import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.cluster.metadata.MappingMetaData; import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.hppc.cursors.ObjectObjectCursor; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; public class SampleProgram{ public static void main(String[] args) { String hostname = "localhost"; String clusterName = "elasticsearch"; Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.sniff", true).put("cluster.name", clusterName).build(); Client esclient = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(hostname, 9300)); String index = "game"; List<String> typeList = new ArrayList<String>(); try { GetMappingsResponse res = esclient.admin().indices().getMappings(new GetMappingsRequest().indices(index)).get(); ImmutableOpenMap<String, MappingMetaData> mapping = res.mappings().get(index); for (ObjectObjectCursor<String, MappingMetaData> c : mapping) { typeList.add(c.key); } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } System.out.println("Type List:"); for (String type : typeList) { System.out.println(type); } } }
Sample Output:
Type List: cricket football