Elasticsearch导出两个时间段内.monitoring-logstash-7-*索引的数据 保存在text上

@ApiOperation(value = "导出详细日志接口", notes = "导出详细日志接口")
@PostMapping("/exportDetailLog")
public void exportDetailLog(@ApiParam(name = "导出详细日志接口输入参数实体", value = "导出详细日志接口输入参数实体",
required = false) @RequestBody HandleDetailLogVO handleDetailLogVO) throws IOException, ParseException {
SimpleDateFormat sd = new SimpleDateFormat(DateFormatEnum.YYYY_MM_DD_HH_MM_SS.getFormat());
String beginTime = handleDetailLogVO.getBeginTime() + DateFormatEnum.BEGIN_HH_MM_SS.getFormat();
String endTime = handleDetailLogVO.getEndTime() + DateFormatEnum.END_HH_MM_SS.getFormat();
String path = handleDetailLogVO.getPath() + "/" + handleDetailLogVO.getBeginTime() + "-"
+ handleDetailLogVO.getEndTime() + ".txt";
// 根据时间查询
NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
BoolQueryBuilder bool = QueryBuilders.boolQuery();
List<QueryBuilder> filters = bool.filter();
filters.add(QueryBuilders.rangeQuery("time").gte(sd.parse(beginTime)).lte(sd.parse(endTime)));
builder.withQuery(bool);
NativeSearchQuery query = builder.build();
IndexCoordinates indexCoordinates = IndexCoordinates.of(".monitoring-logstash-7-*");
// 查询前1w条
SearchScrollHits<DetailLogVO> scroll =
elasticsearchTemplate.searchScrollStart(3000, query, DetailLogVO.class, indexCoordinates);
List<SearchHit<DetailLogVO>> resultList = new ArrayList<>();
while (scroll.hasSearchHits()) {
List<SearchHit<DetailLogVO>> searchHitList = scroll.getSearchHits();
resultList.addAll(searchHitList);
scroll = elasticsearchTemplate.searchScrollContinue(scroll.getScrollId(), 3000, DetailLogVO.class,
indexCoordinates);
}
// 导出为text
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path, true), "utf-8"));
for (int i = 0; i < resultList.size(); i++) {
out.write(resultList.get(i).getContent().getMessage());
out.write("\r\n");
}
out.flush();
out.close();
// 删除索引
elasticsearchTemplate.delete(query, DetailLogVO.class, indexCoordinates);
}

实体

public class HandleDetailLogVO {

/**
* 结束时间
*/
@ApiModelProperty(value = "结束时间", name = "结束时间")
private String endTime;

/**
* 开始时间
*/
@ApiModelProperty(value = "开始时间", name = "开始时间")
private String beginTime;

/**
* 导出路径
*/
@ApiModelProperty(value = "导出路径", name = "导出路径")
private String path;
}