I have this sample data
Name: A
Price: 200
PromotionPrice: 100
PromotionStart: 2020/01/01
PromotionEnd: 2020/01/15
PromotionPrice would be 0 if there is no promotion on them
I would like to sort those have PromotionPrice comming first (asc or desc) and those dont have PromotionPrice coming later. (filter out those have PromotionPrice but out of promotion time range)
I tried working on _script but it's too complicated for me.
Currently working on version 7.
My mapping:
"Price" : {"type": "long"}
"PromotionPrice": {"type": "long"},
"PromotionStart": {"type": "date"},
"PromotionEnd": {"type": "date"},
My code:
Sort by Price or PromotionPrice but did not prioritize those items with promotion first.
"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"
}
}
]
Expected Result:
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
Further topic:
Based on the logic of @Val:
If I would like to sort asc or desc those with promotion above:
with now and type would be included in 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
Sad that this one not working