如何获取当前时间作为Unix时间戳以供脚本使用 [英] How to get current time as unix timestamp for script use

查看:83
本文介绍了如何获取当前时间作为Unix时间戳以供脚本使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个字段:

  • date_start(类型:日期)
  • date_end(类型:日期)
  • 永久(类型:布尔)

我想退回所有符合以下条件的文件:

I would like to return all documents with theses conditions:

date_start <= now AND date_end >= now
OR
date_start <= now AND permanent == true

做到这一点的最佳方法是什么?

What is the best way to do that ?

我认为应该使用这样的脚本:

I thought it would be to use a script like this :

{
 "query": {
   "bool": {
     "filter": [
       {
         "script": {
           "script": {
             "source": "((doc['date_start'].value <= params.now) && (doc['date_end'].value >= params.now)) || ((doc['date_start'].value <= params.now) && (doc['permanent'].value == params.permanent))"
           },
           "lang": "painless",
           "params": {
             "now": "1594390526",
             "permanent": true
           }
         }
       }
     ]
   }
 }
}

但是日期类型比较存在问题,我不知道该如何解决.谢谢

But there is an issue with date types comparison and I don't know how to solve this. Thank you

推荐答案

感谢joe的回答.我没有正确实现它,但是我认为这是脚本解决方案的一个很好的起点.

Thanks joe for your answer. I did not manage to implement it correctly but I think this is a good starting point for a script solution.

但是,我找到了另一种方法,可以结合使用range和bool/should.

However, I find another way to do what I wanted using combination of range and bool/should.

是这里:

{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "date_start": {
              "lte": "now"
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "range": {
                  "date_end": {
                    "gte": "now"
                  }
                }
              },
              {
                "term": {
                  "permanent": true
                }
              }
            ]
          }
        }
      ]
    }
  }
}

这篇关于如何获取当前时间作为Unix时间戳以供脚本使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆