diff --git a/casic-common/pom.xml b/casic-common/pom.xml
index 27e6a55..a47c616 100644
--- a/casic-common/pom.xml
+++ b/casic-common/pom.xml
@@ -46,6 +46,11 @@
mina-core
2.0.4
+
+ com.casic
+ casic-data-es
+ 1.0.1.alpha
+
diff --git a/casic-common/pom.xml b/casic-common/pom.xml
index 27e6a55..a47c616 100644
--- a/casic-common/pom.xml
+++ b/casic-common/pom.xml
@@ -46,6 +46,11 @@
mina-core
2.0.4
+
+ com.casic
+ casic-data-es
+ 1.0.1.alpha
+
diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java
new file mode 100644
index 0000000..3b1f3fe
--- /dev/null
+++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java
@@ -0,0 +1,18 @@
+package com.casic.missiles.es;
+
+import lombok.Data;
+
+@Data
+public class DataGasEs {
+
+ private Long stationId;
+ private String stationName;
+ private Long monitorId;
+ private String monitorName;
+ private String devcode;
+ private Double direction;
+ private Double pitch;
+ private Double concentration;
+ private Double threshold;
+ private String logTime;
+}
diff --git a/casic-common/pom.xml b/casic-common/pom.xml
index 27e6a55..a47c616 100644
--- a/casic-common/pom.xml
+++ b/casic-common/pom.xml
@@ -46,6 +46,11 @@
mina-core
2.0.4
+
+ com.casic
+ casic-data-es
+ 1.0.1.alpha
+
diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java
new file mode 100644
index 0000000..3b1f3fe
--- /dev/null
+++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java
@@ -0,0 +1,18 @@
+package com.casic.missiles.es;
+
+import lombok.Data;
+
+@Data
+public class DataGasEs {
+
+ private Long stationId;
+ private String stationName;
+ private Long monitorId;
+ private String monitorName;
+ private String devcode;
+ private Double direction;
+ private Double pitch;
+ private Double concentration;
+ private Double threshold;
+ private String logTime;
+}
diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java
new file mode 100644
index 0000000..aeef63d
--- /dev/null
+++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java
@@ -0,0 +1,257 @@
+package com.casic.missiles.es;
+
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * ES查询请求体
+ *
+ * @author zhangyingjie123
+ * @since 2020-11-30
+ */
+public class ElasticSearchQuery {
+ private JSONObject body;
+ private JSONObject query;
+ private JSONObject bool;
+ private JSONArray boolMust;
+ private JSONObject filter;
+ private JSONObject filterBool;
+ private JSONArray filterBoolMust;
+ private JSONArray filterBoolMustnot;
+
+ public static final String MUST_FLAG = "1";
+ public static final String MUST_NOT_FLAG = "0";
+
+ public static final String QUERY = "query";
+ public static final String BOOL = "bool";
+ public static final String MUST = "must";
+ public static final String MUST_NOT = "must_not";
+ public static final String FILTER = "filter";
+
+
+ /**
+ * 生成查询请求体基础结构:
+ * {
+ * "query": {
+ * "bool": {
+ * "must" [],
+ * "filter": {
+ * "bool": {
+ * "must": [],
+ * "must_not": []
+ * }
+ * }
+ * }
+ * }
+ * }
+ */
+ public ElasticSearchQuery() {
+ this.body = new JSONObject();
+ this.query = this.query();
+ this.bool = this.bool();
+ this.boolMust = this.boolMust();
+ this.filter = this.filter();
+ this.filterBool = this.filterBool();
+ this.filterBoolMust = this.filterBoolMust();
+ this.filterBoolMustnot = this.filterBoolMustnot();
+ }
+
+ public ElasticSearchQuery(JSONObject body){
+ this.body = body;
+ }
+
+ public JSONObject getBody() {
+ return body;
+ }
+
+ private JSONObject query(){
+ if(ObjectUtil.isEmpty(this.body.get(QUERY))){
+ this.body.put("query",new JSONObject());
+ }
+ return this.body.getJSONObject(QUERY);
+
+ }
+
+ private JSONObject bool() {
+ JSONObject query = this.query;
+ if(ObjectUtil.isEmpty(query.get(BOOL))){
+ query.put(BOOL,new JSONObject());
+ }
+ return query.getJSONObject(BOOL);
+ }
+
+ private JSONArray boolMust(){
+ JSONObject bool = this.bool;
+ if(ObjectUtil.isEmpty(bool.get(MUST))){
+ bool.put(MUST,new JSONArray());
+ }
+ return bool.getJSONArray(MUST);
+ }
+
+ private JSONObject filter(){
+ JSONObject bool = this.bool;
+ if(ObjectUtil.isEmpty(bool.get(FILTER))){
+ bool.put(FILTER,new JSONObject());
+ }
+ return bool.getJSONObject(FILTER);
+ }
+
+ private JSONObject filterBool(){
+ JSONObject filter = this.filter;
+ if(ObjectUtil.isEmpty(filter.get(BOOL))){
+ filter.put(BOOL,new JSONObject());
+ }
+ return filter.getJSONObject(BOOL);
+ }
+
+ private JSONArray filterBoolMust(){
+ JSONObject bool = this.filterBool;
+ if(ObjectUtil.isEmpty(bool.get(MUST))){
+ bool.put(MUST,new JSONArray());
+ }
+ return bool.getJSONArray(MUST);
+ }
+
+ private JSONArray filterBoolMustnot(){
+ JSONObject bool = this.filterBool;
+ if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){
+ bool.put(MUST_NOT,new JSONArray());
+ }
+ return bool.getJSONArray(MUST_NOT);
+ }
+
+ public void match(String filed, Serializable value){
+ JSONArray boolMust = this.boolMust;
+ JSONObject match = new JSONObject();
+ JSONObject filedJson = new JSONObject();
+ filedJson.put(filed,value);
+ match.put("match",filedJson);
+ boolMust.add(match);
+ }
+
+ public void matchPhrase(String filed, Serializable value){
+ JSONArray boolMust = this.boolMust;
+ JSONObject matchPhrase = new JSONObject();
+ JSONObject filedJson = new JSONObject();
+ filedJson.put(filed,value);
+ matchPhrase.put("match_phrase",filedJson);
+ boolMust.add(matchPhrase);
+ }
+
+ public void term(String filed, Serializable value){
+ this.term(filed,value,MUST_FLAG);
+ }
+
+ public void term(String filed, Serializable value,String mustFlag){
+ JSONObject filedJson = new JSONObject();
+ filedJson.put(filed,value);
+ JSONObject term = new JSONObject();
+ term.put("term",filedJson);
+ if(MUST_FLAG.equals(mustFlag)){
+ this.filterBoolMust.add(term);
+ }
+ if(MUST_NOT_FLAG.equals(mustFlag)){
+ this.filterBoolMustnot.add(term);
+ }
+ }
+
+ public void terms(String filed,List value){
+ this.terms(filed,value,MUST_FLAG);
+ }
+
+ public void terms(String filed, List value, String mustFlag){
+ JSONObject filedJson = new JSONObject();
+ filedJson.put(filed,value);
+ JSONObject terms = new JSONObject();
+ terms.put("terms",filedJson);
+ if(MUST_FLAG.equals(mustFlag)){
+ this.filterBoolMust.add(terms);
+ }
+ if(MUST_NOT_FLAG.equals(mustFlag)){
+ this.filterBoolMustnot.add(terms);
+ }
+ }
+
+ public void range(String filed, Serializable from, Serializable to){
+ this.range(filed,from,to,MUST_FLAG);
+ }
+
+ public void range(String filed, Serializable from, Serializable to, String mustFlag){
+ JSONObject rangeJson = new JSONObject();
+ if(ObjectUtil.isNotEmpty(from)){
+ rangeJson.put("from",from);
+ }
+ if(ObjectUtil.isNotEmpty(to)){
+ rangeJson.put("to",to);
+ }
+ JSONObject filedJson = new JSONObject();
+ filedJson.put(filed,rangeJson);
+ JSONObject range = new JSONObject();
+ range.put("range",filedJson);
+ if(MUST_FLAG.equals(mustFlag)){
+ this.filterBoolMust.add(range);
+ }
+ if(MUST_NOT_FLAG.equals(mustFlag)){
+ this.filterBoolMustnot.add(range);
+ }
+ }
+
+ public void size(int size){
+ this.body.put("size",size);
+ }
+
+ public void from(int from) {
+ this.body.put("from",from);
+ }
+
+ public void sort(String sort, String order){
+ JSONObject sortJson = new JSONObject();
+ JSONObject orderJson = new JSONObject();
+ orderJson.put("order",order);
+ sortJson.put(sort,orderJson);
+ this.body.put("sort",sortJson);
+ }
+
+ public void aggs(String aggsName, String filed){
+ JSONObject terms = new JSONObject();
+ terms.put("field",filed);
+ JSONObject aggsTerms = new JSONObject();
+ aggsTerms.put("terms",terms);
+ JSONObject aggsBody = new JSONObject();
+ aggsBody.put(aggsName,aggsTerms);
+ this.body.put("aggs",aggsBody);
+ }
+
+ public void aggs(JSONObject aggBody){
+ this.body.put("aggs",aggBody);
+ }
+
+
+
+ public static void main(String[] args) {
+ ElasticSearchQuery query = new ElasticSearchQuery();
+ query.matchPhrase("name","张三");
+ query.term("sex","2");
+ List deptIds = new ArrayList<>();
+ deptIds.add(0L);
+ deptIds.add(24L);
+ query.terms("deptId", deptIds);
+ Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd");
+ Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd");
+ query.range("collTime",date1.getTime(),date2.getTime());
+ query.term("collState","3",MUST_NOT_FLAG);
+ query.size(10);
+ query.from(0);
+ query.sort("irId","asc");
+ System.out.println(query.getBody().toJSONString());
+ }
+
+
+}
diff --git a/casic-common/pom.xml b/casic-common/pom.xml
index 27e6a55..a47c616 100644
--- a/casic-common/pom.xml
+++ b/casic-common/pom.xml
@@ -46,6 +46,11 @@
mina-core
2.0.4
+
+ com.casic
+ casic-data-es
+ 1.0.1.alpha
+
diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java
new file mode 100644
index 0000000..3b1f3fe
--- /dev/null
+++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java
@@ -0,0 +1,18 @@
+package com.casic.missiles.es;
+
+import lombok.Data;
+
+@Data
+public class DataGasEs {
+
+ private Long stationId;
+ private String stationName;
+ private Long monitorId;
+ private String monitorName;
+ private String devcode;
+ private Double direction;
+ private Double pitch;
+ private Double concentration;
+ private Double threshold;
+ private String logTime;
+}
diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java
new file mode 100644
index 0000000..aeef63d
--- /dev/null
+++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java
@@ -0,0 +1,257 @@
+package com.casic.missiles.es;
+
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * ES查询请求体
+ *
+ * @author zhangyingjie123
+ * @since 2020-11-30
+ */
+public class ElasticSearchQuery {
+ private JSONObject body;
+ private JSONObject query;
+ private JSONObject bool;
+ private JSONArray boolMust;
+ private JSONObject filter;
+ private JSONObject filterBool;
+ private JSONArray filterBoolMust;
+ private JSONArray filterBoolMustnot;
+
+ public static final String MUST_FLAG = "1";
+ public static final String MUST_NOT_FLAG = "0";
+
+ public static final String QUERY = "query";
+ public static final String BOOL = "bool";
+ public static final String MUST = "must";
+ public static final String MUST_NOT = "must_not";
+ public static final String FILTER = "filter";
+
+
+ /**
+ * 生成查询请求体基础结构:
+ * {
+ * "query": {
+ * "bool": {
+ * "must" [],
+ * "filter": {
+ * "bool": {
+ * "must": [],
+ * "must_not": []
+ * }
+ * }
+ * }
+ * }
+ * }
+ */
+ public ElasticSearchQuery() {
+ this.body = new JSONObject();
+ this.query = this.query();
+ this.bool = this.bool();
+ this.boolMust = this.boolMust();
+ this.filter = this.filter();
+ this.filterBool = this.filterBool();
+ this.filterBoolMust = this.filterBoolMust();
+ this.filterBoolMustnot = this.filterBoolMustnot();
+ }
+
+ public ElasticSearchQuery(JSONObject body){
+ this.body = body;
+ }
+
+ public JSONObject getBody() {
+ return body;
+ }
+
+ private JSONObject query(){
+ if(ObjectUtil.isEmpty(this.body.get(QUERY))){
+ this.body.put("query",new JSONObject());
+ }
+ return this.body.getJSONObject(QUERY);
+
+ }
+
+ private JSONObject bool() {
+ JSONObject query = this.query;
+ if(ObjectUtil.isEmpty(query.get(BOOL))){
+ query.put(BOOL,new JSONObject());
+ }
+ return query.getJSONObject(BOOL);
+ }
+
+ private JSONArray boolMust(){
+ JSONObject bool = this.bool;
+ if(ObjectUtil.isEmpty(bool.get(MUST))){
+ bool.put(MUST,new JSONArray());
+ }
+ return bool.getJSONArray(MUST);
+ }
+
+ private JSONObject filter(){
+ JSONObject bool = this.bool;
+ if(ObjectUtil.isEmpty(bool.get(FILTER))){
+ bool.put(FILTER,new JSONObject());
+ }
+ return bool.getJSONObject(FILTER);
+ }
+
+ private JSONObject filterBool(){
+ JSONObject filter = this.filter;
+ if(ObjectUtil.isEmpty(filter.get(BOOL))){
+ filter.put(BOOL,new JSONObject());
+ }
+ return filter.getJSONObject(BOOL);
+ }
+
+ private JSONArray filterBoolMust(){
+ JSONObject bool = this.filterBool;
+ if(ObjectUtil.isEmpty(bool.get(MUST))){
+ bool.put(MUST,new JSONArray());
+ }
+ return bool.getJSONArray(MUST);
+ }
+
+ private JSONArray filterBoolMustnot(){
+ JSONObject bool = this.filterBool;
+ if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){
+ bool.put(MUST_NOT,new JSONArray());
+ }
+ return bool.getJSONArray(MUST_NOT);
+ }
+
+ public void match(String filed, Serializable value){
+ JSONArray boolMust = this.boolMust;
+ JSONObject match = new JSONObject();
+ JSONObject filedJson = new JSONObject();
+ filedJson.put(filed,value);
+ match.put("match",filedJson);
+ boolMust.add(match);
+ }
+
+ public void matchPhrase(String filed, Serializable value){
+ JSONArray boolMust = this.boolMust;
+ JSONObject matchPhrase = new JSONObject();
+ JSONObject filedJson = new JSONObject();
+ filedJson.put(filed,value);
+ matchPhrase.put("match_phrase",filedJson);
+ boolMust.add(matchPhrase);
+ }
+
+ public void term(String filed, Serializable value){
+ this.term(filed,value,MUST_FLAG);
+ }
+
+ public void term(String filed, Serializable value,String mustFlag){
+ JSONObject filedJson = new JSONObject();
+ filedJson.put(filed,value);
+ JSONObject term = new JSONObject();
+ term.put("term",filedJson);
+ if(MUST_FLAG.equals(mustFlag)){
+ this.filterBoolMust.add(term);
+ }
+ if(MUST_NOT_FLAG.equals(mustFlag)){
+ this.filterBoolMustnot.add(term);
+ }
+ }
+
+ public void terms(String filed,List value){
+ this.terms(filed,value,MUST_FLAG);
+ }
+
+ public void terms(String filed, List value, String mustFlag){
+ JSONObject filedJson = new JSONObject();
+ filedJson.put(filed,value);
+ JSONObject terms = new JSONObject();
+ terms.put("terms",filedJson);
+ if(MUST_FLAG.equals(mustFlag)){
+ this.filterBoolMust.add(terms);
+ }
+ if(MUST_NOT_FLAG.equals(mustFlag)){
+ this.filterBoolMustnot.add(terms);
+ }
+ }
+
+ public void range(String filed, Serializable from, Serializable to){
+ this.range(filed,from,to,MUST_FLAG);
+ }
+
+ public void range(String filed, Serializable from, Serializable to, String mustFlag){
+ JSONObject rangeJson = new JSONObject();
+ if(ObjectUtil.isNotEmpty(from)){
+ rangeJson.put("from",from);
+ }
+ if(ObjectUtil.isNotEmpty(to)){
+ rangeJson.put("to",to);
+ }
+ JSONObject filedJson = new JSONObject();
+ filedJson.put(filed,rangeJson);
+ JSONObject range = new JSONObject();
+ range.put("range",filedJson);
+ if(MUST_FLAG.equals(mustFlag)){
+ this.filterBoolMust.add(range);
+ }
+ if(MUST_NOT_FLAG.equals(mustFlag)){
+ this.filterBoolMustnot.add(range);
+ }
+ }
+
+ public void size(int size){
+ this.body.put("size",size);
+ }
+
+ public void from(int from) {
+ this.body.put("from",from);
+ }
+
+ public void sort(String sort, String order){
+ JSONObject sortJson = new JSONObject();
+ JSONObject orderJson = new JSONObject();
+ orderJson.put("order",order);
+ sortJson.put(sort,orderJson);
+ this.body.put("sort",sortJson);
+ }
+
+ public void aggs(String aggsName, String filed){
+ JSONObject terms = new JSONObject();
+ terms.put("field",filed);
+ JSONObject aggsTerms = new JSONObject();
+ aggsTerms.put("terms",terms);
+ JSONObject aggsBody = new JSONObject();
+ aggsBody.put(aggsName,aggsTerms);
+ this.body.put("aggs",aggsBody);
+ }
+
+ public void aggs(JSONObject aggBody){
+ this.body.put("aggs",aggBody);
+ }
+
+
+
+ public static void main(String[] args) {
+ ElasticSearchQuery query = new ElasticSearchQuery();
+ query.matchPhrase("name","张三");
+ query.term("sex","2");
+ List deptIds = new ArrayList<>();
+ deptIds.add(0L);
+ deptIds.add(24L);
+ query.terms("deptId", deptIds);
+ Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd");
+ Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd");
+ query.range("collTime",date1.getTime(),date2.getTime());
+ query.term("collState","3",MUST_NOT_FLAG);
+ query.size(10);
+ query.from(0);
+ query.sort("irId","asc");
+ System.out.println(query.getBody().toJSONString());
+ }
+
+
+}
diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java
new file mode 100644
index 0000000..dc9a968
--- /dev/null
+++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java
@@ -0,0 +1,172 @@
+package com.casic.missiles.es;
+
+import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.casic.missiles.core.es.EsResponse;
+import org.apache.http.nio.entity.NStringEntity;
+import org.apache.http.util.EntityUtils;
+import org.elasticsearch.client.Request;
+import org.elasticsearch.client.RequestOptions;
+import org.elasticsearch.client.Response;
+import org.elasticsearch.client.RestClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.env.Environment;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.List;
+
+/**
+ * ES工具类
+ *
+ * @author zhangyingjie123
+ * @since 2020-11-30
+ */
+@Component
+public class ElasticSearchUtil {
+ private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class);
+
+ @Autowired
+ private RestClient clientMapping;
+ @Autowired
+ private Environment env;
+
+ private static String username;
+ private static String password;
+
+ private static RestClient client;
+
+ @PostConstruct
+ public void init(){
+ client = clientMapping;
+ username = env.getProperty("casic.data.es.username");
+ password = env.getProperty("casic.data.es.password");
+ }
+
+ public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{
+ String method = "GET";
+ String endPoint = index.concat("/").concat(type).concat("/").concat(id);
+ Response response = performRequest(method,endPoint,null);
+ JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
+ return result.getJSONObject("_source");
+ }
+
+ public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{
+ String method = "POST";
+ String endPoint = index.concat("/").concat(type).concat("/");
+ if(ObjectUtil.isNotEmpty(id)){
+ endPoint.concat(id);
+ }
+ endPoint.concat("?refresh=true");
+ logger.debug(entity.toString());
+ return performRequest(method,endPoint,JSON.toJSONString(entity));
+ }
+
+ public static Response deleteDocumentById(String index, String type, String id) throws IOException{
+ String method = "DELETE";
+ String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true");
+ return performRequest(method,endPoint,null);
+ }
+
+ public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{
+ String method = "POST";
+ String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true");
+ logger.debug(entity.toString());
+ return performRequest(method,endPoint,JSON.toJSONString(entity));
+ }
+
+ public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{
+ String method = "POST";
+ String entPoint = index.concat("/").concat(type).concat("/").concat("_search");
+ logger.debug(query.getBody().toString());
+ Response response = performRequest(method, entPoint, query.getBody().toString());
+ // 获取response body,转换为json对象
+ JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
+ return result.toJavaObject(EsResponse.class);
+ }
+
+ public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{
+ String method = "POST";
+ String entPoint = index.concat("/").concat(type).concat("/").concat("_search");
+ logger.debug(query.getBody().toString());
+ Response response = performRequest(method, entPoint, query.getBody().toString());
+ return response;
+ }
+
+ public static List