Passing complex parameters to JSON Input

I'm using JSON input for a kibana visualization and I've read that passing parameters can improve performance. Can I pass something more than just a simple string or number? I'd like to pass the current date in milliseconds so I can get the week to date sales:

{
  "script": {
    "inline": "Calendar today = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek()); if (doc['i.Transaction Date'].date.getMillis() <= today.getTimeInMillis() && doc['i.Transaction Date'].date.getMillis()>=cal.getTimeInMillis()/86400000*86400000) { return doc['i.Sales'].value; } return 0;",
    "lang": "painless",
    "params" :  {
      "currentDateInMillis" : Calendar.getInstance().getTimeInMillis()
    }
  }
}

However, this doesn't parse for me. (Note, I'm not using the param in the JSON Input here, just testing to see if it parses).

Any ideas?

The JSON input is sent to Elasticsearch as is, so the Calendar.getInstance().getTimeInMillis() part isn't going to parse as it is not valid JSON. We could add a feature to Kibana that allows requesting specific parameters, but I doubt it will be very broadly useful and might be tricky to implement correctly.

Can you describe in a bit more detail what you are using this script to do? I suspect there is a way to do it without scripting, but perhaps not.

I'm trying to get to-date metrics, like Year to date, month to date, etc, in Kibana. I'd like to display these in a table.

Interesting, how are you doing that today? Can you share a screenshot of your visualization that shows the config?

Sure. I could also split rows with a Date Range bucket, but that would list my to-dates vertically, rather than across which is preferable.

1 Like

Are you able to share the aggregation part of the setting as well? I'm just trying to replicate something similar based on seeing this, and I'm missing part of your approach :slight_smile:

I'm using a sum aggregation on my i.Sales field. Is that what you're looking for?

That's the main metric agg right, what about the bucket?

I'm using a filter, and then trying to split rows using a weekly date histogram using a variant on your script

I'm using a Terms bucket with a keyword field like Sales Rep.

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