Link Search Menu Expand Document Documentation Menu

xy 查询

要搜索包含 xy 点xy 形状字段的文档,请使用 xy 查询。

空间关系

当您向 xy 查询提供 xy 形状时,文档中的 xy 字段将使用以下空间关系与提供的形状进行匹配。

关系 描述 支持的 xy 字段类型
INTERSECTS (默认)匹配 xy 点或 xy 形状与查询中提供的形状相交的文档。 xy_point, xy_shape
DISJOINT 匹配 xy 形状与查询中提供的形状不相交的文档。 xy_shape
WITHIN 匹配 xy 形状完全在查询中提供的形状内的文档。 xy_shape
CONTAINS 匹配 xy 形状完全包含查询中提供的形状的文档。 xy_shape

以下示例说明了如何搜索包含 xy 形状的文档。要了解如何搜索包含 xy 点的文档,请参阅“查询 xy 点”部分。

在 xy 查询中定义形状

您可以通过在查询时提供新的形状定义,或通过引用在另一个索引中预先索引的形状名称来在 xy 查询中定义形状。

使用新的形状定义

要为 xy 查询提供新形状,请在 xy_shape 字段中定义它。

以下示例说明了如何搜索包含与查询时定义的 xy 形状匹配的 xy 形状的文档。

首先,创建一个索引并将 geometry 字段映射为 xy_shape

PUT testindex
{
  "mappings": {
    "properties": {
      "geometry": {
        "type": "xy_shape"
      }
    }
  }
}

索引包含点和多边形的文档

PUT testindex/_doc/1
{
  "geometry": { 
    "type": "point",
    "coordinates": [0.5, 3.0]
  }
}

PUT testindex/_doc/2
{
  "geometry" : {
    "type" : "polygon",
    "coordinates" : [
      [[2.5, 6.0],
      [0.5, 4.5], 
      [1.5, 2.0], 
      [3.5, 3.5],
      [2.5, 6.0]]
    ]
  }
}

定义一个 envelope[[minX, maxY], [maxX, minY]] 格式的边界矩形)。搜索包含与该信封相交的 xy 点或形状的文档

GET testindex/_search
{
  "query": {
    "xy_shape": {
      "geometry": {
        "shape": {
          "type": "envelope",
          "coordinates": [ [ 0.0, 6.0], [ 4.0, 2.0] ]
        },
        "relation": "WITHIN"
      }
    }
  }
}

下图描绘了该示例。点和多边形都在边界信封内。

xy shape query

响应包含这两个文档

{
  "took" : 363,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "testindex",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "geometry" : {
            "type" : "point",
            "coordinates" : [
              0.5,
              3.0
            ]
          }
        }
      },
      {
        "_index" : "testindex",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "geometry" : {
            "type" : "polygon",
            "coordinates" : [
              [
                [
                  2.5,
                  6.0
                ],
                [
                  0.5,
                  4.5
                ],
                [
                  1.5,
                  2.0
                ],
                [
                  3.5,
                  3.5
                ],
                [
                  2.5,
                  6.0
                ]
              ]
            ]
          }
        }
      }
    ]
  }
}

使用预索引形状定义

在构建 xy 查询时,您还可以引用在另一个索引中预索引的形状名称。使用此方法,您可以在索引时定义一个 xy 形状,并通过名称引用它,在 indexed_shape 对象中提供以下参数。

参数 描述
index 包含预索引形状的索引名称。
id 包含预索引形状的文档的文档 ID。
路径 包含预索引形状的字段的名称(作为路径)。

以下示例说明了引用另一个索引中预索引形状的名称。在此示例中,pre-indexed-shapes 索引包含定义边界的形状,而 testindex 索引包含根据这些边界检查位置的形状。

首先,创建一个 pre-indexed-shapes 索引,并将此索引的 geometry 字段映射为 xy_shape

PUT pre-indexed-shapes
{
  "mappings": {
    "properties": {
      "geometry": {
        "type": "xy_shape"
      }
    }
  }
}

索引一个指定边界的信封,并将其命名为 rectangle

PUT pre-indexed-shapes/_doc/rectangle
{
  "geometry": {
    "type": "envelope",
    "coordinates" : [ [ 0.0, 6.0], [ 4.0, 2.0] ]
  }
}

将包含点和多边形的文档索引到 testindex 索引中

PUT testindex/_doc/1
{
  "geometry": { 
    "type": "point",
    "coordinates": [0.5, 3.0]
  }
}

PUT testindex/_doc/2
{
  "geometry" : {
    "type" : "polygon",
    "coordinates" : [
      [[2.5, 6.0],
      [0.5, 4.5], 
      [1.5, 2.0], 
      [3.5, 3.5],
      [2.5, 6.0]]
    ]
  }
}

testindex 索引中,使用过滤器搜索与 rectangle 相交的形状的文档

GET testindex/_search
{
  "query": {
    "bool": {
      "filter": {
        "xy_shape": {
          "geometry": {
            "indexed_shape": {
              "index": "pre-indexed-shapes",
              "id": "rectangle",
              "path": "geometry"
            }
          }
        }
      }
    }
  }
}

前面的查询使用默认空间关系 INTERSECTS,并返回点和多边形

{
  "took" : 26,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "testindex",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "geometry" : {
            "type" : "point",
            "coordinates" : [
              0.5,
              3.0
            ]
          }
        }
      },
      {
        "_index" : "testindex",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "geometry" : {
            "type" : "polygon",
            "coordinates" : [
              [
                [
                  2.5,
                  6.0
                ],
                [
                  0.5,
                  4.5
                ],
                [
                  1.5,
                  2.0
                ],
                [
                  3.5,
                  3.5
                ],
                [
                  2.5,
                  6.0
                ]
              ]
            ]
          }
        }
      }
    ]
  }
}

查询 xy 点

您还可以使用 xy 查询来搜索包含 xy 点的文档。

创建一个将 point 映射为 xy_point 的映射

PUT testindex1
{
  "mappings": {
    "properties": {
      "point": {
        "type": "xy_point"
      }
    }
  }
}

索引三个点

PUT testindex1/_doc/1
{
  "point": "1.0, 1.0" 
}

PUT testindex1/_doc/2
{
  "point": "2.0, 0.0" 
}

PUT testindex1/_doc/3
{
  "point": "-2.0, 2.0" 
}

搜索位于中心为 (0, 0) 且半径为 2 的圆内的点

GET testindex1/_search
{
  "query": {
    "xy_shape": {
      "point": {
        "shape": {
          "type": "circle",
          "coordinates": [0.0, 0.0],
          "radius": 2
        }
      }
    }
  }
}

xy 点仅支持默认的 INTERSECTS 空间关系,因此您无需提供 relation 参数。

下图描绘了该示例。点 1 和点 2 在圆内,点 3 在圆外。

xy point query

响应返回文档 1 和 2

{
  "took" : 575,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "testindex1",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "point" : "1.0, 1.0"
        }
      },
      {
        "_index" : "testindex1",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "point" : "2.0, 0.0"
        }
      }
    ]
  }
}
剩余 350 字符

有问题?

想做贡献?