在 Elastic Search 中找到从一个点到 n 个最近的多边形的距离?

问题描述 投票:0回答:1

这类似于 Find closest GeoJSON polygon to point when point is outside of all polygons in Elasticsearch 但不是返回 the 最近的多边形,我想返回所有 documents 多边形比特定的更近距离(例如 10 公里)。我希望每个多边形的距离包含在结果中,并且应该按距离排序(升序或降序无关紧要)。

所提及问题中的评论指出了一个 github 问题,现在已经解决了(自 Elastic Search 7.7 起),但我似乎无法根据 文档 弄清楚如何去做。

更新

阅读joeanswer后,我想澄清一下,“距离”是指到多边形边界最近点的最近距离。就像 PostGIS question 中所描述的那样。

elasticsearch
1个回答
1
投票

您可以使用带有缓冲圆的

geo_shape
查询,它将充当您感兴趣的多边形将相交的伞。如果他们这样做,他们匹配。 唯一的问题是“到每个多边形的距离”有点随意——我们是在谈论多边形边界的最近点吗?或者它的中心?

我只能想出后者的实现,但它需要一个辅助中心场,因为脚本化的中心计算太繁重了。

这是视觉效果:

建立索引:

PUT geo/
{
  "mappings": {
    "properties": {
      "polygon_field": {
        "type": "geo_shape"
      },
      "center_field": {
        "type": "geo_point"
      }
    }
  }
}

同步 4 个多边形及其中心:

POST _bulk
{"index":{"_index":"geo","_type":"_doc"}}
{"polygon_field":{"type":"Polygon","coordinates":[[[16.391773223876953,48.20248146453242],[16.389713287353516,48.19939223822629],[16.39812469482422,48.19721822655714],[16.39812469482422,48.200422001027874],[16.395721435546875,48.20248146453242],[16.391773223876953,48.20248146453242]]]},"center_field":[16.393918991088867,48.199849845544776]}
{"index":{"_index":"geo","_type":"_doc"}}
{"polygon_field":{"type":"Polygon","coordinates":[[[16.340789794921875,48.207172155652366],[16.333751678466797,48.203625575146994],[16.337528228759766,48.199735494793444],[16.34490966796875,48.19882013883662],[16.34490966796875,48.20293911184484],[16.341476440429688,48.20682894891699],[16.340789794921875,48.207172155652366]]]},"center_field":[16.339330673217773,48.20299614724449]}
{"index":{"_index":"geo","_type":"_doc"}}
{"polygon_field":{"type":"Polygon","coordinates":[[[16.37014389038086,48.23370651063653],[16.36585235595703,48.23061916787409],[16.369457244873047,48.2286751898296],[16.37683868408203,48.23119091206881],[16.374778747558594,48.233592167930034],[16.37014389038086,48.23370651063653]]]},"center_field":[16.37134552001953,48.23119085023306]}
{"index":{"_index":"geo","_type":"_doc"}}
{"polygon_field":{"type":"Polygon","coordinates":[[[16.47777557373047,48.14936662796115],[16.466102600097656,48.143868849060205],[16.475372314453125,48.13562107648419],[16.494598388671875,48.13493370228957],[16.494598388671875,48.144097934938884],[16.47777557373047,48.14936662796115]]]},"center_field":[16.480350494384766,48.14215016512536]}

按最接近的(相对于中心)升序搜索和排序:

GET geo/_search
{
  "query": {
    "geo_shape": {
      "polygon_field": {
        "shape": {
          "type": "circle",
          "radius": "10km",
          "coordinates": [
            16.3704,
            48.21
          ]
        },
        "relation": "intersects"
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "center_field": [
          16.3704,
          48.21
        ],
        "order": "asc",
        "unit": "km",
        "mode": "min"
      }
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.