Elasticsearch Java API – Get Field List for a given Index and Type

Elasticsearch Java API - Get Field List for a given Index and Type

Elasticsearch Java API – Get Field List for a given Index and Type

The following is sample Java program to get the list of fields for a given index and type name in an Elasticsearch instance.

Sample Program:

package com.report.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
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";
          String type = "football";

          List<String> fieldList = new ArrayList<String>();

          ClusterState cs = esclient.admin().cluster().prepareState().setIndices(index).execute().actionGet().getState();
          IndexMetaData imd = cs.getMetaData().index(index);
          MappingMetaData mdd = imd.mapping(type);
          Map<String, Object> map = null;

          try {
               map = mdd.getSourceAsMap();
          } catch (IOException e) {
               e.printStackTrace();
          }
          fieldList = getList("", map);

          System.out.println("Field List:");
          for (String field : fieldList) {
               System.out.println(field);
          }
     }

     private static List<String> getList(String fieldName, Map<String, Object> mapProperties) {
           List<String> fieldList = new ArrayList<String>();
           Map<String, Object> map = (Map<String, Object>) mapProperties.get("properties");
           Set<String> keys = map.keySet();
           for (String key : keys) {
                if (((Map<String, Object>) map.get(key)).containsKey("type")) {
                     fieldList.add(fieldName + "" + key);
                } else {
                     List<String> tempList = getList(fieldName + "" + key + ".", (Map<String, Object>) map.get(key));
                     fieldList.addAll(tempList);
                }
           }
           return fieldList;
     }
}

Sample Output:

Field List:
country
team
won
draw
lost
points

Related Links: 

Web Application for Elasticsearch :
  1. ElasticTab – Elasticsearch to Excel Report (Web Application)
Elasticsearch Plugin:
  1. Elasticsearch Plugin To Generate (Save and E-Mail) Excel Reports
Elasticsearch:
  1. Execute Multiple Search Query in Elasticsearch
  2. Monitor Elasticsearch Servers with Shell Script - E-Mail Notification
  3. Execute Raw Elasticsearch Query using Transport Client – Java API
  4. Elasticsearch – Apply Nested Filter on Nested (Inner) Aggregation
  5. Execute Multiple Search Query in Elasticsearch
  6. Enable CORS to Send Cross Domain Request to Elasticsearch using AJAX
  7. Elasticsearch Java API – Get Index List
  8. Elasticsearch Java API – Get Alias List
  9. Elasticsearch Java API - Get Type List from given Index
  10. Elasticsearch Java API – Get Field List for a given Index and Type
  11. Elasticsearch Java API – Get Index Type List Mapping
  12. Elasticsearch – Use Script Filter/Conditon in Aggregation/Sub-Aggreagtion
  13. Elasticsearch – Compare/ScriptFilter/Condition on Two Fields using Script Filter – REST Query + Java API
  14. Elasticsearch - Date/Time(String)  Add/Subtract Duration - Days,Months,Years,Hours,Minutes,Seconds
Logstash:
  1. Logstash – Process Log File Once and Exit/Stop Logstash After Reading Log File Once
  2. Measure Logstash Performance using Metrics Filter – Issue/Error in Syntax (Unknown setting ‘message’ for stdout)
  3. Logstash – Process Same Log File (File Input) from Beginning/Start
  4. Create Custom Filter/Plugin to Emit New Events Manually in Logstash
Logstash and Elasticsearch:
  1. Query Elasticsearch Cluster in Filter Section when using Logstash
  2. Custom Elasticsearch Index Name/Type Name based on Events in Logstash
MongoDB and Elasticsearch:
  1. Import Data from Mongo DB to Elasticsearch using Elasticsearch River
 Read More...

[ YOU MAY ALSO LIKE ]

2 thoughts on “Elasticsearch Java API – Get Field List for a given Index and Type

    1. Raghavendar T S

      Settings settings = ImmutableSettings.settingsBuilder().put(“client.transport.sniff”, true).put(“cluster.name”, “CLUSTER_NAME“).build();
      Client esClient = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(“HOSTNAME“, 9300));

      SearchRequestBuilder searchRequestBuilder = esClient.prepareSearch(“INDEX“).setTypes(“TYPE“);
      SearchResponse response = searchRequestBuilder.execute().actionGet();
      SearchHits hits = response.getHits();
      for (int i = 0; i < hits.getHits().length; i++) { Map responseFields = hits.getAt(i).getFields();
      SearchHitField fieldValueObj = responseFields.get(“FIELD_NAME“);
      String fieldValue=fieldValueObj.getValue().toString()
      }

Leave a Reply