处理宿州历史轨迹查询不到数据问题
parent
b887742212
commit
688cb8383c
|
|
@ -3,6 +3,8 @@ package org.dromara.location.config;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.http.HttpHost;
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.auth.AuthScope;
|
||||||
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
import org.apache.http.client.CredentialsProvider;
|
import org.apache.http.client.CredentialsProvider;
|
||||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||||
import org.elasticsearch.client.RestClient;
|
import org.elasticsearch.client.RestClient;
|
||||||
|
|
@ -66,7 +68,7 @@ public class ElasticsearchConfig {
|
||||||
RestClientBuilder builder = RestClient.builder(httpHost);
|
RestClientBuilder builder = RestClient.builder(httpHost);
|
||||||
// 设置用户名、密码
|
// 设置用户名、密码
|
||||||
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||||
// credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
|
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
|
||||||
// 连接延时配置
|
// 连接延时配置
|
||||||
builder.setRequestConfigCallback(requestConfigBuilder -> {
|
builder.setRequestConfigCallback(requestConfigBuilder -> {
|
||||||
requestConfigBuilder.setConnectTimeout(connectTimeOut);
|
requestConfigBuilder.setConnectTimeout(connectTimeOut);
|
||||||
|
|
|
||||||
|
|
@ -30,12 +30,7 @@ public class ElasticSearchController extends BaseController {
|
||||||
String startTime = params.get("startTime").toString();
|
String startTime = params.get("startTime").toString();
|
||||||
String endTime = params.get("endTime").toString();
|
String endTime = params.get("endTime").toString();
|
||||||
String deviceType = params.get("deviceType").toString();
|
String deviceType = params.get("deviceType").toString();
|
||||||
List<Map> gpsInfoEntities = null;
|
List<Map> gpsInfoEntities = searchService.searchCar(deviceCode, startTime, endTime,deviceType);
|
||||||
try {
|
|
||||||
gpsInfoEntities = searchService.searchCar(deviceCode, startTime, endTime,deviceType);
|
|
||||||
}catch (RuntimeException e){
|
|
||||||
return R.fail(500,e.getMessage());
|
|
||||||
}
|
|
||||||
return R.ok(gpsInfoEntities);
|
return R.ok(gpsInfoEntities);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@ import cn.hutool.core.date.DateField;
|
||||||
import cn.hutool.core.date.DateTime;
|
import cn.hutool.core.date.DateTime;
|
||||||
import cn.hutool.core.date.DateUnit;
|
import cn.hutool.core.date.DateUnit;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import com.alibaba.fastjson.JSON;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.dubbo.config.annotation.DubboReference;
|
import org.apache.dubbo.config.annotation.DubboReference;
|
||||||
import org.dromara.location.service.ISearchService;
|
import org.dromara.location.service.ISearchService;
|
||||||
import org.dromara.system.api.RemoteDictService;
|
import org.dromara.system.api.RemoteDictService;
|
||||||
|
|
@ -25,6 +25,10 @@ import org.elasticsearch.search.SearchHits;
|
||||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||||
import org.elasticsearch.search.sort.SortBuilders;
|
import org.elasticsearch.search.sort.SortBuilders;
|
||||||
import org.elasticsearch.search.sort.SortOrder;
|
import org.elasticsearch.search.sort.SortOrder;
|
||||||
|
import org.joda.time.DateTimeZone;
|
||||||
|
import org.joda.time.format.DateTimeFormat;
|
||||||
|
import org.joda.time.format.DateTimeFormatter;
|
||||||
|
import org.joda.time.format.ISODateTimeFormat;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
|
|
@ -40,6 +44,7 @@ import java.util.function.Consumer;
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Service
|
@Service
|
||||||
|
@Slf4j
|
||||||
public class SearchServiceImpl implements ISearchService {
|
public class SearchServiceImpl implements ISearchService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
|
@ -52,28 +57,71 @@ public class SearchServiceImpl implements ISearchService {
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
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){
|
||||||
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()
|
||||||
|
.query(boolBuilder)
|
||||||
|
.sort("gpsTime", SortOrder.ASC)
|
||||||
|
.size(5000);
|
||||||
|
|
||||||
|
SearchRequest rq = new SearchRequest()
|
||||||
|
.indices(esIndexByTime.toArray(new String[0]))
|
||||||
|
.source(sourceBuilder)
|
||||||
|
.scroll(scroll)
|
||||||
|
.indicesOptions(IndicesOptions.lenientExpandOpen());
|
||||||
|
// 添加到构建查询后
|
||||||
|
String dsl = sourceBuilder.toString();
|
||||||
|
log.info("最终DSL查询语句:\n{}", dsl);
|
||||||
|
try {
|
||||||
|
SearchResponse rp = restHighLevelClient.search(rq, RequestOptions.DEFAULT);
|
||||||
|
log.info("ES返回总命中数: {}", rp.getHits().getTotalHits()); // 关键日志
|
||||||
|
|
||||||
|
String scrollId = rp.getScrollId();
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清理滚动
|
||||||
|
//清除滚屏
|
||||||
|
ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
|
||||||
|
clearScrollRequest.addScrollId(scrollId);//也可以选择setScrollIds()将多个scrollId一起使用
|
||||||
|
ClearScrollResponse clearScrollResponse = null;
|
||||||
|
try {
|
||||||
|
clearScrollResponse = restHighLevelClient.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
boolean succeeded = clearScrollResponse.isSucceeded();
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("ES查询异常", e);
|
||||||
|
}
|
||||||
|
return sourceList;
|
||||||
|
|
||||||
|
|
||||||
|
/* Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L));
|
||||||
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
||||||
sourceBuilder.query(boolBuilder).sort("gpsTime",SortOrder.ASC).size(5000);
|
sourceBuilder.query(boolBuilder).sort("gpsTime",SortOrder.ASC).size(5000);
|
||||||
SearchRequest rq = new SearchRequest();
|
SearchRequest rq = new SearchRequest();
|
||||||
|
|
@ -86,7 +134,9 @@ public class SearchServiceImpl implements ISearchService {
|
||||||
// SearchResponse rp = null;
|
// SearchResponse rp = null;
|
||||||
try {
|
try {
|
||||||
SearchResponse rp = restHighLevelClient.search(rq,RequestOptions.DEFAULT);
|
SearchResponse rp = restHighLevelClient.search(rq,RequestOptions.DEFAULT);
|
||||||
|
log.info("查询轨迹结果={}", rp.getHits());
|
||||||
SearchHit[] searchHits = rp.getHits().getHits();
|
SearchHit[] searchHits = rp.getHits().getHits();
|
||||||
|
log.info("查询轨迹结果searchHits={}",searchHits.length);
|
||||||
for (SearchHit searchHit : searchHits) {
|
for (SearchHit searchHit : searchHits) {
|
||||||
Map sourceAsMap = searchHit.getSourceAsMap();
|
Map sourceAsMap = searchHit.getSourceAsMap();
|
||||||
sourceList.add(sourceAsMap);
|
sourceList.add(sourceAsMap);
|
||||||
|
|
@ -94,6 +144,7 @@ public class SearchServiceImpl implements ISearchService {
|
||||||
|
|
||||||
//遍历搜索命中的数据,直到没有数据
|
//遍历搜索命中的数据,直到没有数据
|
||||||
String scrollId = rp.getScrollId();
|
String scrollId = rp.getScrollId();
|
||||||
|
|
||||||
while (searchHits != null && searchHits.length > 0) {
|
while (searchHits != null && searchHits.length > 0) {
|
||||||
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
|
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
|
||||||
scrollRequest.scroll(scroll);
|
scrollRequest.scroll(scroll);
|
||||||
|
|
@ -126,7 +177,7 @@ public class SearchServiceImpl implements ISearchService {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return sourceList;
|
return sourceList;*/
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue