I have a painless in elasticsearch, like:
POST _scripts/painless/calculate-price
{
"script": "Map currencyMap = ['USD': 6.8, 'RUB': 0.122]; return doc['price'] * currencyMap[doc['currency']];"
}
I use this script to sort data, and size of currencyMap has huge impact on time cost.
So is there a way in painless to implement something like singleton, so I can initialize currencyMap only once and use many times?
Any help would be appreciated.
PS:
Following is my test script, document count of test is 5738306.
It took about 2000ms(avg) by calculate-score, and 600ms(avg).
POST _scripts/calculate-score
{
"script": {
"lang": "painless",
"source": "Map currencyMap = ['A': 6.8, 'B': 0.122, 'BC': 0.122, 'C': 0.122, 'D': 0.122, 'E': 0.122, 'F': 0.122, 'G': 0.122, 'H': 0.122, 'I': 0.122, 'J': 0.122, 'K': 0.122, 'L': 0.122, 'M': 0.122, 'N': 0.122, 'O': 0.122, 'P': 0.122, 'Q': 0.122, 'R': 0.122, 'S': 0.122, 'T': 0.122, 'U': 0.122, 'V': 0.122, 'W': 0.122, 'X': 0.122, 'BY': 0.122, 'BZ': 0.122, 'AB': 0.122, 'BB': 0.122, 'CB': 0.122]; def price = doc['price'].getValue(); def currency = doc['currency']; if(doc['currency'] == null) {doc['currency'] = 'A';} def c = currencyMap[currency]; if(c == null) {c = 0.11;}return price * c;"
}
}
POST _scripts/calculate-score2
{
"script": {
"lang": "painless",
"source": "Map currencyMap = ['A': 6.8]; def price = doc['price'].getValue(); def currency = doc['currency']; if(doc['currency'] == null) {doc['currency'] = 'A';} def c = currencyMap[currency]; if(c == null) {c = 0.11;}return price * c;"
}
}
GET /test/_search
{
"_source": ["price", "currency"],
"sort" : {
"_script" : {
"type" : "number",
"script" : {
"id": "calculate-score"
},
"order" : "desc"
}
}
}
GET /test/_search
{
"_source": ["price", "currency"],
"sort" : {
"_script" : {
"type" : "number",
"script" : {
"id": "calculate-score2"
},
"order" : "desc"
}
}
}