2014-12-02 172 views
10

有没有一种方法可以从ElasticSearch信息中检索特定索引上次更新的时间? 我的目标是能够分辨索引中何时最后一次插入/更新/删除任何文档。如果这是不可能的,有什么我可以添加在我的索引修改请求,稍后将提供此信息?Elasticsearch索引上次更新时间

回答

7

你可以从_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 
    } 
+0

谢谢奥利。我试过这个,确实时间戳与文档一起更新。但这只是我需要的一半,因为我想获取最近更新的文档的时间戳。有没有简单的方法来做到这一点,或者我必须对所有具有排序时间戳的文档进行查询并获得最佳结果?另外,如果从索引中删除一个文档,那么将不会有时间戳来表明某些内容已更改,是否正确? – dchar 2014-12-03 11:28:24

+1

我会做一个聚合,返回max _timestamp。如果您也需要该文档,则可以运行使用时间戳记的第二个搜索。你在第二点是正确的,如果文件被删除,你将无法搜索它。 – 2014-12-03 14:25:39

+0

我只需要时间戳。使用最大的聚合它像一个魅力工作。感谢您的帮助! – dchar 2014-12-04 08:52:36

0

我不知道还有谁正在寻找一个相当的人,但在这里是用碎片的统计资料>解决方法Elasticsearch 5用户: 卷曲XGET http://localhost:9200/_stats?level=shards

正如你看到的,你必须每指数一些信息,提交和/或您可能使用,看是否改变指数之(或没有)flushs。

我希望它能帮助别人。