有没有办法从ElasticSearch信息中检索上次更新特定索引的时间?我的目标是能够告诉最后一次在索引中插入/更新/删除任何文档的时间.如果这是不可能的,我可以在索引修改请求中添加一些内容,以便稍后提供此信息吗?
您可以从_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)
我不知道是否有人正在寻找等效的解决方案,但这里有一个针对 > Elasticsearch 5 用户使用分片统计信息的解决方法:curl XGET http ://localhost:9200/_stats?level=shards
正如您将看到的,每个索引、提交和/或刷新都有一些信息,您可以使用它们来查看索引是否更改(或未更改)。
我希望它能帮助某人。
| 归档时间: |
|
| 查看次数: |
19444 次 |
| 最近记录: |