# Elasticsearch groovy script query Not working properly

**URL:** <https://discuss.elastic.co/t/elasticsearch-groovy-script-query-not-working-properly/240540>\
**Category:** Elasticsearch\
**Created:** [July 9, 2020, 2:19pm UTC](https://discuss.elastic.co/t/elasticsearch-groovy-script-query-not-working-properly/240540 "2020-07-09T14:19:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhishek\_Kumar5](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@Abhishek\_Kumar5](https://discuss.elastic.co/u/Abhishek_Kumar5)\
**Post date:** [July 9, 2020, 2:19pm UTC](https://discuss.elastic.co/t/elasticsearch-groovy-script-query-not-working-properly/240540/1 "2020-07-09T14:19:58Z")

</div>

Hi Everybody,

I have a issue. Want your review on it to fix.

```auto
Mapping

   { "supply": {"properties": {
  "rotation_list": {
    "type": "nested",
    "properties": {
      "project_end_date": {
        "type": "nested",
        "properties": {
          "end_date": {
            "format": "yyyy-MM-dd",
            "type": "date"
          }
        }
      },
      "total_days": {
        "type": "integer"
      }
    }
  }
}}}

    data

{"rotation_list": [
    {
          "project_end_date": [
                  {
                       "end_date": "2020-08-07"
                  },
                  {
                       "end_date": "2020-07-07"
                 }
            ],
              "total_days": 23
          },
         {
        "project_end_date": [
             {
                   "end_date": "2020-08-07"
             }
         ],
             "total_days": 26
   }]}

Query:
{"query": {"bool": {
  "filter": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "rotation_list.project_end_date",
            "query": {
              "script": {
                "script": {
                  "lang": "groovy",
                  "inline": "def ratable =false;def now_date = new Date();for(i in doc['rotation_list.project_end_date.end_date'].values){def date_check = [];for (j in i){def long_to_str = new Date(j).format('yyyy-MM-dd');def compare_date = Date.parse('yyyy-MM-dd', long_to_str);if (compare_date < now_date) {date_check.add(true);} else {date_check.add(false);}};if (!date_check.contains(false)){ratable = true;break}else{ratable = false}};if(ratable){return true}else {return false}"
                }
              }
            }
          }
        }
      ]
    }
  }
}}}

```

So the query is not working as i expect. So basically i was trying to check the group end date of each project section less than the current date then only return.  
Idea is to check each end date of project end date. In the first project\_end\_date section there are two end\_dates. One is less then current date and other is greater then current date. Both should be less then current date. Then only return result. Same for second project\_end\_date section. This one also greater then current date. So no data should give as result. But i am getting the result. I am using elasticsearch 5.6. Any suggestion where i am doing mistake.

---

<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:** [August 6, 2020, 2:20pm UTC](https://discuss.elastic.co/t/elasticsearch-groovy-script-query-not-working-properly/240540/2 "2020-08-06T14:20:07Z")

</div>

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