|
@@ -2,26 +2,35 @@ package org.jetlinks.community.elastic.search.utils;
|
|
|
|
|
|
|
|
import lombok.NonNull;
|
|
import lombok.NonNull;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.elasticsearch.common.geo.ShapeRelation;
|
|
|
|
|
+import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
|
|
+import org.elasticsearch.common.xcontent.XContentType;
|
|
|
|
|
+import org.elasticsearch.common.xcontent.json.JsonXContent;
|
|
|
|
|
+import org.elasticsearch.common.xcontent.json.JsonXContentParser;
|
|
|
|
|
+import org.elasticsearch.geometry.LinearRing;
|
|
|
|
|
+import org.elasticsearch.geometry.MultiPolygon;
|
|
|
|
|
+import org.elasticsearch.geometry.Polygon;
|
|
|
import org.elasticsearch.index.query.*;
|
|
import org.elasticsearch.index.query.*;
|
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
import org.elasticsearch.search.sort.SortOrder;
|
|
import org.elasticsearch.search.sort.SortOrder;
|
|
|
import org.hswebframework.ezorm.core.param.QueryParam;
|
|
import org.hswebframework.ezorm.core.param.QueryParam;
|
|
|
import org.hswebframework.ezorm.core.param.Sort;
|
|
import org.hswebframework.ezorm.core.param.Sort;
|
|
|
import org.hswebframework.ezorm.core.param.Term;
|
|
import org.hswebframework.ezorm.core.param.Term;
|
|
|
|
|
+import org.hswebframework.web.exception.BusinessException;
|
|
|
import org.jetlinks.community.elastic.search.geo.EsGeoFilter;
|
|
import org.jetlinks.community.elastic.search.geo.EsGeoFilter;
|
|
|
import org.jetlinks.community.elastic.search.index.ElasticSearchIndexMetadata;
|
|
import org.jetlinks.community.elastic.search.index.ElasticSearchIndexMetadata;
|
|
|
import org.jetlinks.community.elastic.search.parser.DefaultLinkTypeParser;
|
|
import org.jetlinks.community.elastic.search.parser.DefaultLinkTypeParser;
|
|
|
import org.jetlinks.core.metadata.DataType;
|
|
import org.jetlinks.core.metadata.DataType;
|
|
|
import org.jetlinks.core.metadata.PropertyMetadata;
|
|
import org.jetlinks.core.metadata.PropertyMetadata;
|
|
|
|
|
+import org.jetlinks.core.metadata.types.GeoShape;
|
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
-import java.util.Collections;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-import java.util.Map;
|
|
|
|
|
-import java.util.Set;
|
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.function.BiConsumer;
|
|
import java.util.function.BiConsumer;
|
|
|
import java.util.function.Consumer;
|
|
import java.util.function.Consumer;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @author zhouhao
|
|
* @author zhouhao
|
|
@@ -77,20 +86,6 @@ public class QueryParamTranslator {
|
|
|
|
|
|
|
|
|
|
|
|
|
public static SearchSourceBuilder convertSearchSourceBuilder(QueryParam queryParam, ElasticSearchIndexMetadata metadata) {
|
|
public static SearchSourceBuilder convertSearchSourceBuilder(QueryParam queryParam, ElasticSearchIndexMetadata metadata) {
|
|
|
-// SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
|
|
|
|
-// if (queryParam.isPaging()) {
|
|
|
|
|
-// sourceBuilder.from(queryParam.getPageIndex() * queryParam.getPageSize());
|
|
|
|
|
-// sourceBuilder.size(queryParam.getPageSize());
|
|
|
|
|
-// }
|
|
|
|
|
-// for (Sort sort : queryParam.getSorts()) {
|
|
|
|
|
-// if (!StringUtils.isEmpty(sort.getName())) {
|
|
|
|
|
-// sourceBuilder.sort(sort.getName(), SortOrder.fromString(sort.getOrder()));
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-//// GeoPolygonQueryBuilder geoPolygonQueryBuilder = QueryBuilders.geoPolygonQuery();
|
|
|
|
|
-//// sourceBuilder.postFilter(geoPolygonQueryBuilder);
|
|
|
|
|
-// return sourceBuilder.query(createQueryBuilder(queryParam,metadata));
|
|
|
|
|
return convertSearchSourceBuilder(queryParam,null,metadata);
|
|
return convertSearchSourceBuilder(queryParam,null,metadata);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -107,10 +102,53 @@ public class QueryParamTranslator {
|
|
|
}
|
|
}
|
|
|
BoolQueryBuilder queryBuilder = (BoolQueryBuilder) createQueryBuilder(queryParam, metadata);
|
|
BoolQueryBuilder queryBuilder = (BoolQueryBuilder) createQueryBuilder(queryParam, metadata);
|
|
|
if(filter!=null){
|
|
if(filter!=null){
|
|
|
- GeoPolygonQueryBuilder geoPolygonQueryBuilder = QueryBuilders.geoPolygonQuery(filter.getFieldName(),filter.getPoints());
|
|
|
|
|
- queryBuilder.filter(geoPolygonQueryBuilder);
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ GeoShape shape = filter.getShape();
|
|
|
|
|
+ GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery(filter.getFieldName(), compulsionCreateMultiPolygon(shape.getCoordinates()))
|
|
|
|
|
+ .relation(ShapeRelation.WITHIN);
|
|
|
|
|
+ queryBuilder.filter(geoShapeQueryBuilder);
|
|
|
|
|
+ }catch (Exception e){
|
|
|
|
|
+ log.error("地图参数有误",e);
|
|
|
|
|
+ throw new BusinessException("地图参数有误");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
return sourceBuilder.query(queryBuilder);
|
|
return sourceBuilder.query(queryBuilder);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private static MultiPolygon compulsionCreateMultiPolygon(List<Object> coordinates){
|
|
|
|
|
+ ArrayList<Polygon> polygons = new ArrayList<>();
|
|
|
|
|
+ for (Object coordinate : coordinates) {
|
|
|
|
|
+ List<List<List<Double>>> polygonCoordinates= (List<List<List<Double>>>) coordinate;
|
|
|
|
|
+ polygons.add(createPolygon(polygonCoordinates));
|
|
|
|
|
+ }
|
|
|
|
|
+ return new MultiPolygon(polygons);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static MultiPolygon createMultiPolygon(List<List<List<List<Double>>>> coordinates){
|
|
|
|
|
+ return new MultiPolygon(coordinates.stream().map(QueryParamTranslator::createPolygon)
|
|
|
|
|
+ .collect(Collectors.toList()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ private static Polygon createPolygon(List<List<List<Double>>> polygonCoordinates){
|
|
|
|
|
+ return new Polygon(createLinearRing(polygonCoordinates.get(0)));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据坐标点获取LineRing(线环)
|
|
|
|
|
+ * @param coordinates
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ private static LinearRing createLinearRing(List<List<Double>> coordinates){
|
|
|
|
|
+ double[] x = new double[coordinates.size()];
|
|
|
|
|
+ double[] y = new double[coordinates.size()];
|
|
|
|
|
+ for (int i = 0; i < coordinates.size(); i++) {
|
|
|
|
|
+ List<Double> coordinate = coordinates.get(i);
|
|
|
|
|
+ x[i]=coordinate.get(0);
|
|
|
|
|
+ y[i]=coordinate.get(1);
|
|
|
|
|
+ }
|
|
|
|
|
+ return new LinearRing(x,y);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|