我有一个无痛的弹性搜索,比如:
POST _scripts/painless/calculate-price
{
"script": "Map currencyMap = ['USD': 6.8, 'RUB': 0.122]; return doc['price'] * currencyMap[doc['currency']];"
}
我使用这个脚本对数据进行排序,currencyMap的大小对时间开销有很大的影响。
那么,有没有一种无痛苦的方法来实现类似singleton的东西,这样我就可以只初始化currencyMap一次并多次使用?
任何帮助都将不胜感激。
附言:
下面是我的测试脚本,测试的文档计数是5738306。
计算得分时,平均用了2000毫秒,平均用了600毫秒。
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"
}
}
}