首页 > 解决方案 > 在 Elasticsearch 中使用预定义的 geo_shapes 创建文档

问题描述

我有两个索引:

  1. Profiles - 有一个字段,它是一个数组geo_shape
  2. 位置 - 具有字段几何,它是geo_shape

配置文件中的每个文档都有许多位置。目前,位置几何的副本存储在配置文件中。

是否可以通过创建预定义的配置文件来改善这一点geo_shapes?我试过了

PUT profiles/profile/1
{
  "locations": [
    {
      "indexed_shape": {
        "id": "LOC1",
        "index": "locations",
        "path": "geometry",
        "type": "location"
      }
    },
    {
      "indexed_shape": {
        "id": "LOC2",
        "index": "locations",
        "path": "geometry",
        "type": "location"
      }
    }
  ]
}

这很像预定义地理形状的查询语法,但无济于事。我在文档中找不到任何内容。这个问题有解决方案吗,还是我必须管理副本?

标签: elasticsearchgis

解决方案


不幸的是,(还)没有办法存储对索引形状的引用。您可以做的是将形状预先索引到专用索引中,然后将这些形状的 ID 存储在配置文件索引中的位置数组中,如下所示:

PUT shapes/doc/LOC1
{
  ... shape definition goes here ...
}
PUT shapes/doc/LOC2
{
  ... shape definition goes here ...
}

PUT profiles/doc/1
{
  "locations": [ "LOC1", "LOC2" ]
  ... other fields
}

然后,当您需要查询时,您可以先对形状索引进行查询,收集匹配形状的 id,然后使用这些 id 查询配置文件索引。我没有办法让它比这更短。

首先,查询形状并收集 id:

POST shapes/_search?filter_path=hits.hits._id
{ 
  "query" : {
    "geo_shape": {
      "location": {
        "shape": {
          "type": "envelope",
          "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
        },
        "relation": "within"
      }
    }
  }
}

=> returns "LOC1", "LOC3", "LOC4"

最后查询profiles索引

POST profiles/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          ...other profile criteria go here...
        },
        { 
          "terms": {
            "locations": ["LOC1", "LOC3", "LOC4" ]
          }
        }
      ]
    }
  }
}

推荐阅读