Update a variable in a scripted field definition

Is there a way to update a variable within a scripted field in Kibana? Say I had the following scripted field.

MAP car ["bob":"toyota"];
if (car.containsKey(doc["owner"].value)){
    return car.get(doc["owner"].value);
}

Is it possible through the Kibana API to update the above variable "car" to new mapping?

yes it is, please see example bellow

for "runtime field" BrandOwner

Map car = new HashMap(); 
car.put("bob","toyota");
car.put("jane","honda");
if (doc['owner'].size() == 0) {
	emit ("N/A");
} else {
	def keyMap = doc['owner'].value;
	if (car.containsKey(keyMap)){
		emit (car[keyMap]);
	} else {
		emit ("N/A");
	}		
}

when owner = jane, the field will return honda
when owner = bob, the field will return toyota
when onwer = other, the field will return N/A
if owner does not have value, the field will return N/A

I update my maps from time to time, and I have not experienced any issues.

Thank you. Is there a programmatic way to update the variable car in the above example?

I see you could update the entire field via the Kibana API like with something like: (using BrandOwnder field)

POST <kibana host>:<port>/api/index_patterns/index_pattern/<index_pattern_id>/runtime_field/BrandOwner

Is there a good example of using the API to update the a variable within a field? In this example I would like to change the mapping of car to say MAP car ["bob":"ford","jane","lexus"]; but keep the rest of the logic, via the API.

@uncleAlbert I just edit the runtime field in the UI every time I need to update it (essentially I add more puts). I guess you can do the same thing through the API, you POST the updated code version (all lines, not just some of them), as you mentioned :slight_smile:

I see. I would like a way automate the process, so the API seems to be my best bet.

From my understanding there is no way to update variables within a runtime field or scripted field via the API. You must overwrite the entire field with the new additions.

from top of my head, and getting into a house that I have not been invited to...

I would maybe version the runtime variable code in github or similar, as a file; then a pipeline should trigger a Kibana API rewrite of the whole runtime field taking the code from the file whenever the file is updated, as last step (I would lint and make some basic checks before).

You could also create another automated event on the other side, to update your script inserting a new line and committing it to the repo, whenever an event triggers the need of a new "put" line.

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