地理形状查询

编辑

过滤使用 geo_shapegeo_point 类型索引的文档。

geo_shape 查询使用与 geo_shapegeo_point 映射相同的索引,以查找具有与查询形状相关的形状的文档,使用指定的空间关系:相交、包含、在内或不相交。

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

内联形状定义

编辑

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

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

resp = client.indices.create(
    index="example",
    mappings={
        "properties": {
            "location": {
                "type": "geo_shape"
            }
        }
    },
)
print(resp)

resp1 = client.index(
    index="example",
    refresh=True,
    document={
        "name": "Wind & Wetter, Berlin, Germany",
        "location": {
            "type": "point",
            "coordinates": [
                13.400544,
                52.530286
            ]
        }
    },
)
print(resp1)
response = client.indices.create(
  index: 'example',
  body: {
    mappings: {
      properties: {
        location: {
          type: 'geo_shape'
        }
      }
    }
  }
)
puts response

response = client.index(
  index: 'example',
  refresh: true,
  body: {
    name: 'Wind & Wetter, Berlin, Germany',
    location: {
      type: 'point',
      coordinates: [
        13.400544,
        52.530286
      ]
    }
  }
)
puts response
const response = await client.indices.create({
  index: "example",
  mappings: {
    properties: {
      location: {
        type: "geo_shape",
      },
    },
  },
});
console.log(response);

const response1 = await client.index({
  index: "example",
  refresh: "true",
  document: {
    name: "Wind & Wetter, Berlin, Germany",
    location: {
      type: "point",
      coordinates: [13.400544, 52.530286],
    },
  },
});
console.log(response1);
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 扩展查找该点

resp = client.search(
    index="example",
    query={
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "geo_shape": {
                    "location": {
                        "shape": {
                            "type": "envelope",
                            "coordinates": [
                                [
                                    13,
                                    53
                                ],
                                [
                                    14,
                                    52
                                ]
                            ]
                        },
                        "relation": "within"
                    }
                }
            }
        }
    },
)
print(resp)
response = client.search(
  index: 'example',
  body: {
    query: {
      bool: {
        must: {
          match_all: {}
        },
        filter: {
          geo_shape: {
            location: {
              shape: {
                type: 'envelope',
                coordinates: [
                  [
                    13,
                    53
                  ],
                  [
                    14,
                    52
                  ]
                ]
              },
              relation: 'within'
            }
          }
        }
      }
    }
  }
)
puts response
const response = await client.search({
  index: "example",
  query: {
    bool: {
      must: {
        match_all: {},
      },
      filter: {
        geo_shape: {
          location: {
            shape: {
              type: "envelope",
              coordinates: [
                [13, 53],
                [14, 52],
              ],
            },
            relation: "within",
          },
        },
      },
    },
  },
});
console.log(response);
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 字段上进行查询。

resp = client.indices.create(
    index="example_points",
    mappings={
        "properties": {
            "location": {
                "type": "geo_point"
            }
        }
    },
)
print(resp)

resp1 = client.index(
    index="example_points",
    id="1",
    refresh=True,
    document={
        "name": "Wind & Wetter, Berlin, Germany",
        "location": [
            13.400544,
            52.530286
        ]
    },
)
print(resp1)
response = client.indices.create(
  index: 'example_points',
  body: {
    mappings: {
      properties: {
        location: {
          type: 'geo_point'
        }
      }
    }
  }
)
puts response

response = client.index(
  index: 'example_points',
  id: 1,
  refresh: true,
  body: {
    name: 'Wind & Wetter, Berlin, Germany',
    location: [
      13.400544,
      52.530286
    ]
  }
)
puts response
const response = await client.indices.create({
  index: "example_points",
  mappings: {
    properties: {
      location: {
        type: "geo_point",
      },
    },
  },
});
console.log(response);

const response1 = await client.index({
  index: "example_points",
  id: 1,
  refresh: "true",
  document: {
    name: "Wind & Wetter, Berlin, Germany",
    location: [13.400544, 52.530286],
  },
});
console.log(response1);
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 字段的文档。

resp = client.search(
    index="example_points",
    query={
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "geo_shape": {
                    "location": {
                        "shape": {
                            "type": "envelope",
                            "coordinates": [
                                [
                                    13,
                                    53
                                ],
                                [
                                    14,
                                    52
                                ]
                            ]
                        },
                        "relation": "intersects"
                    }
                }
            }
        }
    },
)
print(resp)
response = client.search(
  index: 'example_points',
  body: {
    query: {
      bool: {
        must: {
          match_all: {}
        },
        filter: {
          geo_shape: {
            location: {
              shape: {
                type: 'envelope',
                coordinates: [
                  [
                    13,
                    53
                  ],
                  [
                    14,
                    52
                  ]
                ]
              },
              relation: 'intersects'
            }
          }
        }
      }
    }
  }
)
puts response
const response = await client.search({
  index: "example_points",
  query: {
    bool: {
      must: {
        match_all: {},
      },
      filter: {
        geo_shape: {
          location: {
            shape: {
              type: "envelope",
              coordinates: [
                [13, 53],
                [14, 52],
              ],
            },
            relation: "intersects",
          },
        },
      },
    },
  },
});
console.log(response);
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",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name": "Wind & Wetter, Berlin, Germany",
          "location": [13.400544, 52.530286]
        }
      }
    ]
  }
}

预索引形状

编辑

该查询还支持使用已在另一个索引中索引的形状。当您有一个预定义的形状列表,并且想要使用逻辑名称(例如新西兰)来引用该列表,而不是每次都提供坐标时,这尤其有用。在这种情况下,只需要提供

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

以下是使用预索引形状进行过滤的示例

resp = client.indices.create(
    index="shapes",
    mappings={
        "properties": {
            "location": {
                "type": "geo_shape"
            }
        }
    },
)
print(resp)

resp1 = client.index(
    index="shapes",
    id="deu",
    document={
        "location": {
            "type": "envelope",
            "coordinates": [
                [
                    13,
                    53
                ],
                [
                    14,
                    52
                ]
            ]
        }
    },
)
print(resp1)

resp2 = client.search(
    index="example",
    query={
        "bool": {
            "filter": {
                "geo_shape": {
                    "location": {
                        "indexed_shape": {
                            "index": "shapes",
                            "id": "deu",
                            "path": "location"
                        }
                    }
                }
            }
        }
    },
)
print(resp2)
response = client.indices.create(
  index: 'shapes',
  body: {
    mappings: {
      properties: {
        location: {
          type: 'geo_shape'
        }
      }
    }
  }
)
puts response

response = client.index(
  index: 'shapes',
  id: 'deu',
  body: {
    location: {
      type: 'envelope',
      coordinates: [
        [
          13,
          53
        ],
        [
          14,
          52
        ]
      ]
    }
  }
)
puts response

response = client.search(
  index: 'example',
  body: {
    query: {
      bool: {
        filter: {
          geo_shape: {
            location: {
              indexed_shape: {
                index: 'shapes',
                id: 'deu',
                path: 'location'
              }
            }
          }
        }
      }
    }
  }
)
puts response
const response = await client.indices.create({
  index: "shapes",
  mappings: {
    properties: {
      location: {
        type: "geo_shape",
      },
    },
  },
});
console.log(response);

const response1 = await client.index({
  index: "shapes",
  id: "deu",
  document: {
    location: {
      type: "envelope",
      coordinates: [
        [13, 53],
        [14, 52],
      ],
    },
  },
});
console.log(response1);

const response2 = await client.search({
  index: "example",
  query: {
    bool: {
      filter: {
        geo_shape: {
          location: {
            indexed_shape: {
              index: "shapes",
              id: "deu",
              path: "location",
            },
          },
        },
      },
    },
  },
});
console.log(response2);
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"
            }
          }
        }
      }
    }
  }
}

空间关系

编辑

以下是搜索地理字段时可用的空间关系运算符的完整列表

  • INTERSECTS - (默认)返回所有 geo_shapegeo_point 字段与查询几何形状相交的文档。
  • DISJOINT - 返回所有 geo_shapegeo_point 字段与查询几何形状没有任何共同之处的文档。
  • WITHIN - 返回所有 geo_shapegeo_point 字段在查询几何形状内的文档。不支持线几何形状。
  • CONTAINS - 返回所有 geo_shapegeo_point 字段包含查询几何形状的文档。

忽略未映射

编辑

当设置为 true 时,ignore_unmapped 选项将忽略未映射的字段,并且不会匹配此查询的任何文档。当查询可能具有不同映射的多个索引时,这非常有用。当设置为 false (默认值)时,如果该字段未映射,查询将抛出异常。

注意事项

编辑
  • 当数据在 geo_shape 字段中索引为形状数组时,这些数组被视为一个形状。因此,以下请求是等效的。
resp = client.index(
    index="test",
    id="1",
    document={
        "location": [
            {
                "coordinates": [
                    46.25,
                    20.14
                ],
                "type": "point"
            },
            {
                "coordinates": [
                    47.49,
                    19.04
                ],
                "type": "point"
            }
        ]
    },
)
print(resp)
response = client.index(
  index: 'test',
  id: 1,
  body: {
    location: [
      {
        coordinates: [
          46.25,
          20.14
        ],
        type: 'point'
      },
      {
        coordinates: [
          47.49,
          19.04
        ],
        type: 'point'
      }
    ]
  }
)
puts response
const response = await client.index({
  index: "test",
  id: 1,
  document: {
    location: [
      {
        coordinates: [46.25, 20.14],
        type: "point",
      },
      {
        coordinates: [47.49, 19.04],
        type: "point",
      },
    ],
  },
});
console.log(response);
PUT /test/_doc/1
{
  "location": [
    {
      "coordinates": [46.25,20.14],
      "type": "point"
    },
    {
      "coordinates": [47.49,19.04],
      "type": "point"
    }
  ]
}
resp = client.index(
    index="test",
    id="1",
    document={
        "location": {
            "coordinates": [
                [
                    46.25,
                    20.14
                ],
                [
                    47.49,
                    19.04
                ]
            ],
            "type": "multipoint"
        }
    },
)
print(resp)
response = client.index(
  index: 'test',
  id: 1,
  body: {
    location: {
      coordinates: [
        [
          46.25,
          20.14
        ],
        [
          47.49,
          19.04
        ]
      ],
      type: 'multipoint'
    }
  }
)
puts response
const response = await client.index({
  index: "test",
  id: 1,
  document: {
    location: {
      coordinates: [
        [46.25, 20.14],
        [47.49, 19.04],
      ],
      type: "multipoint",
    },
  },
});
console.log(response);
PUT /test/_doc/1
{
  "location":
    {
      "coordinates": [[46.25,20.14],[47.49,19.04]],
      "type": "multipoint"
    }
}
  • geo_shape 查询假定 geo_shape 字段使用默认的 orientationRIGHT(逆时针)。请参阅多边形方向