地理形状字段类型

编辑

geo_shape 数据类型有助于索引和搜索任意地理形状,例如矩形、线条和多边形。如果被索引的数据包含除点之外的形状,则必须使用此映射。如果数据仅包含点,则可以将其索引为 geo_pointgeo_shape

使用此类型的文档可以用于

映射选项

编辑

geo_shape 映射将 GeoJSON 或 WKT 几何对象映射到 geo_shape 类型。要启用它,用户必须显式地将字段映射到 geo_shape 类型。

GeoJSONWKT 中,因此在 Elasticsearch 中,坐标数组内的正确坐标顺序是经度、纬度(X,Y)。这与许多通常使用口语化的纬度、经度(Y,X)的地理空间 API(例如,Google Maps)不同。

选项 描述 默认值

orientation

可选。字段 WKT 多边形的默认方向

此参数仅设置并返回 RIGHT(逆时针)或 LEFT(顺时针)值。但是,您可以以多种方式指定任一值。

要设置 RIGHT,请使用以下参数之一或其大写变体

  • right
  • counterclockwise
  • ccw

要设置 LEFT,请使用以下参数之一或其大写变体

  • left
  • clockwise
  • cw

RIGHT

ignore_malformed

如果为 true,则会忽略格式错误的 GeoJSON 或 WKT 形状。如果为 false(默认值),则格式错误的 GeoJSON 和 WKT 形状会引发异常并拒绝整个文档。

false

ignore_z_value

如果 true(默认值),则会接受三维点(存储在源中),但只会索引纬度和经度值;第三维会被忽略。如果 false,则包含超过纬度和经度(二维)值的地理点会引发异常并拒绝整个文档。

true

coerce

如果 true,则多边形中未闭合的线性环将自动闭合。

false

index

字段是否应可快速搜索?接受 true(默认值)和 false。仅启用 doc_values 的字段仍然可以查询,但速度较慢。

true

doc_values

是否应以列式方式将字段存储在磁盘上,以便稍后将其用于聚合或脚本?

true

索引方法

编辑

地理形状类型通过将形状分解为三角形网格并将每个三角形索引为 BKD 树中的 7 维点来进行索引。由于所有空间关系都是使用原始形状的编码矢量表示计算的,因此这提供了接近完美的空间分辨率(精度高达十进制度 1e-7)。镶嵌器的性能主要取决于定义多边形/多边形的顶点数。

示例
编辑
resp = client.indices.create(
    index="example",
    mappings={
        "properties": {
            "location": {
                "type": "geo_shape"
            }
        }
    },
)
print(resp)
response = client.indices.create(
  index: 'example',
  body: {
    mappings: {
      properties: {
        location: {
          type: 'geo_shape'
        }
      }
    }
  }
)
puts response
const response = await client.indices.create({
  index: "example",
  mappings: {
    properties: {
      location: {
        type: "geo_shape",
      },
    },
  },
});
console.log(response);
PUT /example
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

输入结构

编辑

可以使用 GeoJSON众所周知的文本 (WKT) 格式表示形状。下表提供了 GeoJSON 和 WKT 到 Elasticsearch 类型的映射

GeoJSON 类型 WKT 类型 Elasticsearch 类型 描述

Point

POINT

point

单个地理坐标。注意:Elasticsearch 仅使用 WGS-84 坐标。

LineString

LINESTRING

linestring

由两个或多个点给定的任意线。

Polygon

POLYGON

polygon

一个闭合多边形,其第一个和最后一个点必须匹配,因此需要 n + 1 个顶点来创建一个 n 边多边形,并且最少需要 4 个顶点。

MultiPoint

MULTIPOINT

multipoint

一组不相连但可能相关的点。

MultiLineString

MULTILINESTRING

multilinestring

一组独立的线串。

MultiPolygon

MULTIPOLYGON

multipolygon

一组独立的多边形。

GeometryCollection

GEOMETRYCOLLECTION

geometrycollection

类似于 multi* 形状的 GeoJSON 形状,不同之处在于可以同时存在多种类型(例如,点和线串)。

N/A

BBOX

envelope

通过仅指定左上角和右下角的点来指定的边界矩形或包络线。

对于所有类型,都需要内部 typecoordinates 字段。

点是单个地理坐标,例如建筑物的位置或智能手机的地理位置 API 给出的当前位置。以下是 GeoJSON 中点的示例。

resp = client.index(
    index="example",
    document={
        "location": {
            "type": "Point",
            "coordinates": [
                -77.03653,
                38.897676
            ]
        }
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: {
      type: 'Point',
      coordinates: [
        -77.03653,
        38.897676
      ]
    }
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location: {
      type: "Point",
      coordinates: [-77.03653, 38.897676],
    },
  },
});
console.log(response);
POST /example/_doc
{
  "location" : {
    "type" : "Point",
    "coordinates" : [-77.03653, 38.897676]
  }
}

以下是 WKT 中点的示例

resp = client.index(
    index="example",
    document={
        "location": "POINT (-77.03653 38.897676)"
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: 'POINT (-77.03653 38.897676)'
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location: "POINT (-77.03653 38.897676)",
  },
});
console.log(response);
POST /example/_doc
{
  "location" : "POINT (-77.03653 38.897676)"
}

由两个或多个位置的数组定义的线串。通过仅指定两个点,线串将表示一条直线。指定两个以上的点会创建任意路径。以下是 GeoJSON 中线串的示例。

resp = client.index(
    index="example",
    document={
        "location": {
            "type": "LineString",
            "coordinates": [
                [
                    -77.03653,
                    38.897676
                ],
                [
                    -77.009051,
                    38.889939
                ]
            ]
        }
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: {
      type: 'LineString',
      coordinates: [
        [
          -77.03653,
          38.897676
        ],
        [
          -77.009051,
          38.889939
        ]
      ]
    }
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location: {
      type: "LineString",
      coordinates: [
        [-77.03653, 38.897676],
        [-77.009051, 38.889939],
      ],
    },
  },
});
console.log(response);
POST /example/_doc
{
  "location" : {
    "type" : "LineString",
    "coordinates" : [[-77.03653, 38.897676], [-77.009051, 38.889939]]
  }
}

以下是 WKT 中线串的示例

resp = client.index(
    index="example",
    document={
        "location": "LINESTRING (-77.03653 38.897676, -77.009051 38.889939)"
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: 'LINESTRING (-77.03653 38.897676, -77.009051 38.889939)'
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location: "LINESTRING (-77.03653 38.897676, -77.009051 38.889939)",
  },
});
console.log(response);
POST /example/_doc
{
  "location" : "LINESTRING (-77.03653 38.897676, -77.009051 38.889939)"
}

上面的线串将绘制一条从白宫到美国国会大厦的直线。

多边形由点的列表的列表定义。每个(外部)列表中的第一个和最后一个点必须相同(多边形必须闭合)。以下是 GeoJSON 中多边形的示例。

resp = client.index(
    index="example",
    document={
        "location": {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        100,
                        0
                    ],
                    [
                        101,
                        0
                    ],
                    [
                        101,
                        1
                    ],
                    [
                        100,
                        1
                    ],
                    [
                        100,
                        0
                    ]
                ]
            ]
        }
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: {
      type: 'Polygon',
      coordinates: [
        [
          [
            100,
            0
          ],
          [
            101,
            0
          ],
          [
            101,
            1
          ],
          [
            100,
            1
          ],
          [
            100,
            0
          ]
        ]
      ]
    }
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location: {
      type: "Polygon",
      coordinates: [
        [
          [100, 0],
          [101, 0],
          [101, 1],
          [100, 1],
          [100, 0],
        ],
      ],
    },
  },
});
console.log(response);
POST /example/_doc
{
  "location" : {
    "type" : "Polygon",
    "coordinates" : [
      [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
    ]
  }
}

以下是 WKT 中多边形的示例

resp = client.index(
    index="example",
    document={
        "location": "POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0))"
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: 'POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0))'
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location:
      "POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0))",
  },
});
console.log(response);
POST /example/_doc
{
  "location" : "POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0))"
}

第一个数组表示多边形的外边界,其他数组表示内部形状(“孔”)。以下是一个带有孔的多边形的 GeoJSON 示例

resp = client.index(
    index="example",
    document={
        "location": {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        100,
                        0
                    ],
                    [
                        101,
                        0
                    ],
                    [
                        101,
                        1
                    ],
                    [
                        100,
                        1
                    ],
                    [
                        100,
                        0
                    ]
                ],
                [
                    [
                        100.2,
                        0.2
                    ],
                    [
                        100.8,
                        0.2
                    ],
                    [
                        100.8,
                        0.8
                    ],
                    [
                        100.2,
                        0.8
                    ],
                    [
                        100.2,
                        0.2
                    ]
                ]
            ]
        }
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: {
      type: 'Polygon',
      coordinates: [
        [
          [
            100,
            0
          ],
          [
            101,
            0
          ],
          [
            101,
            1
          ],
          [
            100,
            1
          ],
          [
            100,
            0
          ]
        ],
        [
          [
            100.2,
            0.2
          ],
          [
            100.8,
            0.2
          ],
          [
            100.8,
            0.8
          ],
          [
            100.2,
            0.8
          ],
          [
            100.2,
            0.2
          ]
        ]
      ]
    }
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location: {
      type: "Polygon",
      coordinates: [
        [
          [100, 0],
          [101, 0],
          [101, 1],
          [100, 1],
          [100, 0],
        ],
        [
          [100.2, 0.2],
          [100.8, 0.2],
          [100.8, 0.8],
          [100.2, 0.8],
          [100.2, 0.2],
        ],
      ],
    },
  },
});
console.log(response);
POST /example/_doc
{
  "location" : {
    "type" : "Polygon",
    "coordinates" : [
      [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ],
      [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]
    ]
  }
}

以下是 WKT 中带孔的多边形的示例

resp = client.index(
    index="example",
    document={
        "location": "POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2))"
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: 'POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2))'
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location:
      "POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2))",
  },
});
console.log(response);
POST /example/_doc
{
  "location" : "POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2))"
}
多边形方向
编辑

多边形的方向表示其顶点的顺序:RIGHT(逆时针)或 LEFT(顺时针)。Elasticsearch 使用多边形的方向来确定它是否跨越国际日期变更线(+/-180° 经度)。

您可以使用 orientation 映射参数为 WKT 多边形设置默认方向。这是因为 WKT 规范没有指定或强制执行默认方向。

GeoJSON 多边形使用 RIGHT 的默认方向,而与 orientation 映射参数的值无关。这是因为 GeoJSON 规范强制要求外部多边形使用逆时针方向,而内部形状使用顺时针方向。

您可以使用文档级的 orientation 参数覆盖 GeoJSON 多边形的默认方向。例如,以下索引请求指定 orientation 的文档级值为 LEFT

resp = client.index(
    index="example",
    document={
        "location": {
            "type": "Polygon",
            "orientation": "LEFT",
            "coordinates": [
                [
                    [
                        -177,
                        10
                    ],
                    [
                        176,
                        15
                    ],
                    [
                        172,
                        0
                    ],
                    [
                        176,
                        -15
                    ],
                    [
                        -177,
                        -10
                    ],
                    [
                        -177,
                        10
                    ]
                ]
            ]
        }
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: {
      type: 'Polygon',
      orientation: 'LEFT',
      coordinates: [
        [
          [
            -177,
            10
          ],
          [
            176,
            15
          ],
          [
            172,
            0
          ],
          [
            176,
            -15
          ],
          [
            -177,
            -10
          ],
          [
            -177,
            10
          ]
        ]
      ]
    }
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location: {
      type: "Polygon",
      orientation: "LEFT",
      coordinates: [
        [
          [-177, 10],
          [176, 15],
          [172, 0],
          [176, -15],
          [-177, -10],
          [-177, 10],
        ],
      ],
    },
  },
});
console.log(response);
POST /example/_doc
{
  "location" : {
    "type" : "Polygon",
    "orientation" : "LEFT",
    "coordinates" : [
      [ [-177.0, 10.0], [176.0, 15.0], [172.0, 0.0], [176.0, -15.0], [-177.0, -10.0], [-177.0, 10.0] ]
    ]
  }
}

Elasticsearch 仅使用多边形的方向来确定它是否跨越国际日期变更线。如果多边形的最小经度与最大经度之间的差小于 180°,则多边形不会跨越日期变更线,并且其方向无效。

如果多边形的最小经度与最大经度之间的差为 180° 或更大,则 Elasticsearch 会检查多边形的文档级 orientation 是否与默认方向不同。如果方向不同,则 Elasticsearch 会认为多边形跨越国际日期变更线,并在日期变更线处分割多边形。

以下是 GeoJSON 点列表的示例

resp = client.index(
    index="example",
    document={
        "location": {
            "type": "MultiPoint",
            "coordinates": [
                [
                    102,
                    2
                ],
                [
                    103,
                    2
                ]
            ]
        }
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: {
      type: 'MultiPoint',
      coordinates: [
        [
          102,
          2
        ],
        [
          103,
          2
        ]
      ]
    }
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location: {
      type: "MultiPoint",
      coordinates: [
        [102, 2],
        [103, 2],
      ],
    },
  },
});
console.log(response);
POST /example/_doc
{
  "location" : {
    "type" : "MultiPoint",
    "coordinates" : [
      [102.0, 2.0], [103.0, 2.0]
    ]
  }
}

以下是 WKT 点列表的示例

resp = client.index(
    index="example",
    document={
        "location": "MULTIPOINT (102.0 2.0, 103.0 2.0)"
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: 'MULTIPOINT (102.0 2.0, 103.0 2.0)'
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location: "MULTIPOINT (102.0 2.0, 103.0 2.0)",
  },
});
console.log(response);
POST /example/_doc
{
  "location" : "MULTIPOINT (102.0 2.0, 103.0 2.0)"
}

以下是 GeoJSON 线串列表的示例

resp = client.index(
    index="example",
    document={
        "location": {
            "type": "MultiLineString",
            "coordinates": [
                [
                    [
                        102,
                        2
                    ],
                    [
                        103,
                        2
                    ],
                    [
                        103,
                        3
                    ],
                    [
                        102,
                        3
                    ]
                ],
                [
                    [
                        100,
                        0
                    ],
                    [
                        101,
                        0
                    ],
                    [
                        101,
                        1
                    ],
                    [
                        100,
                        1
                    ]
                ],
                [
                    [
                        100.2,
                        0.2
                    ],
                    [
                        100.8,
                        0.2
                    ],
                    [
                        100.8,
                        0.8
                    ],
                    [
                        100.2,
                        0.8
                    ]
                ]
            ]
        }
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: {
      type: 'MultiLineString',
      coordinates: [
        [
          [
            102,
            2
          ],
          [
            103,
            2
          ],
          [
            103,
            3
          ],
          [
            102,
            3
          ]
        ],
        [
          [
            100,
            0
          ],
          [
            101,
            0
          ],
          [
            101,
            1
          ],
          [
            100,
            1
          ]
        ],
        [
          [
            100.2,
            0.2
          ],
          [
            100.8,
            0.2
          ],
          [
            100.8,
            0.8
          ],
          [
            100.2,
            0.8
          ]
        ]
      ]
    }
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location: {
      type: "MultiLineString",
      coordinates: [
        [
          [102, 2],
          [103, 2],
          [103, 3],
          [102, 3],
        ],
        [
          [100, 0],
          [101, 0],
          [101, 1],
          [100, 1],
        ],
        [
          [100.2, 0.2],
          [100.8, 0.2],
          [100.8, 0.8],
          [100.2, 0.8],
        ],
      ],
    },
  },
});
console.log(response);
POST /example/_doc
{
  "location" : {
    "type" : "MultiLineString",
    "coordinates" : [
      [ [102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0] ],
      [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0] ],
      [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8] ]
    ]
  }
}

以下是 WKT 线串列表的示例

resp = client.index(
    index="example",
    document={
        "location": "MULTILINESTRING ((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0), (100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8))"
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: 'MULTILINESTRING ((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0), (100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8))'
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location:
      "MULTILINESTRING ((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0), (100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8))",
  },
});
console.log(response);
POST /example/_doc
{
  "location" : "MULTILINESTRING ((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0), (100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8))"
}

以下是 GeoJSON 多边形列表的示例(第二个多边形包含一个孔)

resp = client.index(
    index="example",
    document={
        "location": {
            "type": "MultiPolygon",
            "coordinates": [
                [
                    [
                        [
                            102,
                            2
                        ],
                        [
                            103,
                            2
                        ],
                        [
                            103,
                            3
                        ],
                        [
                            102,
                            3
                        ],
                        [
                            102,
                            2
                        ]
                    ]
                ],
                [
                    [
                        [
                            100,
                            0
                        ],
                        [
                            101,
                            0
                        ],
                        [
                            101,
                            1
                        ],
                        [
                            100,
                            1
                        ],
                        [
                            100,
                            0
                        ]
                    ],
                    [
                        [
                            100.2,
                            0.2
                        ],
                        [
                            100.8,
                            0.2
                        ],
                        [
                            100.8,
                            0.8
                        ],
                        [
                            100.2,
                            0.8
                        ],
                        [
                            100.2,
                            0.2
                        ]
                    ]
                ]
            ]
        }
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: {
      type: 'MultiPolygon',
      coordinates: [
        [
          [
            [
              102,
              2
            ],
            [
              103,
              2
            ],
            [
              103,
              3
            ],
            [
              102,
              3
            ],
            [
              102,
              2
            ]
          ]
        ],
        [
          [
            [
              100,
              0
            ],
            [
              101,
              0
            ],
            [
              101,
              1
            ],
            [
              100,
              1
            ],
            [
              100,
              0
            ]
          ],
          [
            [
              100.2,
              0.2
            ],
            [
              100.8,
              0.2
            ],
            [
              100.8,
              0.8
            ],
            [
              100.2,
              0.8
            ],
            [
              100.2,
              0.2
            ]
          ]
        ]
      ]
    }
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location: {
      type: "MultiPolygon",
      coordinates: [
        [
          [
            [102, 2],
            [103, 2],
            [103, 3],
            [102, 3],
            [102, 2],
          ],
        ],
        [
          [
            [100, 0],
            [101, 0],
            [101, 1],
            [100, 1],
            [100, 0],
          ],
          [
            [100.2, 0.2],
            [100.8, 0.2],
            [100.8, 0.8],
            [100.2, 0.8],
            [100.2, 0.2],
          ],
        ],
      ],
    },
  },
});
console.log(response);
POST /example/_doc
{
  "location" : {
    "type" : "MultiPolygon",
    "coordinates" : [
      [ [[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]] ],
      [ [[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],
        [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]] ]
    ]
  }
}

以下是 WKT 多边形列表的示例(第二个多边形包含一个孔)

resp = client.index(
    index="example",
    document={
        "location": "MULTIPOLYGON (((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0, 102.0 2.0)), ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2)))"
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: 'MULTIPOLYGON (((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0, 102.0 2.0)), ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2)))'
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location:
      "MULTIPOLYGON (((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0, 102.0 2.0)), ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2)))",
  },
});
console.log(response);
POST /example/_doc
{
  "location" : "MULTIPOLYGON (((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0, 102.0 2.0)), ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2)))"
}

以下是 GeoJSON 几何对象集合的示例

resp = client.index(
    index="example",
    document={
        "location": {
            "type": "GeometryCollection",
            "geometries": [
                {
                    "type": "Point",
                    "coordinates": [
                        100,
                        0
                    ]
                },
                {
                    "type": "LineString",
                    "coordinates": [
                        [
                            101,
                            0
                        ],
                        [
                            102,
                            1
                        ]
                    ]
                }
            ]
        }
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: {
      type: 'GeometryCollection',
      geometries: [
        {
          type: 'Point',
          coordinates: [
            100,
            0
          ]
        },
        {
          type: 'LineString',
          coordinates: [
            [
              101,
              0
            ],
            [
              102,
              1
            ]
          ]
        }
      ]
    }
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location: {
      type: "GeometryCollection",
      geometries: [
        {
          type: "Point",
          coordinates: [100, 0],
        },
        {
          type: "LineString",
          coordinates: [
            [101, 0],
            [102, 1],
          ],
        },
      ],
    },
  },
});
console.log(response);
POST /example/_doc
{
  "location" : {
    "type": "GeometryCollection",
    "geometries": [
      {
        "type": "Point",
        "coordinates": [100.0, 0.0]
      },
      {
        "type": "LineString",
        "coordinates": [ [101.0, 0.0], [102.0, 1.0] ]
      }
    ]
  }
}

以下是 WKT 几何对象集合的示例

resp = client.index(
    index="example",
    document={
        "location": "GEOMETRYCOLLECTION (POINT (100.0 0.0), LINESTRING (101.0 0.0, 102.0 1.0))"
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: 'GEOMETRYCOLLECTION (POINT (100.0 0.0), LINESTRING (101.0 0.0, 102.0 1.0))'
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location:
      "GEOMETRYCOLLECTION (POINT (100.0 0.0), LINESTRING (101.0 0.0, 102.0 1.0))",
  },
});
console.log(response);
POST /example/_doc
{
  "location" : "GEOMETRYCOLLECTION (POINT (100.0 0.0), LINESTRING (101.0 0.0, 102.0 1.0))"
}
包络线
编辑

Elasticsearch 支持 envelope 类型,该类型由形状的左上角和右下角点的坐标组成,以 [[minLon, maxLat], [maxLon, minLat]] 格式表示边界矩形

resp = client.index(
    index="example",
    document={
        "location": {
            "type": "envelope",
            "coordinates": [
                [
                    100,
                    1
                ],
                [
                    101,
                    0
                ]
            ]
        }
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: {
      type: 'envelope',
      coordinates: [
        [
          100,
          1
        ],
        [
          101,
          0
        ]
      ]
    }
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location: {
      type: "envelope",
      coordinates: [
        [100, 1],
        [101, 0],
      ],
    },
  },
});
console.log(response);
POST /example/_doc
{
  "location" : {
    "type" : "envelope",
    "coordinates" : [ [100.0, 1.0], [101.0, 0.0] ]
  }
}

以下是使用 WKT BBOX 格式的包络线的示例

注意: WKT 规范期望以下顺序:minLon、maxLon、maxLat、minLat。

resp = client.index(
    index="example",
    document={
        "location": "BBOX (100.0, 102.0, 2.0, 0.0)"
    },
)
print(resp)
response = client.index(
  index: 'example',
  body: {
    location: 'BBOX (100.0, 102.0, 2.0, 0.0)'
  }
)
puts response
const response = await client.index({
  index: "example",
  document: {
    location: "BBOX (100.0, 102.0, 2.0, 0.0)",
  },
});
console.log(response);
POST /example/_doc
{
  "location" : "BBOX (100.0, 102.0, 2.0, 0.0)"
}
圆形
编辑

GeoJSON 和 WKT 都不支持点半径圆形类型。而是使用 圆形摄取处理器将圆形近似为 polygon

排序和检索索引形状

编辑

由于形状的复杂输入结构和索引表示,目前无法对形状进行排序或直接检索其字段。geo_shape 值仅可通过 _source 字段检索。

合成源

编辑

合成 _source 仅对 TSDB 索引(index.mode 设置为 time_series 的索引)正式可用。对于其他索引,合成 _source 处于技术预览状态。技术预览中的功能可能会在未来的版本中更改或删除。Elastic 将努力解决任何问题,但技术预览中的功能不受官方 GA 功能的支持 SLA 的约束。