首先,你需要决定使用什么样的精度。虽然你也可以使用 12 级的精度来索引所有的地理坐标点,但是你真的需要精确到数厘米吗?如果你把精度控制在一个实际一些的值,比如 1km
,那么你可以节省大量的索引空间:
PUT /attractions
{
"mappings": {
"restaurant": {
"properties": {
"name": {
"type": "string"
},
"location": {
"type": "geo_point",
"geohash_prefix": true, (1)
"geohash_precision": "1km" (2)
}
}
}
}
}
-
将
geohash_prefix
设为true
来告诉 Elasticsearch 使用指定精度来索引 geohash 的前缀。 -
精度可以是一个具体的数字,代表的 geohash 的长度,也可以是一个距离。
1km
的精度对应的 geohash 的长度是7
。
通过如上设置, geohash 前缀中 1 到 7 的部分将被索引,所能提供的精度大约在 150 米。