22

Problem: What is the most correct way to simply query for and list all types within a specific index (and all indices) in elasticsearch?

I've been reading through the reference and API but can't seem to find anything obvious.

I can list indices with the command:

$ curl 'localhost:9200/_cat/indices?v'

I can get stats (which don't seem to include types) with the command:

$ curl localhost:9200/_stats

I'd expect that there'd be a straightforward command as simple as:

$ curl localhost:9200/_types

or

$ curl localhost:9200/index_name/_types

Thanks for any help you can offer.

1
  • 4
    There is no _type in ES, if you are only interested in types then have a look at @Andrew White's answer here you will need to install jq for that Commented Dec 8, 2015 at 2:22

3 Answers 3

27

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
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. As I was reading the documentation (and other web resources) I got the impression that mappings were at the field/attribute level, not at the type level. The _mapping command definitely gives me what I'm looking for but I'm using Ruby, not Python, so I'm going to have to figure out how to parse the results in Ruby. Right now, I'm just trying to understand as much of the API as I can by trying different query permutations. As always, I appreciate the help. -- Thx
I've updated my answer with some equivalent Ruby code.
2

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/

Comments

0
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.