我有这个样本数据
Name: A
Price: 200
PromotionPrice: 100
PromotionStart: 2020/01/01
PromotionEnd: 2020/01/15如果没有晋升,PromotionPrice将为0。
我想把那些有PromotionPrice先开始的(asc或desc)和那些没有PromotionPrice的排序。(过滤掉那些有PromotionPrice但超出促销时间范围)
我试着在_script上工作,但这对我来说太复杂了。
目前正在开发版本7。
我的地图:
"Price" : {"type": "long"}
"PromotionPrice": {"type": "long"},
"PromotionStart": {"type": "date"},
"PromotionEnd": {"type": "date"},我的代码:按Price或PromotionPrice排序,但没有优先考虑那些项目和升级。
"sort": [
{
"_script": {
"type": "number",
"script": {
"lang": "painless",
"params": {
"now": "120000000"
},
"source": "if (doc['PromotionPrice'].value != 0 && params.now <= doc['PromotionEnd'].value.getMillis() && params.now >= doc['PromotionStart'].value.getMillis()) {doc['selling_price'].value} else {doc['normal_price'].value}"
},
"order": "asc"
}
}
]预期结果:
Name: A
Price: 200
PromotionPrice: 100
PromotionStart: 2020/11/01
PromotionEnd: 2020/12/01
Name: B
Price: 500
PromotionPrice: 200
PromotionStart: 2020/11/01
PromotionEnd: 2020/12/01
Name: C
Price: 500
PromotionPrice: 300
PromotionStart: 2020/11/01
PromotionEnd: 2020/12/01
Name: E
Price: 100
PromotionPrice: 0
PromotionStart: 2020/11/01
PromotionEnd: 2020/12/01
Name: D
Price: 500 #have PromotionPrice but treated as not having
PromotionPrice: 300 #cuz out of promotion time range
PromotionStart: 2020/11/01
PromotionEnd: 2020/11/05进一步专题:
基于@Val的逻辑:
如果我想对asc或desc进行排序,下面是那些有晋升的:
now和type将包含在params中
def isPromo = (doc.new_price.value > 0 && doc.start_date.value.getMillis() < Long.parseLong(params.now) && doc.end_date.value.getMillis() > Long.parseLong(params.now));
if (isPromo? && if (params.type == 'asc') {return true;} return false;)
(return doc.new_price.value: (doc.price.value+100000000) return doc.new_price.value: (doc.price.value-100000000遗憾的是这个不起作用
发布于 2020-11-09 10:53:52
很好的开始,您的脚本几乎包含正确的逻辑,您只需要添加一个常数到常规价格,您将在下面的反应中看到。
下面的脚本可以帮助您按照预期的方式对结果进行排序。它的工作方式包含在脚本中的注释中:
POST promotions/_search
{
"sort": {
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": """
// current time
def now = new Date().getTime();
// check if the current product satisfies to the promotion constraints
def isPromo = (doc.PromotionPrice.value > 0 && doc.PromotionStart.value.getMillis() < now && doc.PromotionEnd.value.getMillis() > now);
// if the product qualifies for promotion return its promotion price, otherwise return its normal price + a constant
return isPromo ? doc.PromotionPrice.value : (doc.Price.value + 100000000);
"""
},
"order": "asc"
}
}
}结果:
"hits" : [
{
"_index" : "promotions",
"_type" : "_doc",
"_id" : "ilucrHUB7CBHpnCGfnBn",
"_score" : null,
"_source" : {
"Name" : "A",
"Price" : 200,
"PromotionPrice" : 100,
"PromotionStart" : "2020-11-01",
"PromotionEnd" : "2020-12-01"
},
"sort" : [
100.0
]
},
{
"_index" : "promotions",
"_type" : "_doc",
"_id" : "i1ucrHUB7CBHpnCGfnBn",
"_score" : null,
"_source" : {
"Name" : "B",
"Price" : 500,
"PromotionPrice" : 200,
"PromotionStart" : "2020-11-01",
"PromotionEnd" : "2020-12-01"
},
"sort" : [
200.0
]
},
{
"_index" : "promotions",
"_type" : "_doc",
"_id" : "jFucrHUB7CBHpnCGfnBn",
"_score" : null,
"_source" : {
"Name" : "C",
"Price" : 500,
"PromotionPrice" : 300,
"PromotionStart" : "2020-11-01",
"PromotionEnd" : "2020-12-01"
},
"sort" : [
300.0
]
},
{
"_index" : "promotions",
"_type" : "_doc",
"_id" : "jVucrHUB7CBHpnCGfnBn",
"_score" : null,
"_source" : {
"Name" : "E",
"Price" : 100,
"PromotionPrice" : 0,
"PromotionStart" : "2020-11-01",
"PromotionEnd" : "2020-12-01"
},
"sort" : [
1.000001E8
]
},
{
"_index" : "promotions",
"_type" : "_doc",
"_id" : "jlucrHUB7CBHpnCGfnBn",
"_score" : null,
"_source" : {
"Name" : "D",
"Price" : 500,
"PromotionPrice" : 300,
"PromotionStart" : "2020-11-01",
"PromotionEnd" : "2020-11-05"
},
"sort" : [
1.000005E8
]
}
]https://stackoverflow.com/questions/64749890
复制相似问题