Passing parameters to Painless script

Hi,

Can anyone please explain to me how to use painless file script with parameters? I'm using Elasticsearch 5.x.

I created a very simple script under config/scripts folder called: calculate_distance.painless, the content of this file is:
doc[field_name].arcDistance(latitude,longitude)

field_name, latitude, longitude are parameters.

However as soon as I start Elasticsearch, I saw compilation error on this script: Variable [field_name] is not defined

Why should I define the variable as it supposed to be parameters to be passed in?

Then I tried to define a dummy variable for field_name, latitude, longitude by:
String field_name;
Float latitude;
Float longitude;

And run the query, I got error:
u'caused_by': {u'reason': None, u'type': u'null_pointer_exception'}, u'reason': u'runtime error', u'type': u'script_exception'}

How can I use painless file script with parameters in Elasticsearch?

It seems I cannot even do it via inline script, so below query works:
query = {
"query": {
"match_all":{}
},
"script_fields": {
"distance": {
"script":{
"lang":"painless",
"inline":"doc['address_details.location.point'].arcDistance(-38.376465,144.829099)"
}
}
}
}

Below query doesn't work because I use params:
query = {
"query": {
"match_all":{}
},
"script_fields": {
"distance": {
"script":{
"lang":"painless",
"inline":"doc['address_details.location.point'].arcDistance(latitude,longitude)",
"params":{
"latitude":-38.376465,
"longitude":144.829099
}
}
}
}
}
The error msg is : TransportError(500, u'search_phase_execution_exception', u'compile error')

Seems like they've namespaced the params underneath "params", and not documented it.

Try doc['address_details.location.point'].arcDistance(params.latitude, params.longitude)

3 Likes

Thank you, this sovled the problem.

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