How to rewrite this groovy script into painless script ? Facing a lot of issues

My ES document model is as follows

       "hits": [
          {
             "_source": {
                "variant_group": {
                   "variant_info": [
                      {
                         "name": "Toothpaste",
                         "inventory": 10
                      }
                   ],
                   "type_id": 1365
                }
             },
     ]

I want to refactor this groovy script to painless script:

"functions": [
        {
          "script_score": {
            "script": "return _source.variant_group.variant_info.any{i -> i.inventory} ? (_score + (float)doc['variant_group.max_product_per_city_sales_score'].value/100000) : ((float)doc['variant_group.max_product_per_city_sales_score'].value/100000)"
          }
        }
      ],

The new painless script looks like this:

"functions": [
        {
          "script_score": {
            "script": "return params._source.variant_group.variant_info.any(i -> i.inventory) ? (params._score + doc['variant_group.max_product_per_city_sales_score'].value/100000.0) : (doc['variant_group.max_product_per_city_sales_score'].value/100000.0)"
          }
        }
      ],

and it is giving me error:

{
   "error": {
      "root_cause": [
         {
            "type": "script_exception",
            "reason": "runtime error",
            "script_stack": [
               "java.base/java.lang.Class.cast(Class.java:3604)",
               "i -> i.inventory) ? (params._score + doc['variant_group.max_product_per_city_sales_score'].value/100000.0) : (doc['variant_group.max_product_per_city_sales_score'].value/100000.0)",
               "^---- HERE"
            ],
            "script": "return params._source.variant_group.variant_info.any(i -> i.inventory) ? (params._score + doc['variant_group.max_product_per_city_sales_score'].value/100000.0) : (doc['variant_group.max_product_per_city_sales_score'].value/100000.0)",
            "lang": "painless"
         }
      ],
      "type": "search_phase_execution_exception",
      "reason": "all shards failed",
      "phase": "query",
      "grouped": true,
      "failed_shards": [
         {
            "shard": 0,
            "index": "citadel-20200820.3",
            "node": "XaGgi-Y2S-O9uCJLKz5YsA",
            "reason": {
               "type": "script_exception",
               "reason": "runtime error",
               "script_stack": [
                  "java.base/java.lang.Class.cast(Class.java:3604)",
                  "i -> i.inventory) ? (params._score + doc['variant_group.max_product_per_city_sales_score'].value/100000.0) : (doc['variant_group.max_product_per_city_sales_score'].value/100000.0)",
                  "^---- HERE"
               ],
               "script": "return params._source.variant_group.variant_info.any(i -> i.inventory) ? (params._score + doc['variant_group.max_product_per_city_sales_score'].value/100000.0) : (doc['variant_group.max_product_per_city_sales_score'].value/100000.0)",
               "lang": "painless",
               "caused_by": {
                  "type": "class_cast_exception",
                  "reason": "Cannot cast java.lang.Integer to java.lang.Boolean"
               }
            }
         }
      ]
   },
   "status": 400
}

I am getting too many errors while refactoring it. It will be great if anyone can help me with this script.

Thanks!

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