Elasticsearch中Script用法代替sql中a小于b条件
如果real_follow_time为空,判断条件为real_follow_time比预定follow_up_time时间加40天大,
如果real_follow_time不为空,判断条件为现在时间比预定follow_up_time时间加40天大。
1 2 3 4 5
| IF( follow.real_follow_time, date_format(follow.real_follow_time, '%Y-%m-%d') >date_format(date_add(follow.follow_up_time, INTERVAL 40 DAY), '%Y-%m-%d'), now() > date_format(date_add(follow.follow_up_time, INTERVAL 40 DAY), '%Y-%m-%d') )
|
将上面的sql改写成es,查阅发现es还有种script语法里可写painless这种脚本语言
1 2 3 4 5 6 7 8 9 10 11 12 13
| { "query": { "bool": { "filter": [ { "script": { "script": "if (doc['real_follow_time'].size()==0) {return (( now() - doc['follow_up_time'].value.toInstant().toEpochMilli()) / (3600000.0*24)) > 40.0 } else {return ((doc['real_follow_time'].value.toInstant().toEpochMilli() - doc['follow_up_time'].value.toInstant().toEpochMilli()) / (3600000.0*24)) > 40.0 }}" } } ] } } }
|
翻译成脚本语言就是这样,但是这种脚本语言不支持now()这种获取时间的方法,查资料说是es是分布式的每个机器获取时间可能不一样,但是翻译成java代码只要把时间生成在拼接上去就行了

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| if (ObjectUtils.isNotEmpty(req.getBeOverdue()) && req.getBeOverdue()) { LocalDateTime now = DateUtil.dateToLocalDateTime(new Date());
Map<String, Object> params = new HashMap<>(); long time = System.currentTimeMillis(); String str="if (doc['real_follow_time'].size()==0) {return (($str - doc['follow_up_time'].value.toInstant().toEpochMilli()) / (3600000.0*24)) > 40.0 } else {return ((doc['real_follow_time'].value.toInstant().toEpochMilli() - doc['follow_up_time'].value.toInstant().toEpochMilli()) / (3600000.0*24)) > 40.0 }"; str=str.replace("$str",time+"L"); Script script = new Script(ScriptType.INLINE,"painless",str,params); ScriptQueryBuilder scriptQueryBuilder = new ScriptQueryBuilder(script); boolQueryBuilder.filter(scriptQueryBuilder); }
|