Here is the my code:
const convertCurrency = async (currrencyType) => {
try{
const res = await got("https://api.exchangerate-api.com/v4/latest/USD");
return (JSON.parse(res.body)['rates'][currrencyType]);
}
catch(err){
console.log(err);
}
}
async function search(){
try{
client.search({
index : '03776182',
body: {
aggs : {
price_ranges : {
range : {
field : "price",
script : {source : "convertCurrency(/EUR/) * _value"},
ranges : [
{ "from" : 10, "to" : 20 },
]
}
}
}
}
},(err,resp,status)=>{
err ? console.log(err): console.log(JSON.stringify(resp));
});
}
catch(err){console.log(err);}
}
I am trying to insert the custom script inside the source field. But it gives below error.
response: `{"error":{"root_cause":[{"type":"script_exception","reason":"compile error","script_stack":["convertCurrency(/EUR/) * _value"," ^---- HERE"],"script":"convertCurrency(/EUR/) * _value","lang":"painless"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"03776182","node":"C4p_ha96QhK2uB47jV9pxg","reason":{"type":"script_exception","reason":"compile error","script_stack":["convertCurrency(/EUR/) * _value"," ^---- HERE"],"script":"convertCurrency(/EUR/) * _value","lang":"painless","caused_by":{"type":"illegal_state_exception","reason":"Regexes are disabled. Set [script.painless.regex.enabled] to [true] in elasticsearch.yaml to allow them. Be careful though, regexes break out of Painless's protection against deep recursion and long loops."}}}]},"status":500}`,
toString: [Function],
At the same time normal expression are working well.