有没有办法从ElasticSearch信息中检索上次更新特定索引的时间?我的目标是能够告诉最后一次在索引中插入/更新/删除任何文档的时间.如果这是不可能的,我可以在索引修改请求中添加一些内容,以便稍后提供此信息吗?
您可以从_timestamp获取修改时间
为了更容易返回时间戳,您可以设置Elasticsearch来存储它:
curl -XPUT "http://localhost:9200/myindex/mytype/_mapping" -d' { "mytype": { "_timestamp": { "enabled": "true", "store": "yes" } } }'
如果我插入一个文档,然后查询它,我得到时间戳:
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 } } ] } }
更新现有文件:
curl -XPOST "http://localhost:9200/myindex/mytype/1/_update" -d' { "doc" : { "field1": "data", "field2": "more data" }, "doc_as_upsert" : true }'
重新运行上一个查询会显示更新的时间戳:
"fields" : { "_timestamp" : 1417599620167 }