地理多边形查询

编辑

在 7.12 版中已弃用。

请改用 Geoshape,其中多边形以 GeoJSON 或 Well-Known Text (WKT) 定义。

返回仅落在点多边形内的匹配结果的查询。以下是一个示例

resp = client.search(
    query={
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "geo_polygon": {
                    "person.location": {
                        "points": [
                            {
                                "lat": 40,
                                "lon": -70
                            },
                            {
                                "lat": 30,
                                "lon": -80
                            },
                            {
                                "lat": 20,
                                "lon": -90
                            }
                        ]
                    }
                }
            }
        }
    },
)
print(resp)
const response = await client.search({
  query: {
    bool: {
      must: {
        match_all: {},
      },
      filter: {
        geo_polygon: {
          "person.location": {
            points: [
              {
                lat: 40,
                lon: -70,
              },
              {
                lat: 30,
                lon: -80,
              },
              {
                lat: 20,
                lon: -90,
              },
            ],
          },
        },
      },
    },
  },
});
console.log(response);
GET /_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_polygon": {
          "person.location": {
            "points": [
              { "lat": 40, "lon": -70 },
              { "lat": 30, "lon": -80 },
              { "lat": 20, "lon": -90 }
            ]
          }
        }
      }
    }
  }
}

查询选项

编辑
选项 描述

_name

用于识别过滤器的可选名称字段

validation_method

设置为 IGNORE_MALFORMED 以接受纬度或经度无效的地理点,设置为 COERCE 以尝试推断正确的纬度或经度,或设置为 STRICT(默认为 STRICT)。

允许的格式

编辑
经纬度作为数组
编辑

格式为 [lon, lat]

注意:此处经度/纬度的顺序必须符合 GeoJSON

resp = client.search(
    query={
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "geo_polygon": {
                    "person.location": {
                        "points": [
                            [
                                -70,
                                40
                            ],
                            [
                                -80,
                                30
                            ],
                            [
                                -90,
                                20
                            ]
                        ]
                    }
                }
            }
        }
    },
)
print(resp)
const response = await client.search({
  query: {
    bool: {
      must: {
        match_all: {},
      },
      filter: {
        geo_polygon: {
          "person.location": {
            points: [
              [-70, 40],
              [-80, 30],
              [-90, 20],
            ],
          },
        },
      },
    },
  },
});
console.log(response);
GET /_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_polygon": {
          "person.location": {
            "points": [
              [ -70, 40 ],
              [ -80, 30 ],
              [ -90, 20 ]
            ]
          }
        }
      }
    }
  }
}
经纬度作为字符串
编辑

格式为 lat,lon

resp = client.search(
    query={
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "geo_polygon": {
                    "person.location": {
                        "points": [
                            "40, -70",
                            "30, -80",
                            "20, -90"
                        ]
                    }
                }
            }
        }
    },
)
print(resp)
const response = await client.search({
  query: {
    bool: {
      must: {
        match_all: {},
      },
      filter: {
        geo_polygon: {
          "person.location": {
            points: ["40, -70", "30, -80", "20, -90"],
          },
        },
      },
    },
  },
});
console.log(response);
GET /_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_polygon": {
          "person.location": {
            "points": [
              "40, -70",
              "30, -80",
              "20, -90"
            ]
          }
        }
      }
    }
  }
}
Geohash
编辑
resp = client.search(
    query={
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "geo_polygon": {
                    "person.location": {
                        "points": [
                            "drn5x1g8cu2y",
                            "30, -80",
                            "20, -90"
                        ]
                    }
                }
            }
        }
    },
)
print(resp)
const response = await client.search({
  query: {
    bool: {
      must: {
        match_all: {},
      },
      filter: {
        geo_polygon: {
          "person.location": {
            points: ["drn5x1g8cu2y", "30, -80", "20, -90"],
          },
        },
      },
    },
  },
});
console.log(response);
GET /_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_polygon": {
          "person.location": {
            "points": [
              "drn5x1g8cu2y",
              "30, -80",
              "20, -90"
            ]
          }
        }
      }
    }
  }
}

geo_point 类型

编辑

查询 需要在相关字段上设置 geo_point 类型。

忽略未映射

编辑

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