Elasticsearch索引上次更新时间

mbyulnm0  于 2023-06-21  发布在  ElasticSearch
关注(0)|答案(4)|浏览(254)

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

vhipe2zx

vhipe2zx1#

您可以从_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
  }
zf2sa74q

zf2sa74q2#

我不知道是否有人正在寻找一个等价的,但这里有一个使用分片统计数据的解决方案> Elasticsearch 5用户:curl XGET http://localhost:9200/_stats?level=shards
正如你将看到的,你有一些关于每个索引、提交和/或刷新的信息,你可以用它们来查看索引是否改变了(或没有改变)。
我希望它能帮助某人。

lrpiutwd

lrpiutwd3#

我只是在寻找解决这个问题的方法。最近的Elasticsearch版本有一个<index>/_recovery API。
这将返回一个分片列表和一个名为stop_time_in_millis的字段,该字段看起来像是最后一次写入该分片的时间戳。

bvhaajcl

bvhaajcl4#

在某些用例中,一个简单的解决方案可能足够了,也可以查看ElasticSearch用于存储其数据的文件,并根据修改时间对这些文件进行排序,例如:
sudo find /var/lib/elasticsearch/ -type f -exec stat -c“%y - %n”{} ;|排序-k 1,2
这将给予一个保守的估计,在这个意义上,数据肯定不会在具有最新时间戳的文件之后修改。

相关问题