返回elasticsearch中的时间戳字段

era*_*ran 13 elasticsearch

为什么在能够通过它过滤查询时看不到_timestamp字段?

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

{
  "fields": [
    "_timestamp",
    "_source"
  ],
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "range": {
          "_timestamp": {
            "from": "2013-01-01"
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

映射是:

{
    "my_doctype": {
        "_timestamp": {
            "enabled": "true"
        },
        "properties": {
            "cards": {
                "type": "integer"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

样本输出:

{
  "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"}
    }]
  }
Run Code Online (Sandbox Code Playgroud)

imo*_*tov 15

启用时间戳字段时,它会被索引但默认情况下不会存储.因此,虽然您可以按时间戳字段进行搜索和过滤,但您无法使用记录轻松检索它.为了能够检索时间戳字段,您需要使用以下映射重新创建索引:

{
    "my_doctype": {
        "_timestamp": {
            "enabled": "true",
            "store": "yes"
        },
        "properties": {
            ...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这样,您就可以将时间戳检索为自纪元以来的毫秒数.

  • 您将无法在现有类型上更新此映射._timestamp映射只能在类型创建时设置. (10认同)

dmw*_*dmw 5

没有必要存储时间戳字段,因为它的确切值被保留为一个术语,它也更可能已经存在于RAM中,特别是如果您正在查询它.您可以使用以下命令通过其术语访问时间戳script_value:

{
    "query": {
        ...
    },
    "script_fields": {
        "timestamp": {
            "script": "_doc['_timestamp'].value"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

从UNIX纪元开始,结果值以毫秒表示.ElasticSearch无法为你做到这一点非常淫秽,但嘿,没有什么是完美的.

  • 这是一个很好的解决方案,只有经过微小的改动后:`script_fields`而不是`script_values`,因为1.4.3脚本必须存在于文件中,根据[this](http://www.elasticsearch.org/guide) /en/elasticsearch/reference/current/modules-scripting.html). (2认同)
  • 根本不起作用.如上所述,必须将`script_values`更改为`script_fields`,并且必须将`_doc ['timestamp']`更改为`_doc ['_ timestamp']`.仅从存储了_timestamp的类型返回正确的值,而在其他类型上,它只为每个文档返回0. (2认同)