需求整理:
geohash 7网格存储工作热度和学习热度数值,支持随机区域多个范围的热度聚合;
创建索引结构
索引文档需要包含 Geohash 网格、工作热度和学习热度等字段。可以在 Elasticsearch 中定义一个索引,确保 location 字段的类型是 geo_point,用于支持地理空间查询。
PUT /geohash_index
{
"mappings": {
"properties": {
"geohash": {
"type": "keyword"
},
"location": {
"type": "geo_point"
},
"work_heat": {
"type": "integer"
},
"study_heat": {
"type": "integer"
}
}
}
}
插入数据
POST /geohash_index/_doc/1
{
"geohash": "wx4g0f0",
"location": {
"lat": 39.9042,
"lon": 116.4074
},
"work_heat": 100,
"study_heat": 50
}
聚合查询
使用 Elasticsearch 的地理空间范围聚合 (geo_distance aggregation) 实现不同范围内的数据聚合,比如 500m、1.5km、3km,统计工作热度和学习热度。
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.ran