How to query and list all types within an elasticsearch index? How to query and list all types within an elasticsearch index? elasticsearch elasticsearch

How to query and list all types within an elasticsearch index?


What you call "type" is actually a "mapping type" and the way to get them is simply by using:

curl -XGET localhost:9200/_all/_mapping

Now since you only want the names of the mapping types, you don't need to install anything, as you can use simply use Python to only get you what you want out of that previous response:

curl -XGET localhost:9205/_all/_mapping | python -c 'import json,sys; indices=json.load(sys.stdin); indices = [type for index in indices for type in indices.get(index).get("mappings")]; print list(indices);'

The Python script does something very simple, i.e. it iterates over all the indices and mapping types and only retrieves the latter's names:

import json,sys; resp = json.load(sys.stdin); indices = [type for index in resp for type in indices.get(index).get("mappings")]; print list(indices);'

UPDATE

Since you're using Ruby, the same trick is available by using Ruby code:

curl -XGET localhost:9205/_all/_mapping | ruby -e "require 'rubygems'; require 'json'; resp = JSON.parse(STDIN.read); resp.each { |index, indexSpec | indexSpec['mappings'].each {|type, fields| puts type} }"

The Ruby script looks like this:

require 'rubygems';require 'json';resp = JSON.parse(STDIN.read);resp.each { |index, indexSpec |     indexSpec['mappings'].each { |type, fields|         puts type    }}


You can just print the index and use the _mapping API so you will see only the section of "mappings" in the index.

For example: curl -GET http://localhost:9200/YourIndexName/_mapping?pretty

You will get something like that:

{"YourIndexName" : {    "mappings" : {      "mapping_type_name_1" : {        "properties" : {          "dateTime" : {            "type" : "date"          },          "diskMaxUsedPct" : {            "type" : "integer"          },          "hostName" : {            "type" : "keyword"          },          "load" : {            "type" : "float"          },          "memUsedPct" : {            "type" : "float"          },          "netKb" : {            "type" : "long"          }        }      },      "mapping_type_name_2" : {        "properties" : {          "dateTime" : {            "type" : "date"          },          "diskMaxUsedPct" : {            "type" : "integer"          },          "hostName" : {            "type" : "keyword"          },          "load" : {            "type" : "float"          },          "memUsedPct" : {            "type" : "float"          }        }      }    }  }}

mapping_type_name_1 and mapping_type_name_2 are the types in this index, and you also can see the structure of these types.

Good explanation about mapping_types is here: https://logz.io/blog/elasticsearch-mapping/


private Set<String> getTypes(String indexName) throws Exception{    HttpClient client = HttpClients.createDefault();    HttpGet mappingsRequest = new HttpGet(getServerUri()+"/"+getIndexName()+"/_mappings");    HttpResponse scanScrollResponse = client.execute(mappingsRequest);    String response = IOUtils.toString(scanScrollResponse.getEntity().getContent(), Charset.defaultCharset());    System.out.println(response);    String mappings = ((JSONObject)JSONSerializer.toJSON(JSONObject.fromObject(response).get(indexName).toString())).get("mappings").toString();    Set<String> types = JSONObject.fromObject(mappings).keySet();    return types;}