diff --git a/stwzhj-modules/wzhj-location/src/main/java/org/dromara/location/config/ElasticsearchConfig.java b/stwzhj-modules/wzhj-location/src/main/java/org/dromara/location/config/ElasticsearchConfig.java index 5012741c..053d6e17 100644 --- a/stwzhj-modules/wzhj-location/src/main/java/org/dromara/location/config/ElasticsearchConfig.java +++ b/stwzhj-modules/wzhj-location/src/main/java/org/dromara/location/config/ElasticsearchConfig.java @@ -3,6 +3,8 @@ package org.dromara.location.config; import lombok.Data; import lombok.extern.slf4j.Slf4j; 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.impl.client.BasicCredentialsProvider; import org.elasticsearch.client.RestClient; @@ -66,7 +68,7 @@ public class ElasticsearchConfig { RestClientBuilder builder = RestClient.builder(httpHost); // 设置用户名、密码 CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); -// credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password)); + credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password)); // 连接延时配置 builder.setRequestConfigCallback(requestConfigBuilder -> { requestConfigBuilder.setConnectTimeout(connectTimeOut); diff --git a/stwzhj-modules/wzhj-location/src/main/java/org/dromara/location/controller/ElasticSearchController.java b/stwzhj-modules/wzhj-location/src/main/java/org/dromara/location/controller/ElasticSearchController.java index 1fe047e1..4a425fd4 100644 --- a/stwzhj-modules/wzhj-location/src/main/java/org/dromara/location/controller/ElasticSearchController.java +++ b/stwzhj-modules/wzhj-location/src/main/java/org/dromara/location/controller/ElasticSearchController.java @@ -30,12 +30,7 @@ public class ElasticSearchController extends BaseController { String startTime = params.get("startTime").toString(); String endTime = params.get("endTime").toString(); String deviceType = params.get("deviceType").toString(); - List gpsInfoEntities = null; - try { - gpsInfoEntities = searchService.searchCar(deviceCode, startTime, endTime,deviceType); - }catch (RuntimeException e){ - return R.fail(500,e.getMessage()); - } + List gpsInfoEntities = searchService.searchCar(deviceCode, startTime, endTime,deviceType); return R.ok(gpsInfoEntities); } diff --git a/stwzhj-modules/wzhj-location/src/main/java/org/dromara/location/service/impl/SearchServiceImpl.java b/stwzhj-modules/wzhj-location/src/main/java/org/dromara/location/service/impl/SearchServiceImpl.java index 4938a7d2..e06f945c 100644 --- a/stwzhj-modules/wzhj-location/src/main/java/org/dromara/location/service/impl/SearchServiceImpl.java +++ b/stwzhj-modules/wzhj-location/src/main/java/org/dromara/location/service/impl/SearchServiceImpl.java @@ -4,8 +4,8 @@ import cn.hutool.core.date.DateField; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUnit; import cn.hutool.core.date.DateUtil; -import com.alibaba.fastjson.JSON; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.apache.dubbo.config.annotation.DubboReference; import org.dromara.location.service.ISearchService; 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.sort.SortBuilders; 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.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -40,6 +44,7 @@ import java.util.function.Consumer; @RequiredArgsConstructor @Service +@Slf4j public class SearchServiceImpl implements ISearchService { @Autowired @@ -52,28 +57,71 @@ public class SearchServiceImpl implements ISearchService { @Override - public List searchCar(String deviceCode, String startTime, String endTime,String deviceType) throws RuntimeException{ + public List searchCar(String deviceCode, String startTime, String endTime,String deviceType){ List sourceList = new ArrayList(); List esIndexByTime = findEsIndexByTime(startTime, endTime); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); - DateTime startDate = DateUtil.parse(startTime, "yyyy-MM-dd HH:mm:ss"); - DateTime endDate = DateUtil.parse(endTime, "yyyy-MM-dd HH:mm:ss"); + log.info("查询使用的索引列表: {}", esIndexByTime); // 添加日志输出 - BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery(); - // 匹配第一个 - TermQueryBuilder termTerminalBuilder1 = QueryBuilders.termQuery("deviceCode", deviceCode); - // 匹配第二个 - TermQueryBuilder termTerminalBuilder2 = QueryBuilders.termQuery("deviceType", deviceType); - - boolBuilder.must(termTerminalBuilder1); - - boolBuilder.must(termTerminalBuilder2); - System.out.print(format.format(startDate)); - boolBuilder.must(QueryBuilders.rangeQuery("gpsTime") - .gte(format.format(startDate)) - .lte(format.format(endDate)) ); + BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery() + .must(QueryBuilders.termQuery("deviceCode", deviceCode)) + .must(QueryBuilders.termQuery("deviceType", deviceType)) + .must(QueryBuilders.rangeQuery("gpsTime") + .format("yyyy-MM-dd HH:mm:ss") + .gte(startTime) + .lte(endTime)); + log.info("查询条件: deviceCode={}, deviceType={}, start={}, end={}", + deviceCode, deviceType, startTime, endTime); + // 执行滚动查询 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(); sourceBuilder.query(boolBuilder).sort("gpsTime",SortOrder.ASC).size(5000); SearchRequest rq = new SearchRequest(); @@ -86,7 +134,9 @@ public class SearchServiceImpl implements ISearchService { // SearchResponse rp = null; try { SearchResponse rp = restHighLevelClient.search(rq,RequestOptions.DEFAULT); + log.info("查询轨迹结果={}", rp.getHits()); SearchHit[] searchHits = rp.getHits().getHits(); + log.info("查询轨迹结果searchHits={}",searchHits.length); for (SearchHit searchHit : searchHits) { Map sourceAsMap = searchHit.getSourceAsMap(); sourceList.add(sourceAsMap); @@ -94,6 +144,7 @@ public class SearchServiceImpl implements ISearchService { //遍历搜索命中的数据,直到没有数据 String scrollId = rp.getScrollId(); + while (searchHits != null && searchHits.length > 0) { SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId); scrollRequest.scroll(scroll); @@ -126,7 +177,7 @@ public class SearchServiceImpl implements ISearchService { e.printStackTrace(); } - return sourceList; + return sourceList;*/ } @Override