Browse Source

ES数量统计

459242451@qq.com 3 năm trước cách đây
mục cha
commit
15256a2b7f

+ 33 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/gas/GasCommonController.java

@@ -1,17 +1,27 @@
 package com.ruoyi.web.controller.gas;
 
+import cn.hutool.core.convert.Convert;
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
+import com.ruoyi.common.constant.ElasticConstants;
 import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.common.core.domain.BaseEntity;
+import com.ruoyi.framework.config.ElasticSearchClient;
 import com.ruoyi.system.domain.SyncPost;
 import com.ruoyi.web.service.IGasCommonService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * @Description: 公共提出接口
  * @Author: huangcheng
@@ -25,6 +35,8 @@ public class GasCommonController {
 
     @Autowired
     private IGasCommonService gasCommonService;
+    @Autowired
+    private ElasticSearchClient client;
 
     @PreAuthorize("@ss.hasPermi('system:gas:sync')")
     @PostMapping("/sync")
@@ -34,4 +46,25 @@ public class GasCommonController {
         return AjaxResult.success("同步中...");
     }
 
+    @GetMapping("/illegalShipStatic")
+    @ApiOperation("获取硫、黑烟超标数量统计")
+    public AjaxResult illegalShipStatic() {
+//        Map<String, Object> params = baseEntity.getParams();
+//        if (ObjectUtil.isNotEmpty(params.get("beginTime"))) {
+//            params.put("beginTime", params.get("beginTime") + " 00:00:00");
+//        }
+//        if (ObjectUtil.isNotEmpty(params.get("endTime"))) {
+//            params.put("endTime", params.get("endTime") + " 23:59:59");
+//        }
+//        Map<String, Object> so2RangeCondition = new HashMap<>();
+//        so2RangeCondition.put("createTime", StrUtil.concat(true, "[", Convert.toStr(params.get("beginTime")), ",", Convert.toStr(params.get("endTime")), "]"));
+//        so2RangeCondition.put("peakTime", StrUtil.concat(true, "[", Convert.toStr(params.get("beginTime")), ",", Convert.toStr(params.get("endTime")), "]"));
+        long so2Count = client.count(null, null, ElasticConstants.SO2_ALERT, ElasticConstants.AIS_ILLEGAL_SHIP);
+        long blackCount = client.count(null, null, ElasticConstants.HEIYAN_SHIP_RECOGNITION);
+        Map<String, Object> result = new HashMap<>();
+        result.put("so2Count", so2Count);
+        result.put("blackCount", blackCount);
+        return AjaxResult.success(result);
+    }
+
 }

+ 60 - 14
ruoyi-framework/src/main/java/com/ruoyi/framework/config/ElasticSearchClient.java

@@ -27,6 +27,8 @@ import org.elasticsearch.action.update.UpdateResponse;
 import org.elasticsearch.client.RequestOptions;
 import org.elasticsearch.client.RestClient;
 import org.elasticsearch.client.RestHighLevelClient;
+import org.elasticsearch.client.core.CountRequest;
+import org.elasticsearch.client.core.CountResponse;
 import org.elasticsearch.client.indices.CreateIndexRequest;
 import org.elasticsearch.client.indices.CreateIndexResponse;
 import org.elasticsearch.client.indices.GetIndexRequest;
@@ -35,14 +37,7 @@ import org.elasticsearch.common.unit.DistanceUnit;
 import org.elasticsearch.common.xcontent.XContentBuilder;
 import org.elasticsearch.common.xcontent.XContentFactory;
 import org.elasticsearch.core.TimeValue;
-import org.elasticsearch.index.query.BoolQueryBuilder;
-import org.elasticsearch.index.query.GeoDistanceQueryBuilder;
-import org.elasticsearch.index.query.MatchQueryBuilder;
-import org.elasticsearch.index.query.PrefixQueryBuilder;
-import org.elasticsearch.index.query.QueryBuilders;
-import org.elasticsearch.index.query.RangeQueryBuilder;
-import org.elasticsearch.index.query.TermQueryBuilder;
-import org.elasticsearch.index.query.WildcardQueryBuilder;
+import org.elasticsearch.index.query.*;
 import org.elasticsearch.search.SearchHit;
 import org.elasticsearch.search.SearchHits;
 import org.elasticsearch.search.aggregations.AggregationBuilder;
@@ -73,6 +68,7 @@ import org.springframework.util.CollectionUtils;
 import javax.annotation.PostConstruct;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
@@ -427,7 +423,7 @@ public class ElasticSearchClient {
         }
 
         //执行查询
-        SearchResponse response = executeSearch(indexName, sourceBuilder);
+        SearchResponse response = executeSearch(sourceBuilder, indexName);
         SearchHits searchHits = response.getHits();
         SearchHit[] hits = searchHits.getHits();
         totalNum = searchHits.getTotalHits().value;
@@ -501,7 +497,7 @@ public class ElasticSearchClient {
         sourceBuilder.size(0);
 
         // 执行查询
-        SearchResponse response = executeSearch(indexName, sourceBuilder);
+        SearchResponse response = executeSearch(sourceBuilder, indexName);
         // 解析结果
         Aggregations aggregations = response.getAggregations();
         Terms terms = aggregations.get(by);
@@ -534,6 +530,7 @@ public class ElasticSearchClient {
 
     /**
      * 日期直方图聚合
+     *
      * @param indexName
      * @param aggsType
      * @param bucketName
@@ -590,7 +587,7 @@ public class ElasticSearchClient {
         sourceBuilder.size(0);
 
         // 执行查询
-        SearchResponse response = executeSearch(indexName, sourceBuilder);
+        SearchResponse response = executeSearch(sourceBuilder, indexName);
         // 解析结果
         Aggregations aggregations = response.getAggregations();
         ParsedDateHistogram agg = aggregations.get(by);
@@ -622,6 +619,38 @@ public class ElasticSearchClient {
     }
 
     /**
+     * 查询数量(跨索引)
+     * @param equalsCondition
+     * @param rangeCondition
+     * @param indexName
+     * @return
+     */
+    public long count(Map<String, Object> equalsCondition,
+                      Map<String, Object> rangeCondition,
+                      String... indexName) {
+        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
+        // 某一field=具体的值; 也可以某一field 的值 in 具体定义集合里的值
+        if (null != equalsCondition && !equalsCondition.isEmpty()) {
+            for (Map.Entry<String, Object> entry : equalsCondition.entrySet()) {
+                String key = entry.getKey();
+                //由于我创建索引的时候使用字符串不分词使用的.keyword类型
+                if (key.endsWith("_s")) {
+                    queryValueBuild(boolQueryBuilder, key + ".keyword", entry.getValue());
+                } else {
+                    queryValueBuild(boolQueryBuilder, key, entry.getValue());
+                }
+            }
+        }
+        //范围查询
+        if (null != rangeCondition && !rangeCondition.isEmpty()) {
+            rangeValueBuild(boolQueryBuilder, rangeCondition);
+        }
+        // 执行查询
+        CountResponse response = executeCountSearch(boolQueryBuilder, indexName);
+        return response.getCount();
+    }
+
+    /**
      * @param boolQueryBuilder
      * @param key
      * @param value
@@ -751,15 +780,15 @@ public class ElasticSearchClient {
     }
 
     /**
-     * @param indexName     对应的es的index名
      * @param sourceBuilder
+     * @param indexName     对应的es的index名
      * @return
      * @Description 执行查询
      */
-    private static SearchResponse executeSearch(String indexName, SearchSourceBuilder sourceBuilder) {
+    private static SearchResponse executeSearch(SearchSourceBuilder sourceBuilder, String... indexName) {
         // 获取不同系统的换行符
         String lineSeparator = System.lineSeparator();
-        log.info(lineSeparator + "ES查询:index:" + indexName + lineSeparator + "search:" + sourceBuilder.toString() + lineSeparator);
+        log.info(lineSeparator + "ES查询:index:" + Arrays.toString(indexName) + lineSeparator + "search:" + sourceBuilder.toString() + lineSeparator);
         SearchRequest searchRequest = new SearchRequest(indexName);
         SearchResponse response = null;
         searchRequest.source(sourceBuilder);
@@ -773,6 +802,23 @@ public class ElasticSearchClient {
         return response;
     }
 
+    private static CountResponse executeCountSearch(QueryBuilder sourceBuilder, String... indexName) {
+        // 获取不同系统的换行符
+        String lineSeparator = System.lineSeparator();
+        log.info(lineSeparator + "ES查询:index:" + Arrays.toString(indexName) + lineSeparator + "search:" + sourceBuilder.toString() + lineSeparator);
+        CountRequest countRequest = new CountRequest(indexName);
+        CountResponse response = null;
+        countRequest.query(sourceBuilder);
+        try {
+            response = client.count(countRequest, RequestOptions.DEFAULT);
+            log.info("search status:{}, totalNum:{}", response.status(), response.getCount());
+        } catch (IOException e) {
+            //异常处理,实际业务中是需要根据需求具体处理,自定义异常捕获
+            log.error(e.getMessage());
+        }
+        return response;
+    }
+
     /**
      * 索引创建前,判断索引是否存在
      */