2015-12-08 2458 views
17

問題:什麼是最簡單的方法來查詢和列出elasticsearch中特定索引(和所有索引)中的所有類型?如何查詢並列出elasticsearch索引內的所有類型?

我一直在閱讀引用和API,但似乎無法找到任何明顯的東西。

我可以列出與命令指數:

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

我可以得到統計數據(這似乎不包括種)命令:

$ curl localhost:9200/_stats 

我期望有倒是是一個簡單的命令簡單:

$ curl localhost:9200/_types 

$ curl localhost:9200/index_name/_types 

感謝您提供任何幫助。

+3

ES中沒有'_type' ,如果你只對類型感興趣,那麼看看@Andrew White的[在這裏回答](http://stackoverflow.com/questions/31087204/get-all-index-and-types-names-from-cluster-in -elasticsearch)你需要[安裝jq](https://stedolan.github.io/jq/download/)爲 – ChintanShah25

回答

20

你所說的「類型」究竟是「映射類型」,並得到他們的方法就是使用:

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);' 

UPDATE

由於您使用的紅寶石,同樣的招數可通過使用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 
    } 
} 
+1

謝謝。在閱讀文檔(和其他網絡資源)時,我得到了一種印象,即映射是在字段/屬性級別,而不是在類型級別。 _mapping命令明確地給了我所尋找的東西,但我使用的是Ruby,而不是Python,所以我將不得不弄清楚如何在Ruby中解析結果。現在,我只是嘗試通過嘗試不同的查詢排列來儘可能多地理解API。一如既往,我感謝幫助。 - Thx –

+0

我已經用一些等價的Ruby代碼更新了我的答案。 – Val

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; 
} 
相關問題