我有这个样本数据
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
最佳答案
好的开始,您的脚本几乎包含了正确的逻辑,您只需要向正常价格添加一个常量,就可以在下面的回复中看到。
以下脚本可以帮助您按预期方式对结果进行排序。脚本的注释中内联了它的工作方式:
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
]
}
]