geo_shape查询

筛选使用geo_shape或geo_point类型索引的文档。

需要geo_shape映射或geo_point映射。

geo_shape查询使用与geo_shape映射相同的网格正方形表示来查找具有与查询形状相交的形状的文档。它还将使用与字段映射定义的相同的前缀树配置。

该查询支持两种定义查询形状的方法,一种是提供完整的形状定义,另一种是在另一个索引中引用预索引的形状名称。下面用示例定义了这两种格式。

内联的形状定义

与geo_shape类型类似,geo_shape查询使用GeoJSON来表示形状。

给定以下索引,其中位置为geo_shape字段:

PUT /example
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

POST /example/_doc?refresh
{
  "name": "Wind & Wetter, Berlin, Germany",
  "location": {
    "type": "point",
    "coordinates": [ 13.400544, 52.530286 ]
  }
}

下面的查询将使用Elasticsearch的envelope GeoJSON扩展查找该点:

GET /example/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
              "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
            },
            "relation": "within"
          }
        }
      }
    }
  }
}

类似地,可以在geo_point字段上查询上述查询。

PUT /example_points
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}

PUT /example_points/_doc/1?refresh
{
  "name": "Wind & Wetter, Berlin, Germany",
  "location": [13.400544, 52.530286]
}

使用相同的查询,将返回具有匹配geo_point字段的文档。

GET /example_points/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
              "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
            },
            "relation": "intersects"
          }
        }
      }
    }
  }
}
{
  "took" : 17,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "example_points",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name": "Wind & Wetter, Berlin, Germany",
          "location": [13.400544, 52.530286]
        }
      }
    ]
  }
}

预先索引形状

该查询还支持使用已经在另一个索引中建立索引的形状。当您有一个预定义的形状列表,并且希望使用逻辑名称(例如New Zealand)引用该列表,而不是每次都必须提供坐标时,这一点特别有用。在这种情况下,只需要提供:

  • id - 包含预索引形状的文档的ID
  • index - 预索引形状所在的索引名称。默认的形状。
  • path - 指定为包含预索引形状的路径的字段。默认的形状。
  • routing - 如果需要,形状文档的路由。

下面是一个使用Filter和预索引形状的例子:

PUT /shapes
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

PUT /shapes/_doc/deu
{
  "location": {
    "type": "envelope",
    "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
  }
}

GET /example/_search
{
  "query": {
    "bool": {
      "filter": {
        "geo_shape": {
          "location": {
            "indexed_shape": {
              "index": "shapes",
              "id": "deu",
              "path": "location"
            }
          }
        }
      }
    }
  }
}

 空间关系

geo_shape策略映射参数决定了在搜索时可以使用哪些空间关系算子。

以下是搜索地理位置字段时可用的空间关系操作符的完整列表:

  • INTERSECTS - (default) 返回其geo_shape或geo_point字段与查询几何图形相交的所有文档。
  • DISJOINT - 返回其geo_shape或geo_point字段与查询几何图形没有共同之处的所有文档。
  • WITHIN - 返回其geo_shape或geo_point字段在查询几何图形内的所有文档。不支持直线几何图形。
  • CONTAINS -返回其geo_shape或geo_point字段包含查询几何图形的所有文档。

忽略未映射的

备注

使用PrefixTrees实现的地理形状查询将不会执行搜索。Allow_expensive_queries设置为false。

当数据在geo_shape字段中作为一个形状数组进行索引时,这些数组被视为一个形状。由于这个原因,下面的请求是等价的。

PUT /test/_doc/1
{
  "location": [
    {
      "coordinates": [46.25,20.14],
      "type": "point"
    },
    {
      "coordinates": [47.49,19.04],
      "type": "point"
    }
  ]
}
PUT /test/_doc/1
{
  "location":
    {
      "coordinates": [[46.25,20.14],[47.49,19.04]],
      "type": "multipoint"
    }
}

 geo_shape查询假设geo_shape字段使用默认的右(逆时针)方向。See Polygon orientation.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值