我有这个样本数据
Name: A
Price: 200
PromotionPrice: 100
PromotionStart: 2020/01/01
PromotionEnd: 2020/01/15
``` `PromotionPrice` 如果没有提升,则为0
我想把这些分类 `PromotionPrice` 先交代( `asc` 或者 `desc` )那些没有 `PromotionPrice` 稍后再来(过滤掉那些有 `PromotionPrice` 但超出促销时间范围)
我试着在 `_script` 但对我来说太复杂了。
### 目前正在开发版本7。
我的Map:
"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
可惜这个不行
1条答案
按热度按时间xdnvmnnf1#
很好的开始,你的脚本几乎包含正确的逻辑,你只需要添加一个常量到常规价格,你会看到我在下面的回应。
下面的脚本可以帮助您按预期的方式对结果进行排序。它的工作方式是在脚本的注解中内联的:
结果: