嘿~ 今天天气不错嘛

es7.3怎么生成日期相差天数的字段

Elasticsearch | 作者 a1350018340 | 发布于2020年03月22日 | 阅读数:2951

初学者请教一下各位大佬,怎么使用es的script_fields生成一个文档的createTime字段与当前时间相差天数的字段?我打算使用这个字段调整召回的_score
下面附上mapping
{
"mapping": {
"properties": {
"createTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"id": {
"type": "integer"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}
}
随便拿一条数据
{
"_index" : "article",
"_type" : "_doc",
"_id" : "12",
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"id" : 12,
"createTime" : "2020-01-11 13:18:47",
"title" : "如何学习Springboot"
}
}

就是用这个createTime字段得出与目前相差多少天
已邀请:

tacsklet - 公司有用到es

赞同来自: a1350018340

写了一个,不太优雅,你可以再优化一下,抛砖引玉。
 
{
"query":{
"match_all":{

}
},
"script_fields":{
"dis_date":{
"script":{
"lang":"painless",
"source":"new Date().getTime() - doc['createTime'].value.toLocalDateTime().toInstant(ZoneOffset.of('+8')).toEpochMilli()"
}
}
}
}

要回复问题请先登录注册