ES查询按时时间过滤

数据量:document 大概有2KW+,每个document下有一个nest数组结构,数据量最多能达到上百个,每个数组的元素中有个expire_time,搜索时会通过expire_time进行过滤操作

搜索时:es的时间过滤{"from": {{from}}{{^from}}0{{/from}},

"size": {{size}}{{^size}}100{{/size}},
"query": {
"bool":{
"filter":[
{
"nested":{
"path":"object_list",
"query":{
"bool":{
"filter":[
{
"term":{"object_list.object_id":"{{objectId}}" }
}
,{
"range":{
"object_list.expire_time":{
"gt":"now/m"
} }}]}}}}]}}
}

gt对时间的操作性能有很大的影响(分钟级),在查询时会有很大的抖动,延时很大,在这里我们对数据做一个定时脚本,规避掉这个问题

POST /ptp_tags/_update_by_query?conflicts=proceed
{
"script":{
"inline":"ctx._source.object_list.removeIf((item)->item.expire_time<System.currentTimeMillis())",
"lang":"painless"
},
"query":{
"range":{
"object_list.expire_time":{
"gt":"now/m"}
}}

如果对时间不是特别敏感,定时清理掉过期的数据,减少查询时候的结果集

原文地址:https://www.cnblogs.com/brucexiang/p/9758938.html