ElasticSearch:检查多个圆的半径中是否存在GeoPoint

brc7rcf0  于 2023-03-29  发布在  ElasticSearch
关注(0)|答案(1)|浏览(130)

我的要求是检查一个partucular geoPoint是否福尔斯在一个圆的半径内。
我正在使用geoShape:圈出存放位置,我的文档如下:

PUT location_test/doc/1
    {
        "location" : {
            "type" : "circle",
            "coordinates" : [73.7769,18.5642],
          "radius": "10mi"
        }
    }

查询如下:

GET location_test/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "geo_shape": {
                "location": {
                  "shape": {
                    "type": "point",
                    "coordinates": [
                      73.877097,
                      18.455303
                    ],
                    "relation": "contains"
                  }
                }
              }
            }
          ]
        }
      }
    }

此查询适用于单圆几何形状。
然而,现在我想检查一个特定的geoPoint是否福尔斯在多个圆的半径内。
我们可以有这样的文件:

{
  "location": [
    {
      "type": "circle",
      "coordinates": [
        73.7769,
        18.5642
      ],
      "radius": "10mi"
    },
    {
      "type": "circle",
      "coordinates": [
        -118.240853,
        34.052997
      ],
      "radius": "10mi"
    }
  ]
}

并且具有查询以检查地理点是否福尔斯在哪个圆中。
或者有没有其他方法可以实现这一目标?

编辑使用地理点数组为特定地理点排序文档是否是一种好的做法?

Mapping :
    {
      "mappings": {
        "doc": {
          "properties": {
            "locationPoint": {
              "type": "geo_point"
            }
          }
        }
      }
    }

PUT location_test2/doc/1
{
  "locationPoint": ["34.075433, -118.307228","36.336356,-119.304597"]
}

PUT location_test2/doc/2
{
  "locationPoint": ["34.075433, -118.307228"]  
  }

GET location_test2/_search
{
  "sort": [
      {
      "_geo_distance": {
        "locationPoint": "34.075433, -118.307228",
        "order": "asc"
      }
    }
  ]
}
bqujaahr

bqujaahr1#

你可以在一个文档中有多个圆,如果 * 任何 * 个圆包含你的点,搜索仍然会匹配。为了简洁起见,折叠步骤:

PUT location_test
{"mappings":{"properties":{"location":{"type":"geo_shape","strategy":"recursive"}}}}

在你的圆数组中:

PUT location_test/_doc/2
{"location":[{"type":"circle","coordinates":[73.7769,18.5642],"radius":"10mi"},{"type":"circle","coordinates":[-118.240853,34.052997],"radius":"10mi"}]}

与单个圆的查询相同。

GET location_test/_search
{
  "query":{
    "bool":{
      "must":[
        {
          "geo_shape":{
            "location":{
              "shape":{
                "type":"point",
                "coordinates":[
                  73.7769,
                  18.5642
                ],
                "relation":"contains"
              }
            }
          }
        }
      ]
    }
  }
}

这就产生了我们感兴趣的文档。违反直觉但很好的一点是,如果你提供一个对象或一个对象列表,它并不重要。ElasticSearch处理这两个对象而不改变Map。
请注意,您的circles位于地球仪的两侧:

如果您意识到这一点,并且像这样查询locations是有意义的,那么一切都很好。
从性能的Angular 来看,请记住圆表示为多边形

取决于你的ES版本,它们被表示为一堆triangles
因此,您可能希望索引圆形多边形而不是圆形,以加快索引速度,甚至考虑将圆合并到一组多边形(MultiPolygon)中,因为从它的外观来看,您的圆列表表示相关的几何体。

相关问题