Elasticsearch索引的最后更新时间

dch*_*har 11 elasticsearch

有没有办法从ElasticSearch信息中检索上次更新特定索引的时间?我的目标是能够告诉最后一次在索引中插入/更新/删除任何文档的时间.如果这是不可能的,我可以在索引修改请求中添加一些内容,以便稍后提供此信息吗?

Oll*_*ank 9

您可以从_timestamp获取修改时间

为了更容易返回时间戳,您可以设置Elasticsearch来存储它:

curl -XPUT "http://localhost:9200/myindex/mytype/_mapping" -d'
{
  "mytype": {
      "_timestamp": {
          "enabled": "true",
          "store": "yes"
      }
  }
}'
Run Code Online (Sandbox Code Playgroud)

如果我插入一个文档,然后查询它,我得到时间戳:

 curl -XGET 'http://localhost:9200/myindex/mytype/_search?pretty' -d '{
>  fields : ["_timestamp"],
>    "query": {
>     "query_string": { "query":"*"}
>    }
> }'
{
   "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
     "total" : 1,
     "max_score" : 1.0,
     "hits" : [ {
       "_index" : "myindex",
       "_type" : "mytype",
       "_id" : "1",
       "_score" : 1.0,
       "fields" : {
        "_timestamp" : 1417599223918
      }
    } ]
  }
}
Run Code Online (Sandbox Code Playgroud)

更新现有文件:

curl -XPOST "http://localhost:9200/myindex/mytype/1/_update" -d'
{
  "doc" : {
      "field1": "data",
      "field2": "more data"
  },
  "doc_as_upsert" : true
}'
Run Code Online (Sandbox Code Playgroud)

重新运行上一个查询会显示更新的时间戳:

  "fields" : {
    "_timestamp" : 1417599620167
  }
Run Code Online (Sandbox Code Playgroud)

  • 这在2.0中已弃用,有没有其他方法可以获得自动生成的时间戳? (7认同)
  • 我会做一个返回最大_timestamp 的聚合。如果您也需要该文档,您可以使用时间戳运行第二次搜索。您在第二点上是正确的,如果文档被删除,您将无法搜索它。 (2认同)

mil*_*ion 5

我不知道是否有人正在寻找等效的解决方案,但这里有一个针对 > Elasticsearch 5 用户使用分片统计信息的解决方法:curl XGET http ://localhost:9200/_stats?level=shards

正如您将看到的,每个索引、提交和/或刷新都有一些信息,您可以使用它们来查看索引是否更改(或未更改)。

我希望它能帮助某人。