Indexing 如何查询和列出elasticsearch索引中的所有类型?
问题:在elasticsearch中查询并列出特定索引(以及所有索引)中的所有类型的最正确方法是什么 我一直在阅读参考资料和API,但似乎找不到任何明显的东西 我可以使用以下命令列出索引:Indexing 如何查询和列出elasticsearch索引中的所有类型?,indexing,elasticsearch,types,Indexing,elasticsearch,Types,问题:在elasticsearch中查询并列出特定索引(以及所有索引)中的所有类型的最正确方法是什么 我一直在阅读参考资料和API,但似乎找不到任何明显的东西 我可以使用以下命令列出索引: $ curl 'localhost:9200/_cat/indices?v' $ curl localhost:9200/_stats 我可以使用以下命令获取统计信息(似乎不包括类型): $ curl 'localhost:9200/_cat/indices?v' $ curl localhost:92
$ curl 'localhost:9200/_cat/indices?v'
$ curl localhost:9200/_stats
我可以使用以下命令获取统计信息(似乎不包括类型):
$ curl 'localhost:9200/_cat/indices?v'
$ curl localhost:9200/_stats
我希望有一个简单明了的命令:
$ curl localhost:9200/_types
或
感谢您提供的帮助。您所谓的“类型”实际上是一种“映射类型”,获取它们的方法很简单,只需使用:
curl -XGET localhost:9200/_all/_mapping
现在,因为您只需要映射类型的名称,所以不需要安装任何东西,因为您只需使用Python即可从之前的响应中获得所需的内容:
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);'
Python脚本做的事情非常简单,即它迭代所有索引和映射类型,只检索后者的名称:
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);'
更新
因为您使用的是Ruby,所以使用Ruby代码也可以使用相同的技巧:
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} }"
Ruby脚本如下所示:
require 'rubygems';
require 'json';
resp = JSON.parse(STDIN.read);
resp.each { |index, indexSpec |
indexSpec['mappings'].each { |type, fields|
puts type
}
}
private Set getTypes(String indexName)引发异常{
HttpClient=HttpClients.createDefault();
HttpGet mappingsRequest=新的HttpGet(getServerUri()+“/”+getIndexName()+“/”映射);
HttpResponse scanScrollResponse=client.execute(映射请求);
String response=IOUtils.toString(ScanSrollResponse.getEntity().getContent(),Charset.defaultCharset());
System.out.println(响应);
字符串映射=((JSONObject)JSONSerializer.toJSON(JSONObject.fromObject(response).get(indexName).toString()).get(“映射”).toString();
Set types=JSONObject.fromObject(mappings.keySet();
返回类型;
}
您只需打印索引并使用_-mapping API,就可以只看到索引中的“映射”部分
例如:curl-GEThttp://localhost:9200/YourIndexName/_mapping?pretty
你会得到这样的结果:
{
"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和mapping_type_name_2是此索引中的类型,您还可以看到这些类型的结构
这里有关于映射类型的很好的解释:ES中没有
\u type
,如果您只对类型感兴趣,那么可以看看@Andrew White,您需要这样做,谢谢。当我阅读文档(和其他web资源)时,我得到的印象是映射在字段/属性级别,而不是类型级别。_-mapping命令确实给出了我想要的东西,但是我使用的是Ruby,而不是Python,所以我必须弄清楚如何在Ruby中解析结果。现在,我只是通过尝试不同的查询排列来尽可能多地理解API。和往常一样,我感谢你的帮助我已经用一些等价的Ruby代码更新了我的答案。