Elasticsearch索引的最后更新时间

 笨蛋說愛我8_382 发布于 2022-12-08 16:34

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

1 个回答
  • 您可以从_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
      }
    

    2022-12-11 02:09 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有