elasticsearch Geo Bounding Box Query

Geo Bounding Box Query

一种查询,允许根据一个点位置过滤命中,使用一个边界框。假设以下索引文档:

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

PUT /my_locations/_doc/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
                        }
                    }
                }
            }
        }
    }
}

查询参数

_name:可选名称字段来识别过滤器

validation_method:设置为忽略不正确的纬度或经度的地质点,设置为强制也试图推断正确的纬度或经度。(默认是严格)

type:设置为索引或内存,以定义该过滤器是否将在内存中执行或索引。有关进一步详细信息,请参阅下面的类型,默认是内存。

可以被接受的格式

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

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
                        }
                    }
                }
            }
        }
    }
}

Lat Lon 作为数组

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]
                    }
                }
            }
        }
    }
}

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"
                    }
                }
            }
        }
    }
}

边界框作为著名文本(WKT)

GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "pin.location" : {
                        "wkt" : "BBOX (-74.1, -71.12, 40.73, 40.01)"
                    }
                }
            }
        }
    }
}

Geohash

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

顶角

边界框的顶点可以由上左和右下,也可以由上右和下左参数设置。更多的是支持左、右、上右和左下的名称。不要设置成对的值,而是可以使用简单的名称顶部、左边、底部和右边来分别设置这些值。

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
                    }
                }
            }
        }
    }
}

geo_point type

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

每个索引文档多个坐标

这个过滤器可以处理每个文档的多个位置/点。一旦一个位置/点与过滤器匹配,文档就会被包含在过滤器中

类型

默认情况下,边界框执行的类型被设置为内存,这意味着在内存检查中,如果doc落在边界框范围内。在某些情况下,一个索引选项将执行得更快(但是注意,在这种情况下,geopoint类型必须有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.10,
                            "lon" : -71.12
                        }
                    },
                    "type" : "indexed"
                }
            }
        }
    }
}

忽略地图上未标明的

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

笔记精度

地球仪的精度是有限的,在索引时间内总是被四舍五入。在查询期间,边界框的上边界被四舍五入,而较低的边界被四舍五入。结果是,由于舍入误差,在下限(边界框的底部和左边缘)上的点可能不会进入到边界框中。与此同时,即使它们位于边缘之外,也可以通过查询选择上界(顶部和右边缘)。在纬度上,舍入误差小于4。20e-8度,经度小于8。39-8度,即使在赤道处也会出现小于1厘米的误差。

猜你喜欢

转载自www.cnblogs.com/a-du/p/9233457.html