Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

elasticsearch - How to get current time as unix timestamp for script use

I have three fields:

  • date_start (type: date)
  • date_end (type: date)
  • permanent (type: bool)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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

Here it is :

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...