周俊杰
1 year ago
5 changed files with 112 additions and 27 deletions
@ -0,0 +1,45 @@ |
|||
package org.nl.system.service.quartz.task; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.lucene.document.LongPoint; |
|||
import org.apache.lucene.index.IndexWriter; |
|||
import org.apache.lucene.search.Query; |
|||
import org.nl.config.lucene.LuceneAppender; |
|||
import org.nl.system.service.param.ISysParamService; |
|||
import org.nl.system.service.param.dao.Param; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.io.IOException; |
|||
import java.text.ParseException; |
|||
import java.text.SimpleDateFormat; |
|||
import java.time.LocalDateTime; |
|||
import java.time.temporal.ChronoUnit; |
|||
import java.util.Date; |
|||
|
|||
/** |
|||
* @Author: lyd |
|||
* @Description: 自动删除lucene日志 |
|||
* @Date: 2023/10/26 |
|||
*/ |
|||
@Slf4j |
|||
@Component |
|||
public class AutoClearLuceneData { |
|||
@Autowired |
|||
private ISysParamService paramService; |
|||
public void run() throws ParseException, IOException { |
|||
Param log_day = paramService.findByCode("log_day"); |
|||
IndexWriter writer = LuceneAppender.indexWriter; |
|||
// 获取当前时间
|
|||
LocalDateTime now = LocalDateTime.now(); |
|||
// 减去七天
|
|||
LocalDateTime sevenDaysAgo = now.minus(Long.parseLong(log_day.getValue()), ChronoUnit.DAYS); |
|||
// 获取时间戳
|
|||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); |
|||
Date date = dateFormat.parse(String.valueOf(sevenDaysAgo)); |
|||
long unixTimestamp = date.getTime(); // 获取Unix时间戳
|
|||
Query query = LongPoint.newRangeQuery("time", 0L, unixTimestamp); |
|||
writer.deleteDocuments(query); |
|||
writer.commit(); |
|||
} |
|||
} |
Loading…
Reference in new issue