package com.tistar.wais.admin.controller;
import com.alibaba.fastjson.JSONObject;
import com.tistar.wais.common.dataEnum.DataEnum;
import com.tistar.wais.common.result.ResponseBean;
import com.tistar.wais.common.util.JsonPoiTool;
import net.sf.json.JSONArray;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @Author tengpt
* @Date 2019/11/29 16:01
*/
@RestController
@RequestMapping("/elastic")
public class ElasticSearchController {
@Autowired
private RestHighLevelClient restHighLevelClient;
@GetMapping("getJavaInfo")
@ResponseBody
public ResponseBean getJavaInfo(@RequestParam(required = false, defaultValue = "0") int from,
@RequestParam(required = false, defaultValue = "10") int size,
@RequestParam String startTime,
@RequestParam String endTime) {
try {
// 指定索引
SearchRequest searchRequest = new SearchRequest("safeweb-log-*");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.boolQuery()
// 查询条件,按照时间查询错误日志
.must(QueryBuilders.matchQuery("log_level", "ERROR"))
.must(QueryBuilders.rangeQuery("timestamp")
.timeZone("GMT+8")
.gte(startTime)
.lte(endTime))
);
//分页
from = from <= -1 ? 0 : from;
size = size >= 1000 ? 1000 : size;
size = size <= 0 ? 10 : size;
searchSourceBuilder.from(from);
searchSourceBuilder.size(size);
//超时
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
//searchSourceBuilder.sort(new FieldSortBuilder("timestamp").order(SortOrder.DESC));
searchRequest.source(searchSourceBuilder);
// Setting IndicesOptions controls how unavailable indices are resolved and how wildcard expressions are expanded
searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//获取source
// return Arrays.stream(searchResponse.getHits().getHits()).map(b -> b.getSourceAsMap()).collect(Collectors.toList());
return new ResponseBean(DataEnum.SUCCESS, searchResponse);
} catch (IOException e) {
return new ResponseBean(DataEnum.FAIL, e.getMessage());
}
}
}
Elasticsearch 7.3 按照时间分页查询
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- “和父母的交流越来越少,害怕他们识破我脆弱的坚强……” 视频的主角小雯是一个远离家乡,在城市里独自打拼的职业女性。...