一尘不染

在ElasticSearch中返回时间戳字段

elasticsearch

为什么在能够过滤查询时无法看到_timestamp字段?

以下查询返回正确的文档,但不会返回时间戳本身。如何返回时间戳?

{
  "fields": [
    "_timestamp",
    "_source"
  ],
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "range": {
          "_timestamp": {
            "from": "2013-01-01"
          }
        }
      }
    }
  }
}

映射为:

{
    "my_doctype": {
        "_timestamp": {
            "enabled": "true"
        },
        "properties": {
            "cards": {
                "type": "integer"
            }
        }
    }
}

样本输出:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "test1",
      "_type" : "doctype1",
      "_id" : "HjfryYQEQL6RkEX3VOiBHQ",
      "_score" : 1.0, "_source" : {"cards": "5"}
    }, {
      "_index" : "test1",
      "_type" : "doctype1",
      "_id" : "sDyHcT1BTMatjmUS0NSoEg",
      "_score" : 1.0, "_source" : {"cards": "2"}
    }]
  }

阅读 1495

收藏
2020-06-22

共1个答案

一尘不染

启用时间戳字段后,默认情况下会对其进行索引但不存储。因此,尽管您可以通过时间戳字段进行搜索和过滤,但是您无法轻松地通过记录来检索它。为了能够检索时间戳字段,您需要使用以下映射重新创建索引:

{
    "my_doctype": {
        "_timestamp": {
            "enabled": "true",
            "store": "yes"
        },
        "properties": {
            ...
        }
    }
}

这样,您将能够检索到时间戳记以来的毫秒数。

2020-06-22