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 index 8995127..89cf0dd 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -67,11 +67,6 @@ this.filterBoolMustnot = this.filterBoolMustnot(); } - - public JSONObject getBool() { - return this.bool; - } - public ElasticSearchQuery(JSONObject body) { this.body = body; } @@ -103,14 +98,6 @@ return bool.getJSONArray(MUST); } - private JSONArray boolShould() { - JSONObject bool = this.bool(); - if (ObjectUtil.isEmpty(bool.get(SHOULD))) { - bool.put(SHOULD, new JSONArray()); - } - return bool.getJSONArray(SHOULD); - } - private JSONObject filter() { JSONObject bool = this.bool; if (ObjectUtil.isEmpty(bool.get(FILTER))) { @@ -149,6 +136,60 @@ this.boolShould.add(BoolMust); } + public void endShould() { + this.filterBoolMust.add(this.boolShould); + } + + private JSONObject boolBuild; + private JSONArray boolMustBuild; + + private JSONArray boolShould() { + boolBuild=new JSONObject(); + boolBuild.put(BOOL, new JSONObject()); + boolBuild.getJSONObject(BOOL).put(SHOULD, new JSONArray()); + return boolBuild.getJSONObject(BOOL).getJSONArray(SHOULD); + } + + public ElasticSearchQuery build() { + boolBuild=new JSONObject(); + boolBuild.put(BOOL, new JSONObject()); + boolBuild.getJSONObject(BOOL).put(MUST, new JSONArray()); + boolMustBuild = boolBuild.getJSONObject(BOOL).getJSONArray(MUST); + return this; + } + + public ElasticSearchQuery termBool(String filed, Serializable value) { + JSONObject filedJson = new JSONObject(); + filedJson.put(filed, value); + JSONObject term = new JSONObject(); + term.put("term", filedJson); + boolMustBuild.add(term); + return this; + } + + /** + * 范围条件是否必须 + */ + public ElasticSearchQuery rangeBool(String filed, Serializable from, Serializable to) { + 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); + boolMustBuild.add(range); + return this; + } + + public JSONObject builder() { + return this.boolBuild; + } + public void match(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject match = new JSONObject(); 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 index 8995127..89cf0dd 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -67,11 +67,6 @@ this.filterBoolMustnot = this.filterBoolMustnot(); } - - public JSONObject getBool() { - return this.bool; - } - public ElasticSearchQuery(JSONObject body) { this.body = body; } @@ -103,14 +98,6 @@ return bool.getJSONArray(MUST); } - private JSONArray boolShould() { - JSONObject bool = this.bool(); - if (ObjectUtil.isEmpty(bool.get(SHOULD))) { - bool.put(SHOULD, new JSONArray()); - } - return bool.getJSONArray(SHOULD); - } - private JSONObject filter() { JSONObject bool = this.bool; if (ObjectUtil.isEmpty(bool.get(FILTER))) { @@ -149,6 +136,60 @@ this.boolShould.add(BoolMust); } + public void endShould() { + this.filterBoolMust.add(this.boolShould); + } + + private JSONObject boolBuild; + private JSONArray boolMustBuild; + + private JSONArray boolShould() { + boolBuild=new JSONObject(); + boolBuild.put(BOOL, new JSONObject()); + boolBuild.getJSONObject(BOOL).put(SHOULD, new JSONArray()); + return boolBuild.getJSONObject(BOOL).getJSONArray(SHOULD); + } + + public ElasticSearchQuery build() { + boolBuild=new JSONObject(); + boolBuild.put(BOOL, new JSONObject()); + boolBuild.getJSONObject(BOOL).put(MUST, new JSONArray()); + boolMustBuild = boolBuild.getJSONObject(BOOL).getJSONArray(MUST); + return this; + } + + public ElasticSearchQuery termBool(String filed, Serializable value) { + JSONObject filedJson = new JSONObject(); + filedJson.put(filed, value); + JSONObject term = new JSONObject(); + term.put("term", filedJson); + boolMustBuild.add(term); + return this; + } + + /** + * 范围条件是否必须 + */ + public ElasticSearchQuery rangeBool(String filed, Serializable from, Serializable to) { + 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); + boolMustBuild.add(range); + return this; + } + + public JSONObject builder() { + return this.boolBuild; + } + public void match(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject match = new JSONObject(); 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 index 4c7cea5..cf7c98c 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -3,6 +3,7 @@ import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.casic.missiles.dto.EsResponse; import org.apache.http.nio.entity.NStringEntity; import org.apache.http.util.EntityUtils; @@ -51,7 +52,6 @@ } /** - * * @param index * @param type * @param id @@ -102,7 +102,7 @@ /** * 根据条件查询 */ - public static List searchQuery(String index, String type, ElasticSearchQuery query) throws IOException { + public static List 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()); @@ -110,11 +110,28 @@ // 获取response body,转换为json对象 JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); EsResponse esResponse = result.toJavaObject(EsResponse.class); - List results=esResponse.getDatas(); + List results = esResponse.getDatas(); return results; } /** + * 根据条件查询返回分页 + */ + public static Page searchQueryPage(Page page, 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())); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + List results = esResponse.getDatas(); + page.setRecords(results); + page.setTotal(Long.valueOf(esResponse.getHits().getTotal())); + return page; + } + + /** * 根据条件进行滚动查询 */ public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException { @@ -154,7 +171,6 @@ /** * 批处理 - * */ public static Response bulk(String index, String type, String entity) throws IOException { String method = "POST"; 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 index 8995127..89cf0dd 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -67,11 +67,6 @@ this.filterBoolMustnot = this.filterBoolMustnot(); } - - public JSONObject getBool() { - return this.bool; - } - public ElasticSearchQuery(JSONObject body) { this.body = body; } @@ -103,14 +98,6 @@ return bool.getJSONArray(MUST); } - private JSONArray boolShould() { - JSONObject bool = this.bool(); - if (ObjectUtil.isEmpty(bool.get(SHOULD))) { - bool.put(SHOULD, new JSONArray()); - } - return bool.getJSONArray(SHOULD); - } - private JSONObject filter() { JSONObject bool = this.bool; if (ObjectUtil.isEmpty(bool.get(FILTER))) { @@ -149,6 +136,60 @@ this.boolShould.add(BoolMust); } + public void endShould() { + this.filterBoolMust.add(this.boolShould); + } + + private JSONObject boolBuild; + private JSONArray boolMustBuild; + + private JSONArray boolShould() { + boolBuild=new JSONObject(); + boolBuild.put(BOOL, new JSONObject()); + boolBuild.getJSONObject(BOOL).put(SHOULD, new JSONArray()); + return boolBuild.getJSONObject(BOOL).getJSONArray(SHOULD); + } + + public ElasticSearchQuery build() { + boolBuild=new JSONObject(); + boolBuild.put(BOOL, new JSONObject()); + boolBuild.getJSONObject(BOOL).put(MUST, new JSONArray()); + boolMustBuild = boolBuild.getJSONObject(BOOL).getJSONArray(MUST); + return this; + } + + public ElasticSearchQuery termBool(String filed, Serializable value) { + JSONObject filedJson = new JSONObject(); + filedJson.put(filed, value); + JSONObject term = new JSONObject(); + term.put("term", filedJson); + boolMustBuild.add(term); + return this; + } + + /** + * 范围条件是否必须 + */ + public ElasticSearchQuery rangeBool(String filed, Serializable from, Serializable to) { + 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); + boolMustBuild.add(range); + return this; + } + + public JSONObject builder() { + return this.boolBuild; + } + public void match(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject match = new JSONObject(); 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 index 4c7cea5..cf7c98c 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -3,6 +3,7 @@ import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.casic.missiles.dto.EsResponse; import org.apache.http.nio.entity.NStringEntity; import org.apache.http.util.EntityUtils; @@ -51,7 +52,6 @@ } /** - * * @param index * @param type * @param id @@ -102,7 +102,7 @@ /** * 根据条件查询 */ - public static List searchQuery(String index, String type, ElasticSearchQuery query) throws IOException { + public static List 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()); @@ -110,11 +110,28 @@ // 获取response body,转换为json对象 JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); EsResponse esResponse = result.toJavaObject(EsResponse.class); - List results=esResponse.getDatas(); + List results = esResponse.getDatas(); return results; } /** + * 根据条件查询返回分页 + */ + public static Page searchQueryPage(Page page, 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())); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + List results = esResponse.getDatas(); + page.setRecords(results); + page.setTotal(Long.valueOf(esResponse.getHits().getTotal())); + return page; + } + + /** * 根据条件进行滚动查询 */ public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException { @@ -154,7 +171,6 @@ /** * 批处理 - * */ public static Response bulk(String index, String type, String entity) throws IOException { String method = "POST"; diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/dto/DataGasDTO.java b/casic-data/src/main/java/com/casic/missiles/modular/system/dto/DataGasDTO.java index 4f83346..ef61e81 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/dto/DataGasDTO.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/dto/DataGasDTO.java @@ -10,10 +10,10 @@ */ @Data public class DataGasDTO { - @NotNull +// @NotNull private Long monitorId; - @NotNull +// @NotNull private String startTime; - @NotNull +// @NotNull private String endTime; } 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 index 8995127..89cf0dd 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -67,11 +67,6 @@ this.filterBoolMustnot = this.filterBoolMustnot(); } - - public JSONObject getBool() { - return this.bool; - } - public ElasticSearchQuery(JSONObject body) { this.body = body; } @@ -103,14 +98,6 @@ return bool.getJSONArray(MUST); } - private JSONArray boolShould() { - JSONObject bool = this.bool(); - if (ObjectUtil.isEmpty(bool.get(SHOULD))) { - bool.put(SHOULD, new JSONArray()); - } - return bool.getJSONArray(SHOULD); - } - private JSONObject filter() { JSONObject bool = this.bool; if (ObjectUtil.isEmpty(bool.get(FILTER))) { @@ -149,6 +136,60 @@ this.boolShould.add(BoolMust); } + public void endShould() { + this.filterBoolMust.add(this.boolShould); + } + + private JSONObject boolBuild; + private JSONArray boolMustBuild; + + private JSONArray boolShould() { + boolBuild=new JSONObject(); + boolBuild.put(BOOL, new JSONObject()); + boolBuild.getJSONObject(BOOL).put(SHOULD, new JSONArray()); + return boolBuild.getJSONObject(BOOL).getJSONArray(SHOULD); + } + + public ElasticSearchQuery build() { + boolBuild=new JSONObject(); + boolBuild.put(BOOL, new JSONObject()); + boolBuild.getJSONObject(BOOL).put(MUST, new JSONArray()); + boolMustBuild = boolBuild.getJSONObject(BOOL).getJSONArray(MUST); + return this; + } + + public ElasticSearchQuery termBool(String filed, Serializable value) { + JSONObject filedJson = new JSONObject(); + filedJson.put(filed, value); + JSONObject term = new JSONObject(); + term.put("term", filedJson); + boolMustBuild.add(term); + return this; + } + + /** + * 范围条件是否必须 + */ + public ElasticSearchQuery rangeBool(String filed, Serializable from, Serializable to) { + 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); + boolMustBuild.add(range); + return this; + } + + public JSONObject builder() { + return this.boolBuild; + } + public void match(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject match = new JSONObject(); 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 index 4c7cea5..cf7c98c 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -3,6 +3,7 @@ import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.casic.missiles.dto.EsResponse; import org.apache.http.nio.entity.NStringEntity; import org.apache.http.util.EntityUtils; @@ -51,7 +52,6 @@ } /** - * * @param index * @param type * @param id @@ -102,7 +102,7 @@ /** * 根据条件查询 */ - public static List searchQuery(String index, String type, ElasticSearchQuery query) throws IOException { + public static List 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()); @@ -110,11 +110,28 @@ // 获取response body,转换为json对象 JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); EsResponse esResponse = result.toJavaObject(EsResponse.class); - List results=esResponse.getDatas(); + List results = esResponse.getDatas(); return results; } /** + * 根据条件查询返回分页 + */ + public static Page searchQueryPage(Page page, 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())); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + List results = esResponse.getDatas(); + page.setRecords(results); + page.setTotal(Long.valueOf(esResponse.getHits().getTotal())); + return page; + } + + /** * 根据条件进行滚动查询 */ public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException { @@ -154,7 +171,6 @@ /** * 批处理 - * */ public static Response bulk(String index, String type, String entity) throws IOException { String method = "POST"; diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/dto/DataGasDTO.java b/casic-data/src/main/java/com/casic/missiles/modular/system/dto/DataGasDTO.java index 4f83346..ef61e81 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/dto/DataGasDTO.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/dto/DataGasDTO.java @@ -10,10 +10,10 @@ */ @Data public class DataGasDTO { - @NotNull +// @NotNull private Long monitorId; - @NotNull +// @NotNull private String startTime; - @NotNull +// @NotNull private String endTime; } diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/BusPatrolLogServiceImpl.java b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/BusPatrolLogServiceImpl.java index 1f6f780..d4edd84 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/BusPatrolLogServiceImpl.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/BusPatrolLogServiceImpl.java @@ -47,18 +47,17 @@ List obserpoiInfoList = getObserpoiList(monitorId); ElasticSearchQuery elasticSearchQuery = new ElasticSearchQuery(); if (CollectionUtils.isNotEmpty(obserpoiInfoList)) { - for(BusObserpoiInfo busObserpoiInfo:obserpoiInfoList){ + for (BusObserpoiInfo busObserpoiInfo : obserpoiInfoList) { ElasticSearchQuery mustQuery = new ElasticSearchQuery(); - mustQuery.term("direction",busObserpoiInfo.getDirection()); - mustQuery.term("pitch",busObserpoiInfo.getPitch()); - elasticSearchQuery.should(mustQuery.getBool()); + elasticSearchQuery.should(mustQuery.build().rangeBool("direction", busObserpoiInfo.getDirection() - 0.5, busObserpoiInfo.getDirection() + 0.5) + .rangeBool("pitch", busObserpoiInfo.getPitch() - 0.5, busObserpoiInfo.getPitch() + 0.5).builder()); } } + elasticSearchQuery.endShould(); elasticSearchQuery.range("logTime", startTime, endTime); elasticSearchQuery.sort("logTime", "DESC"); elasticSearchQuery.size(Integer.valueOf(String.valueOf(page.getSize()))); - List dataGasEsList = ElasticSearchUtil.searchQuery(DataConst.DATA_GAS_ES_INDEX, DataConst.DATA_GAS_ES_TYPE, elasticSearchQuery); - page.setRecords(dataGasEsList); + page = ElasticSearchUtil.searchQueryPage(page, DataConst.DATA_GAS_ES_INDEX, DataConst.DATA_GAS_ES_TYPE, elasticSearchQuery); return page; } 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 index 8995127..89cf0dd 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -67,11 +67,6 @@ this.filterBoolMustnot = this.filterBoolMustnot(); } - - public JSONObject getBool() { - return this.bool; - } - public ElasticSearchQuery(JSONObject body) { this.body = body; } @@ -103,14 +98,6 @@ return bool.getJSONArray(MUST); } - private JSONArray boolShould() { - JSONObject bool = this.bool(); - if (ObjectUtil.isEmpty(bool.get(SHOULD))) { - bool.put(SHOULD, new JSONArray()); - } - return bool.getJSONArray(SHOULD); - } - private JSONObject filter() { JSONObject bool = this.bool; if (ObjectUtil.isEmpty(bool.get(FILTER))) { @@ -149,6 +136,60 @@ this.boolShould.add(BoolMust); } + public void endShould() { + this.filterBoolMust.add(this.boolShould); + } + + private JSONObject boolBuild; + private JSONArray boolMustBuild; + + private JSONArray boolShould() { + boolBuild=new JSONObject(); + boolBuild.put(BOOL, new JSONObject()); + boolBuild.getJSONObject(BOOL).put(SHOULD, new JSONArray()); + return boolBuild.getJSONObject(BOOL).getJSONArray(SHOULD); + } + + public ElasticSearchQuery build() { + boolBuild=new JSONObject(); + boolBuild.put(BOOL, new JSONObject()); + boolBuild.getJSONObject(BOOL).put(MUST, new JSONArray()); + boolMustBuild = boolBuild.getJSONObject(BOOL).getJSONArray(MUST); + return this; + } + + public ElasticSearchQuery termBool(String filed, Serializable value) { + JSONObject filedJson = new JSONObject(); + filedJson.put(filed, value); + JSONObject term = new JSONObject(); + term.put("term", filedJson); + boolMustBuild.add(term); + return this; + } + + /** + * 范围条件是否必须 + */ + public ElasticSearchQuery rangeBool(String filed, Serializable from, Serializable to) { + 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); + boolMustBuild.add(range); + return this; + } + + public JSONObject builder() { + return this.boolBuild; + } + public void match(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject match = new JSONObject(); 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 index 4c7cea5..cf7c98c 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -3,6 +3,7 @@ import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.casic.missiles.dto.EsResponse; import org.apache.http.nio.entity.NStringEntity; import org.apache.http.util.EntityUtils; @@ -51,7 +52,6 @@ } /** - * * @param index * @param type * @param id @@ -102,7 +102,7 @@ /** * 根据条件查询 */ - public static List searchQuery(String index, String type, ElasticSearchQuery query) throws IOException { + public static List 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()); @@ -110,11 +110,28 @@ // 获取response body,转换为json对象 JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); EsResponse esResponse = result.toJavaObject(EsResponse.class); - List results=esResponse.getDatas(); + List results = esResponse.getDatas(); return results; } /** + * 根据条件查询返回分页 + */ + public static Page searchQueryPage(Page page, 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())); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + List results = esResponse.getDatas(); + page.setRecords(results); + page.setTotal(Long.valueOf(esResponse.getHits().getTotal())); + return page; + } + + /** * 根据条件进行滚动查询 */ public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException { @@ -154,7 +171,6 @@ /** * 批处理 - * */ public static Response bulk(String index, String type, String entity) throws IOException { String method = "POST"; diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/dto/DataGasDTO.java b/casic-data/src/main/java/com/casic/missiles/modular/system/dto/DataGasDTO.java index 4f83346..ef61e81 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/dto/DataGasDTO.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/dto/DataGasDTO.java @@ -10,10 +10,10 @@ */ @Data public class DataGasDTO { - @NotNull +// @NotNull private Long monitorId; - @NotNull +// @NotNull private String startTime; - @NotNull +// @NotNull private String endTime; } diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/BusPatrolLogServiceImpl.java b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/BusPatrolLogServiceImpl.java index 1f6f780..d4edd84 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/BusPatrolLogServiceImpl.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/BusPatrolLogServiceImpl.java @@ -47,18 +47,17 @@ List obserpoiInfoList = getObserpoiList(monitorId); ElasticSearchQuery elasticSearchQuery = new ElasticSearchQuery(); if (CollectionUtils.isNotEmpty(obserpoiInfoList)) { - for(BusObserpoiInfo busObserpoiInfo:obserpoiInfoList){ + for (BusObserpoiInfo busObserpoiInfo : obserpoiInfoList) { ElasticSearchQuery mustQuery = new ElasticSearchQuery(); - mustQuery.term("direction",busObserpoiInfo.getDirection()); - mustQuery.term("pitch",busObserpoiInfo.getPitch()); - elasticSearchQuery.should(mustQuery.getBool()); + elasticSearchQuery.should(mustQuery.build().rangeBool("direction", busObserpoiInfo.getDirection() - 0.5, busObserpoiInfo.getDirection() + 0.5) + .rangeBool("pitch", busObserpoiInfo.getPitch() - 0.5, busObserpoiInfo.getPitch() + 0.5).builder()); } } + elasticSearchQuery.endShould(); elasticSearchQuery.range("logTime", startTime, endTime); elasticSearchQuery.sort("logTime", "DESC"); elasticSearchQuery.size(Integer.valueOf(String.valueOf(page.getSize()))); - List dataGasEsList = ElasticSearchUtil.searchQuery(DataConst.DATA_GAS_ES_INDEX, DataConst.DATA_GAS_ES_TYPE, elasticSearchQuery); - page.setRecords(dataGasEsList); + page = ElasticSearchUtil.searchQueryPage(page, DataConst.DATA_GAS_ES_INDEX, DataConst.DATA_GAS_ES_TYPE, elasticSearchQuery); return page; } diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java index 3833e77..87d0ae5 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java @@ -42,7 +42,7 @@ elasticSearchQuery.range("logTime", startTime, endTime); elasticSearchQuery.size(10000); elasticSearchQuery.sort("logTime", "DESC"); - List dataGasEsList = ElasticSearchUtil.searchQueryScroll(DataConst.DATA_GAS_ES_INDEX, DataConst.DATA_GAS_ES_TYPE, elasticSearchQuery); + List dataGasEsList = ElasticSearchUtil.searchQuery(DataConst.DATA_GAS_ES_INDEX, DataConst.DATA_GAS_ES_TYPE, elasticSearchQuery); return dataGasEsList; } }