地理多边形查询

编辑

在 7.12 版本中已弃用。

请改用 地理形状,其中多边形以 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)。

允许的格式

编辑
纬度/经度作为数组
编辑

格式为 [经度, 纬度]

注意:此处的经度/纬度顺序必须符合 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 ]
            ]
          }
        }
      }
    }
  }
}
纬度/经度作为字符串
编辑

格式为 纬度,经度

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(默认值)时,如果该字段未映射,则查询将抛出异常。