Compile Error | Watcher Condition

Hi,

I am trying to create a loop where all the arrays are searched to find any have more than one value for "doc_count". Once the loop finishes, and all the fields are identified, I wish to put these in a separate array to use this for my action.

When I execute the condition, I get a compile error. Can you please help me?

// Executed Output

// Console thus far

Thank you in advance :slight_smile:

hey,

new is a reserved keyword in painless. Use something like new_list and the script should compile at that stage.

--Alex

Thanks for the quick response

Unfortunately, I am still getting a compile error

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          "... ransform.add(document) } ; return cctx.payload.agg ...",
          "                             ^---- HERE"
        ],
        "script": "ctx.payload.aggregations.managedobjectref_agg.transform = []; def new_list = []; for (int j=0;j<ctx.payload.aggregations.managedobjectref_agg.hits.hits;j++){ new_list = ['bob': ctx.payload.hits.hits[j].buckets.timerange_agg.buckets.0.doc_count >= 1]; ctx.payload.aggregations.managedobjectref_agg.transform.add(document) } ; return cctx.payload.aggregations.managedobjectref_agg.transform",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "compile error",
    "script_stack": [
      "... ransform.add(document) } ; return cctx.payload.agg ...",
      "                             ^---- HERE"
    ],
    "script": "ctx.payload.aggregations.managedobjectref_agg.transform = []; def new_list = []; for (int j=0;j<ctx.payload.aggregations.managedobjectref_agg.hits.hits;j++){ new_list = ['bob': ctx.payload.hits.hits[j].buckets.timerange_agg.buckets.0.doc_count >= 1]; ctx.payload.aggregations.managedobjectref_agg.transform.add(document) } ; return cctx.payload.aggregations.managedobjectref_agg.transform",
    "lang": "painless",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "unexpected token [';'] was expecting one of [<EOF>]."
    }
  },
  "status": 400
}

The error is after.add(document). I'm assuming this means my loop did not go into a separate document
I would appreciate your insight on this

please take the time to properly format your scripts instead of having them inside of the JSON, take this example

ctx.payload.aggregations.managedobjectref_agg.transform = []; 
def new_list = []; 
for (int j=0;j<ctx.payload.aggregations.managedobjectref_agg.hits.hits;j++){ 
  new_list = ['bob': ctx.payload.hits.hits[j].buckets.timerange_agg.buckets.0.doc_count >= 1]; 
  ctx.payload.aggregations.managedobjectref_agg.transform.add(document) 
} ; 
return cctx.payload.aggregations.managedobjectref_agg.transform",

Few things:

  • typo at cctx.
  • mustache style index usage buckets.0.doc_count, that must be buckets[0].doc_count.
  • new_list is used as a map but initialize it as a list.
  • missing semicolon at the last line of the for loop.

debugging tip: When using kibana console, instead of using "script" : "your...script" use

"script": """
def foo...
return ...;
"""

those triple ticks allow you to write scripts in multiple lines, make them more readable and the console is doing all the escaping and using single ticks when sending to ES.

Thanks as always for your help @spinscale

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