如何在Elasticsearch中查找包含给定点的多边形

时间:2019-11-15 19:19:57

标签: elasticsearch geo elasticsearch-percolate elasticsearch-7

我需要在大约50k地形多边形(在ES上存储为geo_shape多边形)的数据库上建立查询,并在其中给出一个点,并返回包含该点的每个多边形。

我设法使用渗滤查询(下面的示例)来做到这一点,但我读到渗滤查询扩展性不好的地方。

有没有更有效的方法来实现此行为?

使用渗滤剂的示例:

Demo polygons

PUT geo_demo
{
  "mappings": {
    "properties": {
      "thepoly": {
        "type": "percolator"
      },
      "thepoint": {
        "type": "geo_point"
      }
    }
  }
}

#region 1 (red)
POST /geo_demo/_doc/1
{
  "thepoly": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_polygon": {
          "thepoint": {
            "points": [
              "-23.573978,-46.664806",
              "-23.583978,-46.664806",
              "-23.583978,-46.658806",
              "-23.573978,-46.658806",
              "-23.573978,-46.664806"
            ]
          }
        }
      }
    }
  }
}

#region 2 (green)
POST /geo_demo/_doc/2
{
  "thepoly": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_polygon": {
          "thepoint": {
            "points": [
              "-23.579978,-46.664806",
              "-23.583978,-46.664806",
              "-23.583978,-46.652806",
              "-23.579978,-46.652806",
              "-23.579978,-46.664806"
            ]
          }
        }
      }
    }
  }
}

#should match doc/1 only
GET /geo_demo/_search
{
  "query": {
    "percolate": {
      "field": "thepoly",
      "document": {
        "thepoint": "-23.577007,-46.661811"
      }
    }
  }
}

#should match both doc/1 and doc/2
GET /geo_demo/_search
{
  "query": {
    "percolate": {
      "field": "thepoly",
      "document": {
        "thepoint": "-23.582002,-46.661811"
      }
    }
  }
}

#should match doc/2 only
GET /geo_demo/_search
{
  "query": {
    "percolate": {
      "field": "thepoly",
      "document": {
        "thepoint": "-23.582041,-46.655717"
      }
    }
  }
}

#should match none
GET /geo_demo/_search
{
  "query": {
    "percolate": {
      "field": "thepoly",
      "document": {
        "thepoint": "-23.576771,-46.655674"
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

除非您有充分的理由,否则您几乎不需要弹性评估。

对于50K多边形,您可以轻松地将它们放在堆中,或将每个多边形分解为地质哈希表。

您可以拥有一个以geohash为键,多边形id为值的堆内地图。

当有点进入时,您首先要计算地理哈希值,然后使用Map#get来检查该点在地图上还是包含该点的多边形。