4

I have an Elastic Search cluster with a lot of nice data, that I have created some nice Kibana dashboards for.

For the next level I decided to take a look at scripted fields to make some of the dashboards even nicer.

I want to translate some of the numeric fields into more easily understandable text values. As an example of what I want to do and what I have tried I will use the http response status code field, that most will understand quite easily but also illustrates the problem.

We log the numeric status code (200, 201, 302, 400, 404, 500 etc.) I can create a data table visualization that tells me the count for each of these status codes. But I would like to display the text reason in my dashboard.

I can create a painless script with a lot of IF statements like this:

if (doc['statuscode'].value == 200) {return "OK";}
if (doc['statuscode'].value == 201) {return "Created";}
if (doc['statuscode'].value == 400) {return "Bad Request";}
return doc['statuscode'].value;

But that isn't very nice I think.

But since I will most likely have about 150 different values and that list won't change very often, so I can live with maintaining a static map. But I haven't found any examples of implementing a map or dictionary in painless scripting.

I was thinking of implementing something like this:

Map reasonMap;
reasonMap[200] = 'OK';
reasonMap[201] = 'Created';

def reason = reasonMap[doc['statuscode'].value];
if (reason != null)
{
   return reason;
}

return doc['statuscode'].value;

I haven't been able to make this code work though. The question is also if this will perform well enough for a map with up to 150 values.

Thanks

EDIT

After some trial and error... and a lot of googling, this is what I came up with that works (notice that the key needs to start with a character and not a number):

def reasonMap = 
[
  's200': 'OK',
  's201': 'Created'
];
def key = 's' + doc['statuscode'].value

def reason = reasonMap[key];
if (reason != null)
{
   return reason;
}

return doc['statuscode'].value;
Jay Pete
  • 4,123
  • 4
  • 35
  • 51
  • Thanks, this was perfect! You might add a semicolon to your `def key` line. It should read `def key = 's' + doc['statuscode'].value` – Cookie Monster Apr 03 '19 at 14:40

1 Answers1

0

Should it be

def reason = reasonMap[doc['statuscode']value];

It will perform well with a Map of 150 values.

user699848
  • 109
  • 7