省厅位置汇聚ES查询修改优化

stwzhj
luyya 2025-08-25 09:47:46 +08:00
parent 769bcc6d9f
commit 87882579c7
5 changed files with 67 additions and 62 deletions

View File

@ -45,6 +45,7 @@ public class ElasticSearchController extends BaseController {
try { try {
gpsInfoEntities = searchService.searchCar(deviceCode, startTime, endTime,deviceType); gpsInfoEntities = searchService.searchCar(deviceCode, startTime, endTime,deviceType);
}catch (RuntimeException e){ }catch (RuntimeException e){
e.printStackTrace();
return R.fail(500,e.getMessage()); return R.fail(500,e.getMessage());
} }
return R.ok(gpsInfoEntities); return R.ok(gpsInfoEntities);
@ -55,7 +56,7 @@ public class ElasticSearchController extends BaseController {
* */ * */
@PostMapping("/spatial-query") @PostMapping("/spatial-query")
public R spatialQuery(@RequestBody SpatialQueryRequest request) { public R spatialQuery(@RequestBody SpatialQueryRequest request) {
String todayIndexName = "rs_gpsinfo"+ LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); String todayIndexName = request.getIndex();
try { try {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();

View File

@ -40,21 +40,36 @@ public class LocationController {
* */ * */
@PostMapping("/getAllLocation") @PostMapping("/getAllLocation")
public R getAllLocaltion(@RequestBody Map<String,Object> params){ public R getAllLocaltion(@RequestBody Map<String,Object> params){
List<JSONObject> list = new ArrayList<>();
String keys = "online_users:"; String keys = "online_users:";
String key = null; // 在不同条件下赋值 最后根据此key取值 String key = ""; // 在不同条件下赋值 最后根据此key取值
if(CollectionUtils.isEmpty(params)){ if(CollectionUtils.isEmpty(params)){
key = keys +"*"; key = keys +"*";
list = RedisUtils.searchAndGetKeysValues(key);
} }
List<JSONObject> list = new ArrayList<>();
if (null != params.get("type")){ if (null != params.get("type")){
String type = params.get("type").toString(); String type = params.get("type").toString();
key = keys + "*:[" +type+"]:*"; String[] types = type.split(",");
if (null != params.get("deptId")){ if (null != params.get("deptId")){
key = keys + params.get("deptId").toString() + "*:["+type+"]:*"; // key值为 online_users:2022-04-20:3401*:[01,02]:* key = keys + params.get("deptId").toString() + "*:"; // key值为 online_users:2022-04-20:3401*:[01,02]:*
for (String s : types) {
String tkey = key + s+":*";
logger.info("keys={}",tkey);
List<JSONObject> tlist = RedisUtils.searchAndGetKeysValues(tkey);
list.addAll(tlist);
}
}else {
for (String s : types) {
String tkey = keys + "*:" + s+":*";
logger.info("keys={}",tkey);
List<JSONObject> tlist = RedisUtils.searchAndGetKeysValues(tkey);
list.addAll(tlist);
}
} }
} }
list = RedisUtils.searchAndGetKeysValues(key);
list.removeAll(Collections.singleton(null)); list.removeAll(Collections.singleton(null));
return R.ok(list); return R.ok(list);
} }
@ -74,7 +89,7 @@ public class LocationController {
String[] zzjgdm = Convert.toStrArray(zzjgdms); String[] zzjgdm = Convert.toStrArray(zzjgdms);
for (String s : zzjgdm) { for (String s : zzjgdm) {
s = deptIdSub(s); s = deptIdSub(s);
String key = keys + s + ":["+type+"]:*"; // key值为 online_users:3401xxxx:[01,02]:* String key = keys + s + ":"+type+":*"; // key值为 online_users:3401xxxx:[01,02]:*
List<JSONObject> list = RedisUtils.searchAndGetKeysValues(key); List<JSONObject> list = RedisUtils.searchAndGetKeysValues(key);
list.removeAll(Collections.singleton(null)); list.removeAll(Collections.singleton(null));
dlist.addAll(list); dlist.addAll(list);

View File

@ -9,6 +9,8 @@ public class SpatialQueryRequest {
private QueryType queryType; private QueryType queryType;
private String index;
// 点查询参数 // 点查询参数
private Point center; private Point center;
private Double radius; private Double radius;

View File

@ -75,7 +75,7 @@ public class CorrectGeoQueryService {
// 创建ES形状 // 创建ES形状
ShapeBuilder shapeBuilder = convertGeometryToEsShape(buffer); ShapeBuilder shapeBuilder = convertGeometryToEsShape(buffer);
return QueryBuilders.geoShapeQuery("location_shape", shapeBuilder) return QueryBuilders.geoShapeQuery("location", shapeBuilder)
.relation(ShapeRelation.INTERSECTS); .relation(ShapeRelation.INTERSECTS);
} }

View File

@ -54,62 +54,51 @@ public class SearchServiceImpl implements ISearchService {
public List<Map> searchCar(String deviceCode, String startTime, String endTime,String deviceType) throws RuntimeException{ public List<Map> searchCar(String deviceCode, String startTime, String endTime,String deviceType) throws RuntimeException{
List<Map> sourceList = new ArrayList<Map>(); List<Map> sourceList = new ArrayList<Map>();
List<String> esIndexByTime = findEsIndexByTime(startTime, endTime); List<String> esIndexByTime = findEsIndexByTime(startTime, endTime);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); log.info("查询使用的索引列表: {}", esIndexByTime); // 添加日志输出
DateTime startDate = DateUtil.parse(startTime, "yyyy-MM-dd HH:mm:ss");
DateTime endDate = DateUtil.parse(endTime, "yyyy-MM-dd HH:mm:ss");
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery(); BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery()
// 匹配第一个 .must(QueryBuilders.termQuery("deviceCode", deviceCode))
TermQueryBuilder termTerminalBuilder1 = QueryBuilders.termQuery("deviceCode", deviceCode); .must(QueryBuilders.termQuery("deviceType", deviceType))
// 匹配第二个 .must(QueryBuilders.rangeQuery("gpsTime")
TermQueryBuilder termTerminalBuilder2 = QueryBuilders.termQuery("deviceType", deviceType); .format("yyyy-MM-dd HH:mm:ss")
.gte(startTime)
boolBuilder.must(termTerminalBuilder1); .lte(endTime));
log.info("查询条件: deviceCode={}, deviceType={}, start={}, end={}",
boolBuilder.must(termTerminalBuilder2); deviceCode, deviceType, startTime, endTime);
System.out.print(format.format(startDate));
boolBuilder.must(QueryBuilders.rangeQuery("gpsTime")
.gte(format.format(startDate))
.lte(format.format(endDate)) );
// 执行滚动查询
Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L)); Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L));
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
sourceBuilder.query(boolBuilder).sort("gpsTime",SortOrder.ASC).size(5000); .query(boolBuilder)
SearchRequest rq = new SearchRequest(); .sort("gpsTime", SortOrder.ASC)
// boolBuilder.withIndicesOptions(IndicesOptions.lenientExpandOpen()); .size(5000);
rq.scroll(scroll);
rq.source(sourceBuilder); SearchRequest rq = new SearchRequest()
rq.indices(esIndexByTime.toArray(new String[0])); .indices(esIndexByTime.toArray(new String[0]))
// 忽略因索引不存在导致的错误 .source(sourceBuilder)
rq.indicesOptions(IndicesOptions.lenientExpandOpen()); .scroll(scroll)
// SearchResponse rp = null; .indicesOptions(IndicesOptions.lenientExpandOpen());
// 添加到构建查询后
String dsl = sourceBuilder.toString();
log.info("最终DSL查询语句:\n{}", dsl);
try { try {
SearchResponse rp = restHighLevelClient.search(rq, RequestOptions.DEFAULT); SearchResponse rp = restHighLevelClient.search(rq, RequestOptions.DEFAULT);
SearchHit[] searchHits = rp.getHits().getHits(); log.info("ES返回总命中数: {}", rp.getHits().getTotalHits()); // 关键日志
for (SearchHit searchHit : searchHits) {
Map sourceAsMap = searchHit.getSourceAsMap(); String scrollId = rp.getScrollId();
sourceList.add(sourceAsMap); SearchHit[] hits = rp.getHits().getHits();
while (hits != null && hits.length > 0) {
for (SearchHit hit : hits) {
sourceList.add(hit.getSourceAsMap());
}
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId)
.scroll(scroll);
rp = restHighLevelClient.scroll(scrollRequest, RequestOptions.DEFAULT);
hits = rp.getHits().getHits();
scrollId = rp.getScrollId(); // 更新scrollId
} }
//遍历搜索命中的数据,直到没有数据 // 清理滚动
String scrollId = rp.getScrollId();
while (searchHits != null && searchHits.length > 0) {
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
scrollRequest.scroll(scroll);
try {
rp = restHighLevelClient.scroll(scrollRequest,RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
scrollId = rp.getScrollId();
searchHits = rp.getHits().getHits();
if (searchHits != null && searchHits.length > 0) {
for (SearchHit searchHit : searchHits) {
Map sourceAsMap = searchHit.getSourceAsMap();
sourceList.add(sourceAsMap);
}
}
}
//清除滚屏 //清除滚屏
ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
clearScrollRequest.addScrollId(scrollId);//也可以选择setScrollIds()将多个scrollId一起使用 clearScrollRequest.addScrollId(scrollId);//也可以选择setScrollIds()将多个scrollId一起使用
@ -120,11 +109,9 @@ public class SearchServiceImpl implements ISearchService {
e.printStackTrace(); e.printStackTrace();
} }
boolean succeeded = clearScrollResponse.isSucceeded(); boolean succeeded = clearScrollResponse.isSucceeded();
} catch (IOException e) {
} catch (Exception e) { log.error("ES查询异常", e);
e.printStackTrace();
} }
return sourceList; return sourceList;
} }