【问题标题】:How to query and list all types within an elasticsearch index?如何查询和列出弹性搜索索引中的所有类型?
【发布时间】:2016-03-12 19:43:58
【问题描述】:

问题: 在 elasticsearch 中简单地查询和列出特定索引(和所有索引)中的所有类型的最正确方法是什么?

我一直在阅读参考资料和 API,但似乎找不到任何明显的东西。

我可以使用以下命令列出索引:

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

我可以使用以下命令获取统计信息(似乎不包括类型):

$ curl localhost:9200/_stats

我希望会有一个简单的命令,如下所示:

$ curl localhost:9200/_types

$ curl localhost:9200/index_name/_types

感谢您提供的任何帮助。

【问题讨论】:

  • ES 中没有_type,如果你只对类型感兴趣,那么看看@Andrew White 的answer here 你需要install jq 来解决这个问题

标签: indexing elasticsearch types


【解决方案1】:

你所说的“类型”实际上是一个“映射类型”,获取它们的方法很简单:

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

【讨论】:

  • 谢谢。当我阅读文档(和其他网络资源)时,我得到的印象是映射是在字段/属性级别,而不是在类型级别。 _mapping 命令肯定给了我我正在寻找的东西,但我使用的是 Ruby,而不是 Python,所以我将不得不弄清楚如何在 Ruby 中解析结果。现在,我只是试图通过尝试不同的查询排列来尽可能多地理解 API。与往常一样,我感谢您的帮助。 -- 谢谢
  • 我已经用一些等效的 Ruby 代码更新了我的答案。
【解决方案2】:

您可以只打印索引并使用 _mapping API,这样您就只能在索引中看到“映射”部分。

例如:curl -GET http://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 是这个索引中的类型,你也可以看到这些类型的结构。

关于 mapping_types 的好解释在这里:https://logz.io/blog/elasticsearch-mapping/

【讨论】:

    【解决方案3】:
    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-25
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      • 2018-05-12
      相关资源
      最近更新 更多