在Elasticsearch中获取索引的日期字段列表

时间:2019-10-08 08:01:29

标签: elasticsearch

我是Elasticsearch的初学者 我想在elasticsearch中编写查询以返回日期(类型=日期)的字段名称。

  

例如在 index3 中,我想返回插入时间 @timestamp time_date

{
  "index3" : {
    "mappings" : {
      "properties" : {
        "@timestamp" : {
          "type" : "date"
        },
        "money" : {
          "type" : "long"
        },
        "time_date" : {
          "type" : "date"
        }
        "parent": {
            "properties": {
              "inserttime": {
                "type": "date"
              },
            }
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

一种方法是利用jq JSON utility

curl -s -XGET localhost:9200/index3 | jq '.index3.mappings.properties | with_entries(select(.value.type | contains("date")))'

运行上面的命令将产生

{
  "@timestamp": {
    "type": "date"
  },
  "time_date": {
    "type": "date"
  }
}

有了Angular,就更容易了:

const indexBody = { "index3: ... };
const dateFields = [];
Object.keys(indexBody.index3.mappings.properties).forEach(field => {
    if (indexBody.index3.mappings.properties[field].type == 'date') {
        dateFields.push(field);
    }
});

console.log(dateFields);

=>结果

[ '@timestamp', 'time_date' ]