形状查询
编辑形状查询
编辑查询包含使用 shape
类型索引的字段的文档。
需要 shape
映射。
此查询支持两种定义目标形状的方式:提供完整的形状定义,或者引用在另一个索引中预先索引的形状的名称或 ID。以下定义了两种格式,并附有示例。
内联形状定义
编辑与 geo_shape
查询类似,shape
查询使用 GeoJSON 或 Well Known Text (WKT) 来表示形状。
给定以下索引
resp = client.indices.create( index="example", mappings={ "properties": { "geometry": { "type": "shape" } } }, ) print(resp) resp1 = client.index( index="example", id="1", refresh="wait_for", document={ "name": "Lucky Landing", "geometry": { "type": "point", "coordinates": [ 1355.400544, 5255.530286 ] } }, ) print(resp1)
response = client.indices.create( index: 'example', body: { mappings: { properties: { geometry: { type: 'shape' } } } } ) puts response response = client.index( index: 'example', id: 1, refresh: 'wait_for', body: { name: 'Lucky Landing', geometry: { type: 'point', coordinates: [ 1355.400544, 5255.530286 ] } } ) puts response
const response = await client.indices.create({ index: "example", mappings: { properties: { geometry: { type: "shape", }, }, }, }); console.log(response); const response1 = await client.index({ index: "example", id: 1, refresh: "wait_for", document: { name: "Lucky Landing", geometry: { type: "point", coordinates: [1355.400544, 5255.530286], }, }, }); console.log(response1);
PUT /example { "mappings": { "properties": { "geometry": { "type": "shape" } } } } PUT /example/_doc/1?refresh=wait_for { "name": "Lucky Landing", "geometry": { "type": "point", "coordinates": [ 1355.400544, 5255.530286 ] } }
以下查询将使用 Elasticsearch 的 envelope
GeoJSON 扩展来查找点
resp = client.search( index="example", query={ "shape": { "geometry": { "shape": { "type": "envelope", "coordinates": [ [ 1355, 5355 ], [ 1400, 5200 ] ] }, "relation": "within" } } }, ) print(resp)
const response = await client.search({ index: "example", query: { shape: { geometry: { shape: { type: "envelope", coordinates: [ [1355, 5355], [1400, 5200], ], }, relation: "within", }, }, }, }); console.log(response);
GET /example/_search { "query": { "shape": { "geometry": { "shape": { "type": "envelope", "coordinates": [ [ 1355.0, 5355.0 ], [ 1400.0, 5200.0 ] ] }, "relation": "within" } } } }
预索引形状
编辑该查询还支持使用已在另一个索引中索引的形状。当您有一个预定义的形状列表,这些形状对您的应用程序很有用,并且您希望使用逻辑名称(例如 *新西兰*)来引用它,而不是每次都必须提供它们的坐标时,这尤其有用。在这种情况下,只需要提供
-
id
- 包含预索引形状的文档的 ID。 -
index
- 预索引形状所在的索引的名称。默认为 *shapes*。 -
path
- 指定为包含预索引形状的路径的字段。默认为 *shape*。 -
routing
- 如果需要,形状文档的路由。
以下是使用带有预索引形状的过滤器的示例
resp = client.indices.create( index="shapes", mappings={ "properties": { "geometry": { "type": "shape" } } }, ) print(resp) resp1 = client.index( index="shapes", id="footprint", document={ "geometry": { "type": "envelope", "coordinates": [ [ 1355, 5355 ], [ 1400, 5200 ] ] } }, ) print(resp1) resp2 = client.search( index="example", query={ "shape": { "geometry": { "indexed_shape": { "index": "shapes", "id": "footprint", "path": "geometry" } } } }, ) print(resp2)
response = client.indices.create( index: 'shapes', body: { mappings: { properties: { geometry: { type: 'shape' } } } } ) puts response response = client.index( index: 'shapes', id: 'footprint', body: { geometry: { type: 'envelope', coordinates: [ [ 1355, 5355 ], [ 1400, 5200 ] ] } } ) puts response response = client.search( index: 'example', body: { query: { shape: { geometry: { indexed_shape: { index: 'shapes', id: 'footprint', path: 'geometry' } } } } } ) puts response
const response = await client.indices.create({ index: "shapes", mappings: { properties: { geometry: { type: "shape", }, }, }, }); console.log(response); const response1 = await client.index({ index: "shapes", id: "footprint", document: { geometry: { type: "envelope", coordinates: [ [1355, 5355], [1400, 5200], ], }, }, }); console.log(response1); const response2 = await client.search({ index: "example", query: { shape: { geometry: { indexed_shape: { index: "shapes", id: "footprint", path: "geometry", }, }, }, }, }); console.log(response2);
PUT /shapes { "mappings": { "properties": { "geometry": { "type": "shape" } } } } PUT /shapes/_doc/footprint { "geometry": { "type": "envelope", "coordinates": [ [ 1355.0, 5355.0 ], [ 1400.0, 5200.0 ] ] } } GET /example/_search { "query": { "shape": { "geometry": { "indexed_shape": { "index": "shapes", "id": "footprint", "path": "geometry" } } } } }
空间关系
编辑以下是可用的空间关系运算符的完整列表
-
INTERSECTS
- (默认) 返回shape
字段与查询几何图形相交的所有文档。 -
DISJOINT
- 返回shape
字段与查询几何图形没有任何共同之处的所有文档。 -
WITHIN
- 返回shape
字段在查询几何图形内的所有文档。 -
CONTAINS
- 返回shape
字段包含查询几何图形的所有文档。
忽略未映射
编辑当设置为 true
时,ignore_unmapped
选项将忽略未映射的字段,并且不会匹配此查询的任何文档。这在查询可能具有不同映射的多个索引时很有用。当设置为 false
(默认值)时,如果该字段未映射,查询将抛出异常。