Preface

  • This article is valid for Elasticsearch 7.13
  • Create time (create_time) did not find a good way to achieve
  • If the data stored in the database is no longer updated, update_time in the text can be equivalent to create_time

update_time example

PUT _ingest/pipeline/add_update_time
{
  "processors": [
    {
      "script": {
        "lang": "painless",
        "source": """DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); df.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); Date date = new Date(); ctx.update_time = df.format(date);"""
      }
    }
  ]
}
  • Create an index and set the default pipeline
PUT my_index
{
  "settings": {
    "index.default_pipeline": "add_update_time"
  }
}
  • Insert data
POST my_index/_doc/1
{
  "title": "nothing"
}
  • View mapping
GET my_index/_mapping
{
  "my_index" : {
    "mappings" : {
      "properties" : {
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "update_time" : {
          "type" : "date"
        }
      }
    }
  }
}
  • Query data
GET my_index/_search
{
  "query": {
    "range": {
      "update_time": {
        "time_zone": "+08",        
        "gte": "2021-07-28T16:00:00", 
        "lte": "now"                  
      }
    }
  }
}
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "update_time" : "2021-07-28T18:03:35+08",
          "title" : "nothing"
        }
      }
    ]
  }
}
This article is from qbit snap

qbit
268 声望279 粉丝