Elasticsearch groovy script query Not working properly

Hi Everybody,

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

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.

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