1. Geo Bounding Box Query(地理边框查询)

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.4/query-dsl-geo-bounding-box-query.html

译文链接 : Geo Bounding Box Query(地理边框查询)

贡献者 : @yangbinApacheCNApache中文网

允许使用边框对基于点位置的点击进行过滤的查询。 假设以下索引文档:

PUT /my_locations
{
    "mappings": {
        "location": {
            "properties": {
                "pin": {
                    "properties": {
                        "location": {
                            "type": "geo_point"
                        }
                    }
                }
            }
        }
    }
}

PUT /my_locations/location/1
{
    "pin" : {
        "location" : {
            "lat" : 40.12,
            "lon" : -71.34
        }
    }
}

然后可以使用 geo_bounding_box 过滤器执行以下简单查询:

GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "pin.location" : {
                        "top_left" : {
                            "lat" : 40.73,
                            "lon" : -74.1
                        },
                        "bottom_right" : {
                            "lat" : 40.01,
                            "lon" : -71.12
                        }
                    }
                }
            }
        }
    }
}

1.1.1. Query Options(查询选项)

Option(选项) Description(描述)
_name(名称) 可选名称字段来标识过滤器。
ignore_malformed(忽略异常) [5.0.0] 设置为 true 以接受无效纬度或经度的地理点(默认值为假)。
validation_method(验证方法) 设置为 IGNORE_MALFORMED 以接受无效纬度或经度的地理点,设置为 COERCE 也尝试推断正确的纬度或经度。 (默认为 STRICT)。
type(类型) 设置为索引或内存之一,以定义此过滤器是否将在内存或索引中执行。 有关详细信息,请参阅下面的 Type 默认值是 Memory。

1.1.2. Accepted Formats(接受格式)

以同样的方式,geo_point 类型可以接受地理点的不同表示,过滤器也可以接受它:

1.1.3. Lat Lon As Properties(Lat Lon 作为属性)

GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "pin.location" : {
                        "top_left" : {
                            "lat" : 40.73,
                            "lon" : -74.1
                        },
                        "bottom_right" : {
                            "lat" : 40.01,
                            "lon" : -71.12
                        }
                    }
                }
            }
        }
    }
}

1.1.4. Lat Lon As Array (Lat Lon 作为排列)

格式在 [lon,lat] 中,注意,这里的 lon / lat 的顺序是为了符合 GeoJSON

GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "pin.location" : {
                        "top_left" : [-74.1, 40.73],
                        "bottom_right" : [-71.12, 40.01]
                    }
                }
            }
        }
    }
}

1.1.5. Lat Lon As String(Lat Lon 作为字符串)

格式在 lat,lon

GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "pin.location" : {
                        "top_left" : "40.73, -74.1",
                        "bottom_right" : "40.01, -71.12"
                    }
                }
            }
    }
}

1.1.6. Geohash(地理散列)

GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "pin.location" : {
                        "top_left" : "dr5r9ydj2y73",
                        "bottom_right" : "drj7teegpus6"
                    }
                }
            }
        }
    }
}

1.1.7. Vertices(顶点)

边框的顶点可以由 top_left bottom_righttop_rightand bottom_left 参数设置。 更多的名称 topLeft,bottomRight,topRight和bottomLeft 是支持的。 而不是成对设置值,可以使用简单的名称顶部,左侧,底部和右侧单独设置值。

GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "pin.location" : {
                        "top" : 40.73,
                        "left" : -74.1,
                        "bottom" : 40.01,
                        "right" : -71.12
                    }
                }
            }
        }
    }
}

1.1.8. geo_point Type(geo_point类型)

过滤器需要在相关字段上设置 geo_point 类型。

1.1.9. Multi Location Per Document(每个文档的多个位置)

过滤器可以与每个文档的多个位置/点配合使用。 一旦单个位置/点与过滤器匹配,文档将被包含在过滤器中。

1.1.10. Type(类型)

默认情况下,边框执行的类型设置为内存,这意味着在内存中检查文档是否在边框范围内。 在某些情况下,索引选项的执行速度会更快(但是请注意,在这种情况下,geo_point 类型必须具有 latlon索引)。 请注意,使用索引选项时,不支持每个文档字段的多个位置。 这是一个例子:

GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "pin.location" : {
                        "top_left" : {
                            "lat" : 40.73,
                            "lon" : -74.1
                        },
                        "bottom_right" : {
                            "lat" : 40.10,
                            "lon" : -71.12
                        }
                    },
                    "type" : "indexed"
                }
            }
        }
    }
}

1.1.11. Ignore Unmapped(忽略未映射)

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

Copyright © Kilvn 2021. all right reserved,powered by Gitbook最后更新时间: 2021-06-08 20:22:42

results matching ""

    No results matching ""