# How could I compare these two time values using painless script?

**URL:** <https://discuss.elastic.co/t/how-could-i-compare-these-two-time-values-using-painless-script/291034>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [December 6, 2021, 11:58am UTC](https://discuss.elastic.co/t/how-could-i-compare-these-two-time-values-using-painless-script/291034 "2021-12-06T11:58:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peilin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peilin/32/98389_2.png) [@Peilin](https://discuss.elastic.co/u/Peilin)\
**Post date:** [December 6, 2021, 11:58am UTC](https://discuss.elastic.co/t/how-could-i-compare-these-two-time-values-using-painless-script/291034/1 "2021-12-06T11:58:52Z")

</div>

![image](https://us1.discourse-cdn.com/elastic/original/3X/2/7/27e9745cfa8e028060eac0347882214644a40849.png)  
I am using ES to analysis medical data. how can i using painless script compare these two time values(beginDateTime, endDateTime).deep values. if it's not deep value, i found a solution like this

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "script": {
            "script": "if (doc['basic.visitStartDateTime'].size()==0 || doc['basic.visitEndDateTime'].size()==0) {return false} else {return doc['basic.visitStartDateTime'].value.toInstant().toEpochMilli() >= doc['basic.visitEndDateTime'].value.toInstant().toEpochMilli()}"
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [December 6, 2021, 1:29pm UTC](https://discuss.elastic.co/t/how-could-i-compare-these-two-time-values-using-painless-script/291034/2 "2021-12-06T13:29:18Z")

</div>

What is the expected result you are look for from these two values? The documentation has multiple [examples](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-datetime.html) on doing datetime math and differences.

---

<div class="post-metadata">

**Author:** ![Peilin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peilin/32/98389_2.png) [@Peilin](https://discuss.elastic.co/u/Peilin)\
**Post date:** [December 7, 2021, 1:14am UTC](https://discuss.elastic.co/t/how-could-i-compare-these-two-time-values-using-painless-script/291034/3 "2021-12-07T01:14:44Z")

</div>

Hi Aron, thanks for your kindly reply, what puzzles me is the data structure, I don't know how to use the painless script in DSL to get these two values. In Kibana, Lucene is easy to get the values, but can't compare them.

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [December 7, 2021, 1:34pm UTC](https://discuss.elastic.co/t/how-could-i-compare-these-two-time-values-using-painless-script/291034/4 "2021-12-07T13:34:26Z")

</div>

I can help you out but need to know what the result from the 2 values you are looking for. Difference in days, minutes, seconds, etc?

Using the example you posted you have a `beginDateTime` and `endDateTime`. What's the new field you want created and the value of that field?

---

<div class="post-metadata">

**Author:** ![Peilin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peilin/32/98389_2.png) [@Peilin](https://discuss.elastic.co/u/Peilin)\
**Post date:** [December 8, 2021, 2:46am UTC](https://discuss.elastic.co/t/how-could-i-compare-these-two-time-values-using-painless-script/291034/5 "2021-12-08T02:46:00Z")

</div>

🌞 Goodmorning  
I want to compare the beginDateTime and endDateTime exact to second (yyyy-mm-dd hh24:mi: ss).  
If the endDateTime is before \< the beginDateTime, returned all query records.  
No need to create a new field, just use these query records in Kibana（Find records with time logic errors in medical data）

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [December 8, 2021, 12:53pm UTC](https://discuss.elastic.co/t/how-could-i-compare-these-two-time-values-using-painless-script/291034/6 "2021-12-08T12:53:16Z")

</div>

I believe this is what you are looking for.

```auto
POST peilin/_doc/1
{
  "id": 1,
  "beginDateTime": "2021-10-25T00:00:00",
  "endDateTime": "2021-10-24T00:00:00"
}

POST peilin/_doc/2
{
  "id": 2,
  "beginDateTime": "2021-10-25T00:00:00",
  "endDateTime": "2021-10-23T00:00:00"
}

POST peilin/_doc/3
{
  "id": 3,
  "beginDateTime": "2021-10-25T00:00:00",
  "endDateTime": "2021-10-26T00:00:00"
}

GET peilin/_search
{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": """
            if (doc['endDateTime'].value.isBefore(doc['beginDateTime'].value)) {
              return true;
            }
            return false;
          """
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Peilin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peilin/32/98389_2.png) [@Peilin](https://discuss.elastic.co/u/Peilin)\
**Post date:** [December 10, 2021, 8:25am UTC](https://discuss.elastic.co/t/how-could-i-compare-these-two-time-values-using-painless-script/291034/7 "2021-12-10T08:25:59Z")

</div>

Hi Aron, thanks for your help. 😀

Now. I solved this problem. it's a nested layer query problem, I am not familiar with the solution of this kind matter before.  
Here is my code. Hope it can be helpful to those who also have problems with query nested data

```auto
GET /a-case-l2-recent/_search
{
  "query": {
     "nested": {
      "path": "stay.dept4Ds.slices",
         "query": {
           "bool": {
              "must": [
                         {
                          "script": {
                             "script": {
                                "source": """
                                if(doc['stay.dept4Ds.slices.beginDateTime.keyword'].size()==0 || doc['stay.dept4Ds.slices.endDateTime.keyword'].size()==0){
                                  return false
                                }else{
                                if(doc['stay.dept4Ds.slices.beginDateTime.keyword'].value.compareTo(doc['stay.dept4Ds.slices.endDateTime.keyword'].value) <0){return false} else {return true }
                                }
                               """
                               }
                          }
                         }
              ]
           }
         }
     }
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 7, 2022, 8:26am UTC](https://discuss.elastic.co/t/how-could-i-compare-these-two-time-values-using-painless-script/291034/8 "2022-01-07T08:26:39Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
