在ElasticSearch 7.4 Java客户端的geoShapeQuery中使用geojson多边形

有人在 ElasticSearch 7.x 形状查询中使用geojson形状的代码示例吗?

当我使用ES构建器构建形状时,它可以工作,但是我需要使用传入的geojson(见下文),这可能需要将geojson解析为ES形状:

{"type":"Polygon","coordinates":[[[-82.30957031249999,26.657277674217585],[-81.7767333984375,25.84686509678058],[-80.90057373046875,24.986058021167594],[-80.25238037109375,25.16517336866393],[-79.97222900390625,26.08885491679362],[-79.771728515625,26.76277822801415],[-80.2606201171875,27.25707120788274],[-80.83740234375,27.332735136859146],[-81.529541015625,27.166695222253114],[-82.30957031249999,26.657277674217585]]]}
snow6298 回答:在ElasticSearch 7.4 Java客户端的geoShapeQuery中使用geojson多边形

我也一直在为此苦苦挣扎,尽管我实际上想直接将JTS Geometry馈送到查询中。我想出的解决方案是使用WrapperQueryBuilder用JSON编写查询:

import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

...

String queryName = "...";
String geoJson = "...";

QueryBuilder geoShapeQuery = QueryBuilders.wrapperQuery(
  String.format(
    "{ \"geo_shape\": { \"%s\": { \"shape\": %s,\"relation\": \"%s\" } } }",queryName,geoJson,ShapeRelation.INTERSECTS.getRelationName()));
,

我的查询形状是简单的多边形,因此我最终将其转换为点数组,并根据这些点创建多边形:

if (null != shape) {
   JSONObject jsonShape = new JSONObject(shape);
   JSONArray coords = jsonShape.getJSONArray("coordinates");
   CoordinatesBuilder cb = new CoordinatesBuilder();
   for (Object coord: coords.getJSONArray(0)) {
      JSONArray c = new JSONArray(coord.toString());
      cb.coordinate((Double)c.get(0),(Double)c.get(1));
   }

   PolygonBuilder pb  = new PolygonBuilder(cb);
   gsqb = QueryBuilders.geoShapeQuery("FOOTPRINT",pb.buildGeometry());
}
本文链接:https://www.f2er.com/3155387.html

大家都在问